RabbitMQ Acknowledgements

Exclusive offer: get 50% off this eBook here
Instant RabbitMQ Messaging Application Development How-to [Instant]

Instant RabbitMQ Messaging Application Development How-to [Instant] — Save 50%

Build scalable message-based applications with RabbitMQ with this book and ebook

£8.99    £4.50
by Andrew Keig | September 2013 | Open Source

 

2.The article, RabbitMQ Acknowledgements, gives you a glimpse of RabbitMQ. This article by Andrew Keig, author of the book Instant RabbitMQ Messaging Application Development How-to, explains the one of the important features, Acknowledgements, in RabbitMQ that helps in reliable message delivery.

(For more resources related to this topic, see here.)

Acknowledgements (Intermediate)

This task will examine reliable message delivery from the RabbitMQ server to a consumer.

Getting ready

If a consumer takes a message/order from our queue and the consumer dies, our unprocessed message/order will die with it.

In order to make sure a message is never lost, RabbitMQ supports message acknowledgments or acks. When a consumer has received and processed a message, an acknowledgment is sent to RabbitMQ from the consumer informing RabbitMQ that it is free to delete the message. If a consumer dies without sending an acknowledgment, RabbitMQ will redeliver it to another consumer.

How to do it...

Let's navigate to our source code examples folder and locate the folder Message-Acknowledgement. Take a look at the consumer.js script and examine the changes we have made to support acks.

  1. We pass to the {ack:true} option to the q.subscribe function, which tells the queue that messages should be acknowledged before being removed:

    q.subscribe({ack:true}, function(message) {

  2. When our message has been processed we call q.shift, which informs RabbitMQ that the message has been processed, and it can now be removed from the queue:

    q.shift();

    You can also use the prefetchCount option to increase the window of how many messages the server will send you before you need to send an acknowledgement. {ack:true, prefetchCount:1} is the default and will only send you one message before you acknowledge. Setting prefetchCount to 0 will make that window unlimited. A low value will impact performance, so it may be worth considering a higher value.

Let's demonstrate this concept.

  1. Edit the consumer.js script located in the folder Message-Acknowledgement. Simply comment out the line q.shift(), which will stop the consumer from acknowledging the messages.
  2. Open a command-line console and start RabbitMQ:

    rabbitmq-server

  3. Now open a command-line console, navigate to our source code examples folder, and locate the folder Message-Acknowledgement. Execute the following command:

    Message-Acknowledgement> node producer

    Let the producer create several message/orders; press Ctrl + C while on the command-line console to stop the producer creating orders. Now execute the following to begin consuming messages:

    Message-Acknowledgement> node consumer

  4. Let's open another command-line console and run list_queues:

    rabbitmqctl list_queues messages_ready
    messages_unacknowledged

    The response should display our shop queue; details include the name, the number of messages ready to be processed, and one message which has not been acknowledged.

    Listing queues ...
    shop.queue 9 1
    ...done.

  5. If you press Ctrl + C while on the command-line console, the consumer script is stopped, and then list the queues again you will notice the message has returned to the queue.

    Listing queues ...
    shop.queue 10 0
    ...done.

If you edit the change we made to consumer.js script and re-run these steps, the application will work correctly, consuming messages one at a time and sending an acknowledgment to RabbitMQ when each message has been processed.

Summary

This article explained a reliable message delivery process in RabbitMQ using Acknowledgements. It also listed the steps that will give you acknowledegements for a messaging application using scripts in RabbitMQ.

Resources for Article :


Further resources on this subject:


Instant RabbitMQ Messaging Application Development How-to [Instant] Build scalable message-based applications with RabbitMQ with this book and ebook
Published: May 2013
eBook Price: £8.99
See more
Select your format and quantity:

About the Author :


Andrew Keig

Andrew Keig is a London based web developer who has been building web applications since 2000. He is the author of Packt's Instant RabbitMQ Messaging Application Development How-to. Andrew has a degree in Computing and blogs at blog.airasoul.net on topics he is passionate about, such as Node.js, REST, Web APIs, and behaviour-driven development. He also contributes to various Node.js open source projects. He is a director at Airasoul, which specializes in the design and build of scalable, RESTful, specification-driven, and real-time web-based applications on the Node.js stack. He is also the co-founder of openue.com, a property search startup.

Books From Packt


Instant Apache ActiveMQ Messaging Application Development How-to [Instant]
Instant Apache ActiveMQ Messaging Application Development How-to [Instant]

Managing Software Development with Trac and Subversion
Managing Software Development with Trac and Subversion

Drush User’s Guide
Drush User’s Guide

Django 1.2 E-commerce
Django 1.2 E-commerce

Django 1.0 Website Development
Django 1.0 Website Development

Django JavaScript Integration: AJAX and jQuery
Django JavaScript Integration: AJAX and jQuery

ZeroMQ
ZeroMQ

Django 1.0 Template Development
Django 1.0 Template Development


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software