This task will look at a RabbitMQ extension to AMQP called Publisher Confirms as a way of doing asynchronous transactions.
We mentioned in the previous task, Persistent messages (Intermediate), that there is a short window in which the failure of a RabbitMQ server would result in a message being lost.
The only way to truly guarantee that a message isn't lost is to use AMQP transactions. Since AMQP transactions are synchronous (they block IO) and decrease throughput, we will use Publisher Confirms which is 100 times faster than AMQP transactions. There is more information on the RabbitMQ website (http://www.rabbitmq.com/confirms.html) as to why this extension was created.
Let's navigate to our source code examples folder and locate the folder Publisher-Confirms
. Take a look at the producer.js
script and examine the changes made to support Publisher Confirms.
connect.exchange
function the following options, which tells the connection the channel should be in the confirm mode confirm:true
:var ex = connect.exchange('shop.exchange', {type: 'direct', confirm:true});
ex.publish('order.key', newOrder, {deliveryMode:2}, function(isError){ if (isError) console.log('ERROR, Order has not been acknowledged.'), });
Let's demonstrate this concept.
rabbitmq-server
Publisher-Confirms> node producer
Messages that RabbitMQ fails to queue will be displayed, although an error will need to be induced in order to see this. Our code will simulate an error on the tenth message, and you should see this message:
ERROR, Order has not been acknowledged.
3.143.17.27