我试着让我的听者在我想要立即删除我的消费者之后,只听一条消息。我怎样才能做到这一点。这是密码。
queueListener:function(Queue,timeOut){
var deferred=sails.promise.defer(),timer,data;
sails.amqp.connect('amqp://localhost', function(err, conn) {
conn.createConfirmChannel(function(err, ch) {
if(err){
conn.close();
deferred.reject(err);
}else{
ch.assertQueue(Queue, {durable: true});
ch.prefetch(1);
ch.consume(Queue,function(msg){
data=msg.content.toString();
clearTimeout(timer);
ch.ack(msg);
setTimeout(function(){
conn.close();
deferred.resolve(data);
},0);
},{noAck: false});
}
});
timer=setTimeout(function(){
conn.close();
deferred.reject(new Error("Nothing in the Queue."));
},timeOut-5);
});
return deferred.promise;
}在上面的队列中,是它将要侦听的队列,timeOut表示侦听器侦听的时间。如果它侦听了一条消息,我想停止listening.And以便进一步侦听,下次我将调用函数queueListner。虽然我已经制作了conn.close(),但是在UI中它仍然显示了使用者。

发布于 2016-06-08 14:32:53
不要使用consume。如果只需要获得一条消息,请使用get方法
get
ch.get("queue-name").then(messageHandlerFunction)这将从指定的队列中检索单个消息,并通过指定的消息处理程序函数运行它。
https://stackoverflow.com/questions/37695995
复制相似问题