如果兔崽子不允许你指定的交流,队列或绑定,那么你就需要找到另一个库。
问题是你的绑定。如您所见,您可以从管理界面创建交换,队列和绑定。但是,在Jackrabbit创建特定于工作人员的队列时,您将无法在交换和Jackrabbit创建的队列之间创建绑定。
我使用,并强烈建议,在wascally库。它建立在amqplib之上,与Jackrabbit相同,但提供了更多功能丰富的API和功能。
至于如何使用Wascally ...设置三个文件与此代码的例子:
config.json
{
"connection": {
"user": "test",
"pass": "password",
"server": "localhost",
"vhost": "test-app"
},
"exchanges":[
{"name": "sample-ex.1", "type": "direct"}
],
"queues":[
{"name": "sample-q.1"}
],
"bindings":[
{"exchange": "sample-ex.1", "target": "sample-q.1"}
]
}
receiver.js
var rabbit = require("wascally");
var config = require("config.json");
rabbit.configure(config)
.then(function(){
rabbit.handle("test.message.type", handleMessage);
rabbit.startSubscription("sample-q.1");
})
.then(undefined, reportError);
function reportError(err){
console.log(err.stack);
process.exit();
}
function handleMessage(msg){
var body = msg.body;
console.log("received a message");
console.log(body);
msg.ack();
setTimeout(function(){
// do work
}, 5000);
}
sender.js
var rabbit = require("wascally");
var config = require("config.json");
rabbit.configure(config)
.then(function(){
rabbit.publish("sample-ex.1", {
type: "test.message.type",
routingKey: "",
body: {
foo: "bar"
}
});
})
.then(undefined, reportError);
function reportError(err){
console.log(err.stack);
process.exit();
}
现在,只要你想,你可以创建接收器的多个实例。几次运行node receiver.js
。
然后运行node sender.js
几次,您将看到通过接收器传来的消息 - 一个接收器收到一条消息,rabbitmq会将消息循环到接收器。
p.s.如果您有兴趣,此代码来自我的RabbitMQ For Devs包中的屏幕录像。