Sunday, October 31, 2010

Using StormMQs Messaging Cloud with Ruby

A little while back I was asked by Ross Cooney of StormMQ to write a Ruby client for their enterprise class managed message queueing service. It was a fun little project. The client code is open source and is hosted on GitHub and we've also published it as a gem for easy installation. What follows is a tutorial explaining how to get up and running with StormMQ's service using the Ruby client.

The first thing you'll need is an account with StormMQ, so head on over to their site and register. Registration is free, non-binding and gives you access to the free cloud based message queue service.

Next you'll need to install the Ruby client. The easiest way to do this is via the gem. Open a command line and type:


gem install stormmq-client


This will install the client and its dependencies on your system.

For this tutorial we're going to write some simple code to publish a test message to a queue and then read it back. Since message queues are most often used to build loosely coupled systems that communicate asynchronously via one of more queues, we'll separate the publisher and consumer of the test message by writing each as an independent script.

Let's start with the publisher. Launch your favourite text editor, or IDE, paste in the following code and save it as 'publish.rb':


#!/usr/bin/env ruby

# Before we can call the client we need to require it!
require 'rubygems'
require 'stormmq/amqp'

# For convenience we'll create a hash of the required connection options.
client_options = {
:user => 'my_username',
:pass => 'a_very_looooooong_password',
:company => 'my_username',
:system => 'my_username',
:environment => 'development'
}

# Connect to the AMQP service using the options configured above.
# Note, the run() method creates the connection and executes
# the code in the block. When the block terminates, the connection
# is closed.
StormMQ::AMQPClient.run(client_options) do |client|

# Declare a queue on the exchange
queue = client.queue('test1')

# Publish (submit) our test message to the new queue.
queue.publish('Hello World!')
end


The above code will open a connection to StormMQ's service, using your account details (you did use your account details, right?). It will then declare a queue called 'test1', publish the traditional 'Hello World' to the queue and close the connection.

Run it from the command line with

ruby publish.rb


That's all well and good, but let's see if we can re-connect to the queue and read the message from it. Returning to the text editor or IDE, open a new file and paste in the following:


#!/usr/bin/env ruby

require 'rubygems'
require 'stormmq/amqp'

# Use the same settings as before
client_options = {
:user => 'my_username',
:pass => 'a_very_looooooong_password',
:company => 'my_username',
:system => 'my_username',
:environment => 'development'
}

StormMQ::AMQPClient.run(client_options) do |client|
queue = client.queue('test1')

# Read the message from the queue and display it.
puts queue.pop[:payload]
end


Save the new file as 'subscribe.rb' and run it as follows:


ruby subscribe.rb


All going well, this should print 'Hello World!' on screen. Congratulations! You've just written your first code to talk to an enterprise class message queuing system.

The Ruby client is actually a thin wrapper around the Bunny library, which is an AMQP compliant ruby client. My contribution provides the extra bits needed to support StormMQ's virtual host configuration. My client is also a REST client for StormMQ's configuration system to allow customers to manage their systems. We'll take a look at the REST component of the client in a future blog post.

No comments: