-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve queue declaration and binding.
- Loading branch information
1 parent
d0877d2
commit c4812c8
Showing
2 changed files
with
82 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var debug = require('debug')('crane-amqp'); | ||
|
||
|
||
/** | ||
* `Broker` constructor. | ||
* | ||
* @api protected | ||
*/ | ||
function Queue(queue, exchange) { | ||
this._q = queue; | ||
this._exchange = exchange; | ||
} | ||
|
||
/** | ||
* Bind queue to topic. | ||
* | ||
* @api public | ||
*/ | ||
Queue.prototype.bind = function(topic, cb) { | ||
// AMQP uses period ('.') separators rather than slash ('/') | ||
topic = topic.replace(/\//g, '.'); | ||
|
||
var q = this._q | ||
, exchange = this._exchange; | ||
|
||
var onError = function(err) { | ||
return cb(err); | ||
}; | ||
|
||
debug('bind %s %s %s', q.name, this._exchange, topic); | ||
q.bind(exchange, topic, function(q) { | ||
q.removeListener('error', onError); | ||
return cb(); | ||
}); | ||
|
||
// NOTE: This will occur if an attempt is made to bind to an exchange that | ||
// does not exist. | ||
// | ||
// For example, the underlying `amqp` emits an error with the following | ||
// properties: | ||
// - message: NOT_FOUND - no exchange 'foo' in vhost '/' | ||
// - code: 404 | ||
q.once('error', onError); | ||
} | ||
|
||
|
||
/** | ||
* Expose `Broker`. | ||
*/ | ||
module.exports = Queue; |