Reputation: 495
I'm new to RabbitMQ. What I want is to execute only one item at a time, the next one will be consumed after the previous one is finished.
Here is how Consumer is created:
consumer.assertQueue(queue, { durable: true });
consumer.consume(queue, handleMessage, { noAck: false });
Here is function handleMessage:
async function handleMessage(message) {
console.log('handleMessage', message.content.toString());
// wait for 1 second
await wait(1000);
console.log('ACK', message.content.toString());
consumer.ack(message);
}
The Publisher will send items like this:
publisher.sendToQueue(queue, Buffer.from('1'));
publisher.sendToQueue(queue, Buffer.from('2'));
Here is the log. It seem does not wait for the handle process to wait 1 second to finished:
handleMessage 1
handleMessage 2
ACK 1
ACK 2
What I want is:
handleMessage 1
ACK 1
handleMessage 2
ACK 2
Mean message 1 must be finished before it handle message 2. How can I do it? Thanks.
Upvotes: 0
Views: 1438
Reputation: 1525
If you have a single queue with a single consumer, you can set the prefetch to 1.
channel.prefetch(1)
You can get more detail from RabbitMQ 's official doc:
The value defines the max number of unacknowledged deliveries that are permitted on a channel. Once the number reaches the configured count, RabbitMQ will stop delivering more messages on the channel unless at least one of the outstanding ones is acknowledged
Upvotes: 1