amqp-connection-manager
Advanced tools
Comparing version 2.1.2 to 2.2.0
@@ -0,1 +1,8 @@ | ||
# [2.2.0](https://github.com/benbria/node-amqp-connection-manager/compare/v2.1.2...v2.2.0) (2018-09-25) | ||
### Features | ||
* Set 'this' to be the channel wrapper in the setup function. ([551200f](https://github.com/benbria/node-amqp-connection-manager/commit/551200f)) | ||
## [2.1.2](https://github.com/benbria/node-amqp-connection-manager/compare/v2.1.1...v2.1.2) (2018-09-13) | ||
@@ -2,0 +9,0 @@ |
@@ -52,3 +52,3 @@ 'use strict'; | ||
if (this._channel) { | ||
return _promiseBreaker2.default.call(setup, null, this._channel); | ||
return _promiseBreaker2.default.call(setup, this, this._channel); | ||
} else { | ||
@@ -75,3 +75,3 @@ return undefined; | ||
return (this._settingUp || Promise.resolve()).then(() => this._channel ? _promiseBreaker2.default.call(teardown, null, this._channel) : undefined); | ||
return (this._settingUp || Promise.resolve()).then(() => this._channel ? _promiseBreaker2.default.call(teardown, this, this._channel) : undefined); | ||
}); | ||
@@ -161,2 +161,4 @@ } | ||
this.context = {}; | ||
this._json = 'json' in options ? options.json : false; | ||
@@ -210,3 +212,3 @@ | ||
// TODO: Use a timeout here to guard against setupFns that never resolve? | ||
_promiseBreaker2.default.call(setupFn, null, channel).catch(err => { | ||
_promiseBreaker2.default.call(setupFn, this, channel).catch(err => { | ||
if (this._channel) { | ||
@@ -213,0 +215,0 @@ this.emit('error', err, { name: this.name }); |
{ | ||
"name": "amqp-connection-manager", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Auto-reconnect and round robin support for amqplib.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -10,10 +10,7 @@ [![Build Status](https://travis-ci.org/benbria/node-amqp-connection-manager.svg?branch=master)](https://travis-ci.org/benbria/node-amqp-connection-manager) | ||
Connection management for amqplib. | ||
amqp-connection-manager | ||
======================= | ||
# amqp-connection-manager | ||
Features: | ||
--------- | ||
## Features | ||
@@ -26,20 +23,22 @@ * Automatically reconnect when your amqplib broker dies in a fire. | ||
Installation: | ||
------------- | ||
## Installation | ||
npm install --save amqplib amqp-connection-manager | ||
Basics: | ||
------- | ||
## Basics | ||
The basic idea here is that, usually, when you create a new channel, you do some setup work at the beginning (like | ||
asserting that various queues or exchanges exist, or binding to queues), and then you send and receive messages and | ||
you never touch that stuff again. | ||
The basic idea here is that, usually, when you create a new channel, you do some | ||
setup work at the beginning (like asserting that various queues or exchanges | ||
exist, or binding to queues), and then you send and receive messages and you | ||
never touch that stuff again. | ||
amqp-connection-manager will reconnect to a new broker whenever the broker it is currently connected to dies. When you | ||
ask amqp-connection-manager for a channel, you specify one or more `setup` functions to run; the setup functions will | ||
be run every time amqp-connection-manager reconnects, to make sure your channel and broker are in a sane state. | ||
amqp-connection-manager will reconnect to a new broker whenever the broker it is | ||
currently connected to dies. When you ask amqp-connection-manager for a | ||
channel, you specify one or more `setup` functions to run; the setup functions | ||
will be run every time amqp-connection-manager reconnects, to make sure your | ||
channel and broker are in a sane state. | ||
Before we get into an example, note this example is written using Promises, however much like amqplib, any | ||
function which returns a Promise will also accept a callback as an optional parameter. | ||
Before we get into an example, note this example is written using Promises, | ||
however much like amqplib, any function which returns a Promise will also accept | ||
a callback as an optional parameter. | ||
@@ -54,4 +53,4 @@ Here's the example: | ||
// Ask the connection manager for a ChannelWrapper. Specify a setup function to run every time we reconnect | ||
// to the broker. | ||
// Ask the connection manager for a ChannelWrapper. Specify a setup function to | ||
// run every time we reconnect to the broker. | ||
var channelWrapper = connection.createChannel({ | ||
@@ -61,2 +60,3 @@ json: true, | ||
// `channel` here is a regular amqplib `ConfirmChannel`. | ||
// Note that `this` here is the channelWrapper instance. | ||
return channel.assertQueue('rxQueueName', {durable: true}), | ||
@@ -71,11 +71,12 @@ } | ||
.then(function() { | ||
return console.log("Message was sent! Hooray!"); | ||
return console.log("Message was sent! Hooray!"); | ||
}).catch(function(err) { | ||
return console.log("Message was rejected... Boo!"); | ||
return console.log("Message was rejected... Boo!"); | ||
}); | ||
``` | ||
Sometimes it's handy to modify a channel at run time. For example, suppose you have a channel that's listening to | ||
one kind of message, and you decide you now also want to listen to some other kind of message. This can be done | ||
by adding a new setup function to an existing ChannelWrapper: | ||
Sometimes it's handy to modify a channel at run time. For example, suppose you | ||
have a channel that's listening to one kind of message, and you decide you now | ||
also want to listen to some other kind of message. This can be done by adding a | ||
new setup function to an existing ChannelWrapper: | ||
@@ -92,13 +93,15 @@ ```js | ||
`addSetup()` returns a Promise which resolves when the setup function is finished (or immediately, if the underlying | ||
connection is not currently connected to a broker.) There is also a `removeSetup(setup, teardown)` which will run | ||
`teardown(channel)` if the channel is currently connected to a broker (and will not run `teardown` at all otherwise.) | ||
Note that `setup` and `teardown` *must* either accept a callback or return a Promise. | ||
`addSetup()` returns a Promise which resolves when the setup function is | ||
finished (or immediately, if the underlying connection is not currently | ||
connected to a broker.) There is also a `removeSetup(setup, teardown)` which | ||
will run `teardown(channel)` if the channel is currently connected to a broker | ||
(and will not run `teardown` at all otherwise.) Note that `setup` and `teardown` | ||
*must* either accept a callback or return a Promise. | ||
See a complete example in the [examples](./examples) folder. | ||
API: | ||
---- | ||
## API | ||
### connect(urls, options) | ||
Creates a new AmqpConnectionManager, which will connect to one of the URLs provided in `urls`. If a broker is | ||
@@ -108,2 +111,3 @@ unreachable or dies, then AmqpConnectionManager will try the next available broker, round-robin. | ||
Options: | ||
* `options.heartbeatIntervalInSeconds` - Interval to send heartbeats to broker. Defaults to 5 seconds. | ||
@@ -119,7 +123,8 @@ * `options.reconnectTimeInSeconds` - The time to wait before trying to reconnect. If not specified, | ||
### AmqpConnectionManager events | ||
* `connect({connection, url})` - Emitted whenever we successfully connect to a broker. | ||
* `disconnect({err})` - Emitted whenever we disconnect from a broker. | ||
### AmqpConnectionManager#createChannel(options) | ||
### AmqpConnectionManager#createChannel(options) | ||
Create a new ChannelWrapper. This is a proxy for the actual channel (which may or may not exist at any moment, | ||
@@ -129,18 +134,23 @@ depending on whether or not we are currently connected.) | ||
Options: | ||
* `options.name` - Name for this channel. Used for debugging. | ||
* `options.setup(channel, [cb])` - A function to call whenever we reconnect to the broker (and therefore create a new | ||
underlying channel.) This function should either accept a callback, or return a Promise. See `addSetup` below. | ||
* `options.setup(channel, [cb])` - A function to call whenever we reconnect to the | ||
broker (and therefore create a new underlying channel.) This function should | ||
either accept a callback, or return a Promise. See `addSetup` below. | ||
Note that `this` inside the setup function will the returned ChannelWrapper. | ||
The ChannelWrapper has a special `context` member you can use to store | ||
arbitrary data in. | ||
* `options.json` if true, then ChannelWrapper assumes all messages passed to `publish()` and `sendToQueue()` | ||
are plain JSON objects. These will be encoded automatically before being sent. | ||
### AmqpConnectionManager#isConnected() | ||
### AmqpConnectionManager#isConnected() | ||
Returns true if the AmqpConnectionManager is connected to a broker, false otherwise. | ||
### AmqpConnectionManager#close() | ||
### AmqpConnectionManager#close() | ||
Close this AmqpConnectionManager and free all associated resources. | ||
### ChannelWrapper events | ||
### ChannelWrapper events | ||
* `connect` - emitted every time this channel connects or reconnects. | ||
@@ -150,4 +160,4 @@ * `error(err, {name})` - emitted if an error occurs setting up the channel. | ||
### ChannelWrapper#addSetup(setup) | ||
### ChannelWrapper#addSetup(setup) | ||
Adds a new 'setup handler'. | ||
@@ -168,9 +178,9 @@ | ||
### ChannelWrapper#removeSetup(setup, teardown) | ||
### ChannelWrapper#removeSetup(setup, teardown) | ||
Removes a setup handler. If the channel is currently connected, will call `teardown(channel)`, passing in the | ||
underlying amqplib ConfirmChannel. `teardown` should either take a callback or return a Promise. | ||
### ChannelWrapper#publish and ChannelWrapper#sendToQueue | ||
### ChannelWrapper#publish and ChannelWrapper#sendToQueue | ||
These work exactly like their counterparts in amqplib's Channel, except that they return a Promise (or accept a | ||
@@ -181,13 +191,13 @@ callback) which resolves when the message is confirmed to have been delivered to the broker. The promise rejects if | ||
### ChannelWrapper#ack and ChannelWrapper#nack | ||
### ChannelWrapper#ack and ChannelWrapper#nack | ||
These are just aliases for calling `ack()` and `nack()` on the underlying channel. They do nothing if the underlying | ||
channel is not connected. | ||
### ChannelWrapper#queueLength() | ||
### ChannelWrapper#queueLength() | ||
Returns a count of messages currently waiting to be sent to the underlying channel. | ||
### ChannelWrapper#close() | ||
### ChannelWrapper#close() | ||
Close a channel, clean up resources associated with it. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117297
532
192