我正在使用nodejs编写一个简单的服务器应用程序,我想将rabbitmq用作其他服务的消息代理。我的问题是我不明白如何重用在其他文件中异步创建的通道对象?
RabbitMQ有以下几个:
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var q = 'hello';
var msg = 'Hello World!';
ch.assertQueue(q, {durable: false});
ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() { conn.close(); process.exit(0) }, 500);
});
我想实现类似的目标:
var channel = require('channelcreation');
// some express.js router logic
router.get('path', function(req, res) {
. . .
channel.send(...);
. . .
})