-
Notifications
You must be signed in to change notification settings - Fork 37
Connection class
The connection class defines the connection with the AMQP server.
- constructor
- close
- declareExchange
- declareQueue
- declareTopology
- completeConfiguration
- deleteConfiguration
A detailed reference explaining the meaning and use of each method and property.
constructor ( url?: string,
socketOptions?: any,
reconnectStrategy?: ReconnectStrategy)
Creates a new connection to an AMQP server
url?: string
: amqp connection string in amqplib.connect format, defaults to"amqp://localhost"
.socketOptions?: any
: socket options as explained in amqplib.connect.reconnectStrategy?: ReconnectStrategy
: defines the reconnection strategy used, defaults to{retries: 0 //forever//, interval: 1500 //ms//}
.import * as Amqp from "amqp-ts"; var connection = new Amqp.Connection("amqp://localhost?heartbeat=60");
connection.close (): Promise<void>
Closes the connection
Promise<void>
: promise that resolves when the connection is closed.connection.close();
connection.declareExchange ( name: string,
type?: string,
options?: Exchange.DeclarationOptions)
: Exchange
Connect to the exchange on the server and create it if it does not already exist. If an exchange with the same name is already declared for this connection, the already declared exchange instance is returned and type and options parameters are ignored.
name: string
: exchange name.type?: string
: exchange type, a valid AMQP exchange type name.options?: Exchange.DeclarationOptions
: exchange options as defined in amqplib. An extra exchange declaration option has been added in v1.3:noCreate
, this connects to an already existing AMQP exchangename
, ignoring the exchange type and all other declaration options.
Exchange
: the declared exchange.connection.declareExchange("exchangeName", "amq.topic", {durable: false}); // expect an existing exchange connection.declareExchange("existingExchangeName", "", {noCreate: true});
connection.declareQueue ( name: string,
options?: Queue.DeclarationOptions)
: Queue
Connect to the queue on the server and create it if it does not already exist. If a queue with the same name is already declared for this connection, the already declared queue instance is returned and type and options parameters are ignored.
name: string
: queue name.options?: Queue.DeclarationOptions
: exchange options as defined in amqplib. An extra queue declaration option has been added in v1.3:noCreate
, this connects to an already existing AMQP queuename
, ignoring all other declaration options.
Queue
: the declared queue.connection.declareQueue("queueName", {durable: false}); // expect an existing queue connection.declareQueue("existingQueueName", {noCreate: true});
connection.declareTopology (topology: Connection.Topology): Promise<void>
Declare a topology, consisting of zero or more Exchanges, Queues and Bindings.
Promise<any>
: promise that resolves when all declared exchanges, queues and bindings for the topology are resolved.var topology = { exchanges: [ {name: "exchange1", type: "direct", options: {durable: false}}, {name: "exchange2"} ], queues: [ {name: "queue1", options: {messageTtl: 60000}}, {name: "queue2"} ], bindings: [ {source: "exchange1", queue: "queue1", pattern: "debug", args: {}}, {source: "exchange1", exchange: "exchange2", pattern: "error"}, {source: "exchange2", queue: "queue2"} ] }; connection.declareTopology(topology).then() => { // do things when everything is in place }
connection.completeConfiguration (): Promise<void>
Makes sure every defined Exchange, Queue and Binding for this Connection is resolved.
Promise<any>
: promise that resolves when all defined exchanges, queues and bindings for the connection are resolved.connection.completeConfiguration().then(() => { // do things when everything is in place });
connection.deleteConfiguration (): Promise<void>
Deletes every defined Exchange, Queue and Binding defined in this Connection.
warning: this deletes the exchanges, queues and bindings from the AMQP server, even if they already existed before.
Promise<any>
: promise that resolves when all defined exchanges, queue and bindings in the connection have been deleted.connection.deleteConfiguration().then(() => { // everything we created has been removed from the server connection.close(); });
connection.initialized: Promise<void>
indicates whether the connection initialization is resolved (or rejected)
connection.initialized.then(() => { // stuff to do } connection.initialized.catch((err) => { // something went wrong }