it('should fulfill a request/reply event', function (done) { var connection = amqp.createConnection({ host: process.env.NYHOST, port: 5672, login: process.env.NYLOGIN, password: process.env.NYPASSWORD }); connection.on('ready', function () { var nyply = new (require('../nyply'))(connection); nyply.call('test', { blah: 'blah' }, function response(err, response) { assert.equal(response.response, 'OK'); done(); }, 10000); }); var responseConnection = amqp.createConnection({ host: process.env.NYHOST, port: 5672, login: process.env.NYLOGIN, password: process.env.NYPASSWORD }) responseConnection.on('ready', function () { responseConnection.queue('test', { 'durable': false, 'autoDelete': true }, function (q) { q.subscribe(function (message, headers, deliveryInfo, m) { responseConnection.publish(m.replyTo, { response: "OK", index: message.index }, { contentType: 'application/json', contentEncoding: 'utf-8', correlationId: m.correlationId }); }); }); }); })
t.test( 'get connections', function ( t ) { t.plan( 2 ); connection1 = amqp.createConnection( {url:amqp_url} ); connection2 = amqp.createConnection( {url:amqp_url} ); connection1.on( 'ready', function () { t.ok( connection1, 'Got connection #1'); }); connection2.on( 'ready', function () { t.ok( connection2, 'Got connection #2'); }); });
50, // limit function iterator(i, callback){ var connection = amqp.createConnection({ host: 'localhost' }); connection.on('error', amqpError); // log some progress if(i % 1000 === 0){ console.log(('\t > ' + i + ' clients connected').blue); } connection.on('ready', function (err) { var exchange = connection.exchange('messages', { type: 'fanout' }, function() { }); // setup queue var queue = connection.queue( 'pubsub' + i, function (q) { q.bind(exchange, "messages", function() { }); q.subscribe(function queueCallback (message, headers, deliveryInfo, messageObject){ // when message is received, increment counter messagesReceived++; }); // Conncetion finished clientsConnected++; callback(); }); }); },
exports.startServer = function(host) { host = host || 'localhost' var connection = amqp.createConnection({ host: host }); connection.on('ready', function() { connection.queue('core-route',function(q) { console.log("READY"); q.subscribe(function(msg) { console.log("hit"); if(msg.method && commands[msg.method]) { var f = msg.method; f.call(msg.params); } else if(msg.method == 'route.register') { console.log("REG"); console.log("params=" + JSON.stringify(msg.params)); console.log(msg.toString('utf-8')); } else { console.log(msg.toString('utf-8')); } }) }) }) }
superMinionStart(function(err, state) { if(err) { callback(err, state); } else { state.connection = amqpMod.createConnection(self.amqpConfig); state.connection.on('error', function(what) { options.logger('General Error: ' + util.inspect(what)); }); state.connection.on('ready', function() { Step( function workersExchange() { self.createWorkersExchange(state.connection, exchangeName, this); }, function workersRetryExchange(workersExchange) { state.exchange = workersExchange; self.createWorkersRetryExchange(state.connection, exchangeName, this); }, function done(workersRetryExchange) { state.exchangeRetry = workersRetryExchange; callback(undefined, state); } ); }); } });
var init = module.exports.init = function(mqConfig, callback) { if (!connection) { log().info('Initializing RabbitMQ connector.'); connection = amqp.createConnection(mqConfig.connection); connection.on('error', function(err) { log().error({'err': err}, 'Error connecting to RabbitMQ.'); return callback(err); }); connection.on('close', function() { log().info('Closed connection to RabbitMQ'); }); connection.on('ready', function() { log().info('Connection to RabbitMQ established.'); // Initialize the exchange for the job queues connection.exchange(Constants.DEFAULT_TASK_EXCHANGE_NAME, Constants.DEFAULT_TASK_EXCHANGE_OPTS, function(exchange) { queueExchange = exchange; return callback(); }); }); } else { log().warn('Attempted to initialize an existing RabbitMQ connector. Ignoring'); callback(); } };
listen() { this.logger.info('listening for requests'); this.connection = amqp.createConnection(); // Wait for connection to become established. this.connection.on('ready', () => { this.logger.info('connected to rabbitmq'); // Use the default 'amq.topic' exchange this.connection.queue('convert-request-queue', q => { // Catch all messages q.bind('#'); // Receive messages q.subscribe(message => { let data = message.data.toString('utf8'); let jsonMessage = JSON.parse(data); switch(jsonMessage.action) { case 'convert': this._convert(jsonMessage.ticketId, jsonMessage.data); break; case 'cancel': this._cancel(jsonMessage.ticketId); break; case 'cleanup': this._clean(jsonMessage.data); break; } }); }); }); }
followToAMQP = function (cfgCouch, cfgAMQP, format, condition) { condition = condition || function (cb) { cb(); }; var connection = amqp.createConnection(cfgAMQP); connection.on('ready', function () { connection.exchange(cfgAMQP.exchange, { type: 'topic' }, function (exchange) { var db = url.parse(cfgCouch.db).pathname.substr(1); cfgCouch.include_docs = true; follow(cfgCouch, function (err, change) { condition (function () { if (!err) { var doc = change.doc, msg = (format || defaultFormat)(doc); if (msg.body) { exchange.publish(msg.name, msg.body, msg.options); } } }); }); }); }); };
function main( workerID ) { var connection = amqp.createConnection({host: 'localhost'}); var workerStr = workerID ? "W["+ workerID +"]" : ""; connection.on('ready', function() { connection.queue('task_queue', {autoDelete: false, durable: true}, function(queue) { console.log('[*] Waiting for messages. To exit press CTRL+C'); queue.subscribe({ack: true, prefetchCount: 1}, function (json, headers, info, message) { var body = json.data.toString('utf-8'); console.log(" [*]%s Received %s", workerStr, body); setTimeout( function() { var rnd = Math.floor((Math.random()*3)+1); if ( rnd == 2 ) { console.log( " [x]%s Failed %s", workerStr, body ); message.reject(true); //queue.shift(true, true); // NOT WORK: https://github.com/postwait/node-amqp/issues/210 } else { console.log( " [√]%s Done %s", workerStr, body ); queue.shift(); // basic_ack equivalent } }, 4000 ); }); }); }); }
function Context(opts) { EventEmitter.constructor.call(this); var that = this; var c = this._connection = amqp.createConnection(opts); c.on('ready', function() { that.emit('ready') }); c.on('error', function(e) { that.emit('error', e) }); };
module.exports.Client = function* (url) { var a = Object(); info('AMQP Connect: ' + url); var defer = Q.defer(); var conn = amqp.createConnection({url: url}); conn.on('ready', function () { defer.resolve(a); }); a.subscribe = function (exchange, routing_key, callback) { conn.queue('', function (q) { q.bind(exchange, routing_key, function () { q.subscribe(function (message, headers, deliveryinfo, messageObject) { var pkt = JSON.parse(message.data); callback(pkt); }); }); }); } a.publish = function* (exchange, routing_key, pkt) { var defer2 = Q.defer(); conn.exchange(exchange, {'passive': true}, function (xch) { xch.publish(routing_key, pkt); defer2.resolve(); }); return yield defer2.promise; } return yield defer.promise; }
function initializeBrokerResourcesAndThen(doCallback, busSettings) { // init relayerConnection, deadletter exchange, queue and bind them. doCallback() when finished. busSettings = busSettings || {}; relayerConnection = amqp.createConnection(busSettings); relayerConnection.on('ready', function () { relayerConnectionIsOk = true; logger.info('Creating', deadletterName, 'in:', relayerConnection.options); var queue = relayerConnection.queue(deadletterName, deadletterQueueOptions, function (deadletterQueue) { deadletterExchange = relayerConnection.exchange(deadletterName, deadletterExchangeOptions, function () { queue.bind(deadletterName, deadletterQueueRk, function () { logger.info('Created queue[', deadletterName, '] exchange [', deadletterName, '] and binded with Routing Key [', deadletterQueueRk, ']'); if (doCallback) { doCallback(); } }) }); deadletterExchange.on('error', _logError('Error creating deadletter exchange:')); }); queue.on('error', _logError('Error creating deadletter queue: ')); }); relayerConnection.on('error', function (err) { logger.error('Error connecting to broker: ' + inspect(busSettings) + "Error: " + err); // eyeos-run-server should restart the service process.exit(1); }); relayerConnection.on('close', function () { logger.info('Closed connection to broker: ' + inspect(busSettings)); // eyeos-run-server should restart the service process.exit(1); }); }
exports.connect = function(callback) { // Create the amqp connection to rabbitMQ server connection = amqp.createConnection(addr); connection.on('ready', function () { //Create a direct exchange exc = connection.exchange('rpcExchange', {type: 'direct'}, function (exchange) { logger.info('Exchange ' + exchange.name + ' is open'); //Create the queue for send messages clientQueue = connection.queue('', function (q) { logger.info('ClientQueue ' + q.name + ' is open'); clientQueue.bind('rpcExchange', clientQueue.name); clientQueue.subscribe(function (message) { if(map[message.corrID] !== undefined) { clearTimeout(map[message.corrID].to); map[message.corrID].fn(message.data); delete map[message.corrID]; } }); callback(); }); }); }); }
function _openBusConnectionAndExchangeThenSend(userBusSettings, destinationName, busMessage) { var connection = amqp.createConnection(userBusSettings); // disposable connection for this request. connection.on('ready', function () { logger.debug('Connection established for user request:', connection.options); var exchange = connection.exchange(destinationName, {passive: true}, function () { logger.debug('Exchange opened for user request:', exchange.name, ' => publishing message.', busMessage); exchange.publish(busMessage.routingKey, JSON.stringify(busMessage)); _closeConnection(connection, destinationName); }); exchange.on('error', function (err) { logger.warn('Error on exchange: ', destinationName, err); // error in exchange usually means that user has no permissions or exchange does not exist. send to deadletter. _sendToDeadletter(destinationName, busMessage, err, userBusSettings); _closeConnection(connection, destinationName); }) }); connection.on('error', function (err) { debugger; logger.error('Error connecting to RabbitMQ for request: ', busMessage, '\nRequest connection Settings:', userBusSettings, '\nError:', err); // if relayerConnection is OK, send to deadletter exchange, and cancel reconnect // if relayerConnection is KO, we lost connection to broker. Maintain reconnect=true, eventually it should work. if (relayerConnectionIsOk) { // disable reconnect, since autoreconnect was enabled, connection will raise a 2nd error // for that reason, remove all error listeners and add a dummy listener, since the error is being handled now. _closeConnection(connection, destinationName); err.code = 401; err.codeDescription = 'Unauthorized'; deadletterExchange.publish(destinationName, JSON.stringify({busMessage: busMessage, error: err, busSettings: userBusSettings})); } else { logger.warn('Lost connection to RabbitMQ. Error trying to publish on exchange: ', destinationName); } }); }
Starsky.prototype.connect = function (callback) { debug('connect'); var config = this.config; var options = { host: config.get('mq host') , port: config.get('mq port') , login: config.get('mq username') , password: config.get('mq password') , vhost: config.get('mq vhost') , heartbeat: config.get('mq heartbeat') , ssl: { enabled: config.get('mq tls') , keyFile: config.get('mq tls key') , certFile: config.get('mq tls cert') } }; if ('function' == type(callback)) { this.on('connect', function listener () { this.removeListener('connect', listener); callback(); }); } this.connection = amqp.createConnection(options); this.connection.on('error', this.onConnectionError.bind(this)); this.connection.on('close', this.onConnectionClose.bind(this)); this.connection.on('ready', this.onConnectionReady.bind(this)); this.connection.on('heartbeat', this.onConnectionHeartbeat.bind(this)); };
/** Private functions */ /** * Setup the connection * @param {Object} connectCfg AMQP connection configurations. * @param {Object} options AMQP connection options. * See https://github.com/postwait/node-amqp#connection-options-and-url for the format of the connectCfg and options */ function setupConn(connectCfg, options) { var conn = amqp.createConnection(connectCfg, options); conn.on('ready', function(){ debug('received ready event from node-amqp'); var eventName = 'connection'; //Ready event will be emitted when re-connected. //To keep backward compatible, only emit 'connection' event for the first time if(_connection){ eventName = 'reconnect'; } _connection = conn; _connection._isClosed = false; self.emit('ready'); // wrapped in 'nextTick' for unit test friendliness process.nextTick(function(){ debug('going to emit ' + eventName); self.emit(eventName); }); autoSubscribe(); publishCachedMessages(); }); conn.on('error', function(err){ self.emit('error', err); }); conn.on('close', function(){ if(_connection){ _connection._isClosed = true; } }); }
App.prototype.setupAmqpClient = function () { var self = this; var connection = self.amqpConnection = amqp.createConnection(self.config.amqp, { log: self.log }); connection.on('ready', function () { connection.connected = true; self.log.info('AMQP connection ready'); self.moray.ensureClientReady(function () { self.ur.useConnection(self.amqpConnection); self.ur.bindQueues(); }); }); connection.on('error', function (e) { connection.connected = false; self.log.error(e); }); connection.on('end', function () { connection.connected = false; }); // Set up Ur client. self.log.debug('Ready to communicate with ur'); self.ur = new Ur({ log: self.log }); self.ur.on('serverSysinfo', self.onSysinfoReceivedUr.bind(self)); };
self.backend.on('ready', function() { debug('Connecting to broker...'); if (self.conf.broker_type === 'redis') { self.broker = new RedisBroker(self.conf); } else if (self.conf.broker_type === 'amqp') { self.broker = amqp.createConnection(self.conf.BROKER_OPTIONS, { defaultExchangeName: self.conf.DEFAULT_EXCHANGE }); } self.broker.on('error', function(err) { self.emit('error', err); }); self.broker.on('end', function() { self.emit('end'); }); self.broker.on('ready', function() { debug('Broker connected...'); self.ready = true; debug('Emiting connect event...'); self.emit('connect'); }); });
TrackingClient.prototype.start = function() { this.emit('log', 'starting'); this.exchangeOpen = false; this.mqConnection = amqp.createConnection({url:this.mqUrl}); this.mqConnection.on('ready', this.mqConnectionReady.bind(this)); this.mqConnection.on('error', this.emit.bind(this)); };
P.connect = function() { var self = this; this.connection = amqp.createConnection({url: this.url}); this.connection.on('ready', function () { var destroyQueue = function(callback) { var q = self.connection.queue(self.options.queueName, {durable:true, 'autoDelete': false}, function(queue) { queue.destroy(); if (callback) callback(); }); }; var startAdapter = function() { var adapter = require('./adapters/' + self.task); adapter.create(self, function(err) { if (err) { } else { self.emit('connection', self); } }); }; if (self.options.clean) { destroyQueue(startAdapter); } else { startAdapter(); } }); };
fs.readFile(gitosisConfig, function(e, d) { if(e){ sys.puts("[WARNING] Unable to read configuration file: " + gitosisConfig + " : " + e); } else { var config; try { config = ini.parse(d); } catch(err) { throw new Error("[ERROR] Unable to parse config file '" + gitosisConfig + "': " + err); } if(currentAMQPConnection){ // This is an expected close event so we don't want to attempt reconnect on the // same handle currentAMQPConnection.removeListener('close', connectionCloseHandle); currentAMQPConnection.close(); currentAMQPConnection = null; } var connection = amqp.createConnection({port:Number(config.amqp.port), host:config.amqp.host}); connection.config = config; connection.addListener('close', connectionCloseHandle ); connection.addListener('ready', connectionReadyHandle.bind(this,connection)); currentAMQPConnection = connection; } });
exports.init = function(queue, callback) { connection = amqp.createConnection({ host: conf.RABBIT_HOST, port: conf.RABBIT_PORT }); connection.on('ready', function() { conf.logger.log('[RabbitMQ] Connection established'); connection.queue(queue, { durable: true, 'exclusive': false, 'autoDelete': false }, function(q) { conf.logger.log('[RabbitMQ] Queue declared'); q.bind('#'); q.subscribe(callback); }); }); connection.on('error', function() { conf.logger.warn('[RabbitMQ] Error', arguments); }); };
Rabbit.prototype.connect = function (config, readycallback) { var self = this; this.connection = amqp.createConnection(config.connection, { heartbeat: 1 }); if (readycallback) this.on('ready', function(me) { readycallback(null, me); } ); this.connection.on('error', function (e) { console.log("ERROR:",e); self.emit('error', self.exchanges, self.queues, e ); process.nextTick(function () { self.exchange = []; // stop pointing to the old array self.queues= []; // stop pointing to the old array }); } ); this.connection.on('close', function (e) { self.emit('close', self.exchanges, self.queues, e ) } ); this.on('allDone', function () { self.connection.end(); } ); this.connection.on('ready', function () { self.on('Rabbit_Declare_Exchange', function (exchdef, callback) { self.exchange(exchdef, callback); } ); self.on('Rabbit_Declare_Queue', function (queuedef, callback) { self.queue(queuedef, callback); } ); // first the exchanges, then the queues! async.series( [ function (sercb) { if (config.exchanges) { async.map( config.exchanges, function (exchdef, mapcb) { self.emit('Rabbit_Declare_Exchange', exchdef, mapcb); }, function (error, results) { if (error) return sercb(error); sercb(null, results); } ); } else { sercb(); } } , function (sercb) { if (config.queues) { async.map( config.queues, function (queuedef, mapcb) { self.emit('Rabbit_Declare_Queue', queuedef, mapcb); }, function (error, results) { if (error) return sercb(error); sercb(null, results); } ); } else { sercb(); } } ], function (error, results) { if (error) console.log("RABBIT ERROR CALLBACK", error); if (readycallback && error) return readycallback(error); if (error) throw error; self.emit('ready', self); } ); } ); }
function rabbitConnect (server) { var connection = amqp.createConnection({ host: server }); connection.on('error', function(err){ console.log("Rabbit error: ", err.message); }); // Wait for connection to become established. connection.on('ready', function () { console.log('Rabbit connected to ' + server); // Skip current server if ( server !== settings.host ) { connection.queue(QUEUE_NAME, function(q){ // Catch all messages q.bind('#'); console.log('Rabbit queue[' + QUEUE_NAME + '] binded on server[' + server + ']'); q.subscribe(function (message) { console.log("Rabbit message: ", message); tskl.addKey(message.origin, message.key); }); mqConnections.push(connection); }); } else { mqConn = connection; } }); }
/** * Initializes bus connection, exchanges and queue. * @param callback */ function initialize (callback){ // Connect to bus connection = amqp.createConnection({ url: config.connectionURL }); connection.on('ready', function() { console.log('[BUS] Connected to ' + connection.serverProperties.product); // declare "administration" AMQP exchange adminExchange = connection.exchange('administration', { type: 'topic', durable: 'true' }); // declare agent-specific queue (named with agent hostname) queue = connection.queue(process.env.HOSTNAME, function(q){ // Receive messages q.subscribe(function (message, headers, deliveryInfo) { console.log('[BUS] Received', deliveryInfo.routingKey, 'message:', message); var cb = listeners[deliveryInfo.routingKey]; if (cb){ cb(message); } else { console.log('[BUS] Routing key', deliveryInfo.routingKey, 'not handled. Message discarded.'); } }); callback(null); }); }); }
setup = function() { clearTimeout(timer); timer = null; connection = amqp.createConnection(options, {reconnect: false}); // the connection may become ready many times // the amqp library has a built-in retry mechanism connection.on('ready', function () { connection.exchange(argv['output-exchange'], {}, function (ex) { exchange = ex; emitter.emit('ready'); }); }); // when the connection closes, cycle everything connection.on('close', cycle); // prevent error events from bubbling up as thrown exceptions connection.on('error', function(err){ // design choice: don't cycle from here, only on 'close' // rationale: testing shows that many error events happen for each close event // alternative: call cycle and let the if(!timer) handle scheduling }); };
init : function(){ if(!this._bindFlag){ this._client = amqp.createConnection(rabbitmqConfig.server_option); var _self = this; this._client.once("ready", function(){ rabbitMqLog.info('SSOLogSendService class init method connection success! '); _self._bindFlag = true; //------ _self._client.exchange(rabbitmqConfig.logExchangeName, {type:"topic"}, function(exchange){ rabbitMqLog.info('SSOLogSendService class init method Exchange '+exchange.name+' is open! '); _self._client.queue(rabbitmqConfig.logQueueName, function(queue){ rabbitMqLog.info('SSOLogSendService class init method Queue '+queue.name+' is open! '); queue.bind(exchange,rabbitmqConfig.logQueueName, function(){ _self._exchange = exchange; _self._queue = queue; }); }); }); /**this._client.once('heartbeat',function(){ rabbitMqLog.info('SSOLogSendService class init method heartbeat event'); });*/ _self._client.once('error', function(err){ _self.destroy(); throw new Error('rabbitmq connection error : '+err); }); }); } },
exports.initialize = function(){ //get the server status from the configuration file. this value should be true to use this var isMessagingServerRunning = serverConfig.status; if(isMessagingServerRunning === 'true'){ logger.info('MessagingFactory', ' Starting AMQP Messaging server....'); //create a connection to the external AMQP server connection = amqp.createConnection(serverConfig.server); connection.on('error', function(err){ logger.error('MessagingFactory', 'Connection to the AMQP server failed!!.' + 'Attempting to reconnect.....'); }); //start the subscribers amqpSubs.startSub(connection, eventSubs); logger.info('MessagingFactory', ' AMQP Messaging server Started'); } else if(isMessagingServerRunning === 'false'){ logger.info('MessagingFactory', ' AMQP Messaging server is not running'); } else { logger.error('MessagingFactory', ' Syntax error. Check the variable \'amqp.status\' in \'server_config.json\''); } }
function Bus(options, implOpts) { var noop = function () {}; options = options || {}, implOpts, self = this; options.url = options.url || process.env.RABBITMQ_URL || 'amqp://localhost'; implOpts = implOpts || { defaultExchangeName: 'amq.topic' }; this.log = options.log || { debug: noop, info: noop, warn: noop, error: noop }; this.delayOnStartup = options.delayOnStartup || 10; this.initialized = false; this.log.debug('connecting to rabbitmq on ' + options.url); this.connection = amqp.createConnection(options, implOpts); this.pubsubqueues = {}; this.queues = {}; var self = this; this.connection.on('error', function (err) { self.log.error('Error connecting to rabbitmq at ' + options.url + ' error: ' + err.toString()); throw err; }); this.connection.on('close', function (err) { self.log.debug('rabbitmq connection closed.'); }); this.connection.on('ready', function () { self.initialized = true; self.log.debug("rabbitmq connected to " + self.connection.serverProperties.product); }); }
exports.getConnection = function (){ if(connection != null){ return connection; } else { var isMessagingServerRunning = serverConfig.status; if(isMessagingServerRunning === 'true'){ logger.info('MessagingFactory', ' Starting AMQP Messaging server....'); connection = amqp.createConnection(serverConfig.server); connection.on('error', function(err){ logger.error('MessagingFactory', 'Connection to the AMQP server failed!!.' + 'Attempting to reconnect.....'); }); logger.info('MessagingFactory', ' AMQP Messaging server Started'); } else if(isMessagingServerRunning === 'false'){ logger.info('MessagingFactory', ' AMQP Messaging server is not running'); } else { logger.error('MessagingFactory', ' Syntax error. Check the variable \'amqp.status\' in \'server_config.json\''); } return connection; } }