RabbitMQ
The RabbitMQ plugin creates and manages named RabbitMQ connections for your application. It is built on amqp-connection-manager, so connection URLs, manager options, reconnection behavior, and channel usage follow that library. See its documentation for details on using the connection manager directly.
Install
Install the RabbitMQ plugin in your project:
npm install @axisparkjs/rabbitmq
Configure connections
Register RabbitMQPlugin with options created by RabbitMQPluginOptionsFactory. Add one entry for each connection, with a unique name, a RabbitMQ url, and optional amqp-connection-manager options:
import { AxiSparkFactory } from '@axisparkjs/core';
import { RabbitMQPlugin, RabbitMQPluginOptionsFactory } from '@axisparkjs/rabbitmq';
export const app = AxiSparkFactory.create({
name: 'My application',
basePath: __dirname
});
app.use(
RabbitMQPlugin,
RabbitMQPluginOptionsFactory.create({
connections: [
{
name: 'ORDERS',
url: 'amqp://user:password@localhost:5672',
options: {
heartbeatIntervalInSeconds: 5,
reconnectTimeInSeconds: 5
}
},
{
name: 'EVENTS',
url: ['amqp://broker-a:5672', 'amqp://broker-b:5672']
}
]
})
);
Each url can be a single connection URL or an array of URLs. When given multiple URLs, the connection manager tries them in order until it connects. The optional options object is passed to amqp-connection-manager; refer to its API documentation for the available settings.
The plugin establishes all configured connections during registration and closes them when the plugin stops. Connection names must be unique because the name is used to retrieve and inject a connection.
Retrieve a connection
There are two ways to access configured connections.
Inject a named connection
Use @InjectRabbitMQConnection(name) on a constructor parameter to inject one connection by its configured name. The injected value is an AmqpConnectionManager from amqp-connection-manager:
import { InjectRabbitMQConnection } from '@axisparkjs/rabbitmq';
import type { AmqpConnectionManager } from 'amqp-connection-manager';
export class OrderPublisher {
constructor(
@InjectRabbitMQConnection('ORDERS')
private readonly connection: AmqpConnectionManager
) {}
isConnected(): boolean {
return this.connection.isConnected();
}
}
The decorator resolves the name configured in connections. Its underlying dependency injection token is RABBITMQ_CONNECTION_<name>; you can also use the standard @Inject(new InjectionToken('RABBITMQ_CONNECTION_ORDERS')) form if needed.
Use the connection manager
Inject RabbitMQConnectionManager to look up one connection or retrieve a copy of the map containing all configured connections:
import { RabbitMQConnectionManager } from '@axisparkjs/rabbitmq';
export class ConnectionInspector {
constructor(private readonly connections: RabbitMQConnectionManager) {}
inspect(): void {
const orders = this.connections.getConnection('ORDERS');
const allConnections = this.connections.getAllConnections();
console.log(orders?.isConnected());
console.log([...allConnections.keys()]);
}
}
getConnection(name) returns the connection for that name, or undefined if no connection with that name is configured. getAllConnections() returns a new Map<string, AmqpConnectionManager> containing all configured connections.