First Steps with Rabbit MQ in Ruby 1.8.6
Until recently I was more than happy using ActiveMQ as my message broker. I had heard of RabbitMQ several times but never took the chance to look into it. A recent talk at LRUG made me decide that I had left it too long and that if I didn't start investigating soon I'd be left behind.
Here's how I got started using RabbitMQ 1.6.0 on OS X under Ruby 1.8.6.
Installation
mkdir /tmp/rabbit-mq && cd /tmp/rabbit-mq
wget http://www.rabbitmq.com/releases/rabbitmq-server/v1.6.0/rabbitmq-server-generic-unix-1.6.0.tar.gz
tar -xzvf rabbitmq-server-generic-unix-1.6.0.tar.gz
sudo mv rabbitmq_server-1.6.0/ /opt/local/lib
Running the server
sudo /opt/local/lib/rabbitmq_server-1.6.0/sbin/rabbitmq-server
Seriously, that's it.
Passing messages
When I wrote about getting started with SMQueue I created a consumer that pushed timestamps onto the queue and a consumer that printed the values from the queue to the terminal. Recreating that using the AMQP gem is simple.
First, install the AMQP gem.
gem sources -a http://gems.github.com
gem install tmm1-amqp
Open an IRB session and paste this code to create a producer:
require 'mq'
EM.run {
broker = MQ.new
EM.add_periodic_timer(1) {
broker.queue("timestamps").publish(Time.now.to_f)
}
}
Open another IRB session and paste this to create a consumer:
require 'mq'
EM.run {
broker = MQ.new
broker.queue("timestamps").subscribe { |timestamp|
time = Time.at(timestamp.to_f)
puts "Got #{timestamp} which is #{time}"
}
}
Profit. RabbitMQ is extremely easy to get started with. I don't imagine that it would take too much effort to write an adaptor for SMQueue to easily change deployed projects to use it without changing their implementation. If you do this I'd love to hear about it.
curl -LO http://barkingiguana.com/2009/08/13/first-steps-with-rabbit-mq-in-ruby-186.html.orig
curl -LO http://barkingiguana.com/2009/08/13/first-steps-with-rabbit-mq-in-ruby-186.html.orig.asc
gpg --verify first-steps-with-rabbit-mq-in-ruby-186.html.orig{.asc,}
If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.