Publisher Confirms (Intermediate)

This task will look at a RabbitMQ extension to AMQP called Publisher Confirms as a way of doing asynchronous transactions.

Getting ready

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.

How to do it...

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.

  1. We pass to the 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});
  2. When we publish a message, we pass a callback as the last argument. It is here we are able to handle messages that RabbitMQ failed to add to a queue.
    ex.publish('order.key', newOrder, {deliveryMode:2},    
     function(isError){
      if (isError)
      console.log('ERROR, Order has not been acknowledged.'),
     });

Let's demonstrate this concept.

  1. Open a command-line console and start RabbitMQ:
    rabbitmq-server
    
  2. Now open a command-line console, navigate to our source code examples folder, and locate the folder Publisher-Confirms. Execute the following command:
    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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.17.27