Friday, 23 September 2011

RabbitMQ

RabbitMQ

Here’s a simple example of how to use RabbitMQ to provide a simple pub/sub messaging architecture (aka observer/listener pattern).

Pub/Sub example

Once the RabbitMQ server is installed we are going to create a C# client to:

  • Create a queue
  • Send 10 messages to the queue
  • Read the messages back using two different techniques (5 messages each)

The code also shows how to inspect for the last message retrieved from a subscription.

The Output

ScreenShot134

Code

 

RabbitMQ Server Installation

For the server you’ll need to first install Erlang and then run the RabbitMQ installer:

http://www.erlang.org/download.html  (Windows Binary File)

http://www.rabbitmq.com/server.html  (Installer for Windows systems)

RabbitMQ Client Libraries

For the C# client libraries:

http://www.rabbitmq.com/dotnet.html

Why would you use a queue?

Queues allow messages containing work items or events to be stored in a durable manner. This enables disconnected middleware processes to be able to read from the queue, allowing for scalable processing via multiple processes reading from the queue. By contrast, sockets and other network protocols assume that direct connections always exist.

Different message types can be used on the same queue to provide a workflow scenario. Different processes can monitor the same queue for specific message types and post back to the same queue with a message type for the next stage in the workflow, which in turn gets picked up by another process and so it goes on.

For example, we might have a Web Server dealing with HTTP traffic and 2 worker processes all reading from and writing to the same queue.

  • A web server receives a request from a user to for an order and places an OrderReceived message on queue
  • Worker 1, which had subscribed to to this message type reads the message and “confirms the order” on the back end systems.  Once completed it writes OrderConfirmed message type back to the queue, which is picked up by worker 2
  • Worker 2, which had subscribed to the OrderConfirmed message type, completes its processing and writes an OrderDespatched message type back to the queue.

In reality, “Worker X” might be a bank of multiple computers and processes to allow for a high throughput of messages to be actioned.

A nice webcast can be found here by Clemens Vasters:

http://blogs.msdn.com/b/clemensv/archive/2011/03/18/why-would-anyone-ever-use-a-message-queue.aspx

Source Code

You can download the source code for this example:

http://stevenhollidge.com/blog-source-code/RabbitMqDemo.zip

No comments:

Post a Comment