The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Monday, 21 September 2009

Ruby ActiveMQ and Stomp

Apache ActiveMQ can be used as messaging system with Ruby. The easy way to use it is with the Stomp protocol.

To use tis example you need to use the Stomp gem:

gem install stomp

The following example uses the producer/consumer paridigm. Create and run a consumer script:

require "rubygems"
require "stomp"

client = "stomp://localhost:61613"
# client.send('/queue/testing', "hello world")
# Processing loop
client.subscribe('/queue/testing') do |msg|
puts msg
client.join # Wait until listening thread dies

The code above waits for incoming messages, tehn prints the message. Write the producer script:

require "rubygems"
require "stomp"
# connect to ActiveMQ
client = "stomp://localhost:61613"
# send a message to '/queue/testing'
client.send('/queue/testing', "hasta la vista!!")

From this example you can see how easy is to communicate between applications and servers.

The Stomp protocol is an HTTP like protocol to wirte clients to access other protocol or technologies. Here Stomp is used to connect to ActiveMQ, which is a JMS compliant server.

A better way to use MOM services in Ruby is to use the ActiveMessaging gem. You can find an article in InfoQ: Introduction to ActiveMessaging for Rails.

Labels: ,