Node.js RabbitMQ中的预取和消息处理顺序

Node.js RabbitMQ中的预取和消息处理顺序,node.js,rabbitmq,Node.js,Rabbitmq,我有一个专门用于消费的频道和50个队列,每个队列只有一个消费者。 该通道的预取值为5,适用于 我的应用程序需要按照在每个队列中接收消息的顺序准确地处理消息。 由于预取值较高,队列的使用者是否可能在不确认之前的消息的情况下开始处理下一条消息?我将NodeJS与一起使用,答案是它取决于您使用的语言 在JS中,如果我们在回调中执行异步操作,它将不会被排序。 为了确保它是有序的,我们可以利用JS是单线程的这一事实来利用技巧。 下面是一个粗略的想法,您可以使用hashmap将其扩展到多个队列 beingP

我有一个专门用于消费的频道和50个队列,每个队列只有一个消费者。 该通道的预取值为5,适用于

我的应用程序需要按照在每个队列中接收消息的顺序准确地处理消息。
由于预取值较高,队列的使用者是否可能在不确认之前的消息的情况下开始处理下一条消息?我将NodeJS与

一起使用,答案是它取决于您使用的语言

在JS中,如果我们在回调中执行异步操作,它将不会被排序。 为了确保它是有序的,我们可以利用JS是单线程的这一事实来利用技巧。 下面是一个粗略的想法,您可以使用hashmap将其扩展到多个队列

beingProcessed = false;

.on("messageFromQueue", (msg) => { // sync callback
  // very important that the callback here does no async work prior to inserting into the 
  // queue to guarantee that the next message won't be pushed first
  queue.push(msg); // in memory queue
  processSerially(msg); 
})

// sync function
processSerially() {
 if(beingProcessed || queue.size() === 0) {
   return;
 }
 beingProcessed = true;
 
 doSomeTaskAsyncWithMessage(queue.pop(), () => {
   beingProcessed = false;
   // this recursive call in the callback is the reason why this works
   processSerially(); 
 })
}


答案是,这取决于你所使用的语言

在JS中,如果我们在回调中执行异步操作,它将不会被排序。 为了确保它是有序的,我们可以利用JS是单线程的这一事实来利用技巧。 下面是一个粗略的想法,您可以使用hashmap将其扩展到多个队列

beingProcessed = false;

.on("messageFromQueue", (msg) => { // sync callback
  // very important that the callback here does no async work prior to inserting into the 
  // queue to guarantee that the next message won't be pushed first
  queue.push(msg); // in memory queue
  processSerially(msg); 
})

// sync function
processSerially() {
 if(beingProcessed || queue.size() === 0) {
   return;
 }
 beingProcessed = true;
 
 doSomeTaskAsyncWithMessage(queue.pop(), () => {
   beingProcessed = false;
   // this recursive call in the callback is the reason why this works
   processSerially(); 
 })
}