Using RabbitMQ and AMQP with Ruby

AMQP stands for Advanced Message Queuing Protocol. RabbitMQ is a server that implements the protocol. RabbitMQ is available on Linux, OSX, and Windows. Installation instructions for RabbitMQ are available at http://www.rabbitmq.com/install.html

I will be running RabbitMQ on Ubuntu 9.10. First, let’s install the server:

Next, we install the amqp gem by Aman Gupta. The gem can also be found at http://github.com/tmm1/amqp

Let’s now build a simple publisher code. Note the difference in the gem’s name and the file you need to use the gem.

All queues are created automatically the first time it is accessed. Make sure this is the same queue our consumers will use.

Next, the consumer code:

The ‘subscribe’ method registers with the queue telling it to call the block when a message has arrived. Alternatively, you can use ‘pop’ but this would constantly poll the server for new messages making unnecessary calls even if the queue is empty.

If you want to run AMQP on several machines, just specify the location of the broker in your publisher and consumer code:

You only need to run 1 rabbitmq server, which in my case is on Ubuntu. When you try the code in other machines, you only need the amqp gem installed.

I highly recommend Distributed Programming with Ruby by Mark Bates if you’re interested in distributed programming.

About these ads
Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: