Until recently I was perfectly happy using ActiveMQ as my message broker. I had heard of RabbitMQ several times but never got around to investigating it. Then a talk at LRUG convinced me I had left it too long — if I didn't start soon, I would be left behind.
Here is how I got started with 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 is it.
Passing Messages
When I wrote about getting started with SMQueue, I created a producer that pushed timestamps onto a queue and a consumer that printed them to the terminal. Recreating that with the AMQP gem is straightforward.
First, install the AMQP gem:
gem sources -a http://gems.github.com
gem install tmm1-amqp
Open an IRB session and paste this 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}"
}
}
That is all there is to it. RabbitMQ is extremely easy to get started with. I suspect it would not take much effort to write an SMQueue adapter for it, letting deployed projects switch message brokers without changing their code. If you end up building one, I would love to hear about it.