我制作了一个简单的应用程序,通过RabbitMQ服务器向客户端发送消息。 为此我创建了发件人端代码,如下所示。
var amqp=require('amqp');
var connection = amqp.createConnection({host:'localhost',login:'guest',password:'guest'});
connection.on('ready', function () {
// There is no need to declare type, 'topic' is the default:
var exchange = connection.exchange('node-topic-exchange');
console.log("publishing messages");
exchange.publish("topic_a.subtopic_a", {msg:'First Message'});
exchange.publish("topic_a.subtopic_b", {msg:'Second Message'});
exchange.publish("topic_b.subtopic_b", {msg:'Third Message'});
});
connection.on('error',function (err) {
console.log('an error '+err);
});
我的接收方代码如下所示:
var amqp=require('amqp');
var connection = amqp.createConnection({host:'localhost',login:'guest',password:'guest'});
connection.on('ready', function () {
// There is no need to declare type, 'topic' is the default:
var exchange = connection.exchange('node-topic-exchange');
// Consumer:
var queue = connection.queue('node-topic-queue-one');
queue.bind(exchange, "topic_a.*");
queue.subscribe(function (message) {
// Get original message string:
console.log('Message : ' + message.msg);
});
});
connection.on('error',function (err) {
console.log('an error '+err);
});
问题是..它给出了类似的错误
D:\ node example \ RabbitMQ示例>节点worker1.js错误
错误: NOT_FOUND - 没有交换' node-topic-exchange'在vhost' /'一个错误 错误:读取ECONNRESET
请帮忙解决这个问题.....
答案 0 :(得分:1)
您需要使用node.js事件驱动方法:"An exchange will emit the 'open' event when it is finally declared." - 当您尝试使用它时,尚未声明交换。
在接收方,你需要为交换和队列做同样的事情:"A queue will call the callback given to the connection.queue() method once it is usable"
为了完整起见,我在此报告示例:
var q = connection.queue('my-queue', function (queue) {
console.log('Queue ' + queue.name + ' is open');
});