QueueingRabbit provides a Ruby DSL to interact with RabbitMQ. It is fairly flexible and allows you to integrate with existing infrastructure and naming conventions. It currently offers gems bunny and amqp as supported back-ends.
I built this gem at Wildbit in 2012. Back then there weren’t any solid generic queueing systems targeting RabbitMQ for Ruby. The gem was open-sourced in early 2013, but I never put any effort into selling it to the community. Even though the gem is working, maintained, and is still used by Wildbit in production, in the long run you should be better with now-existing mainstream alternatives like hutch and sneakers.
The following Ruby program publishes an excerpt of Joseph Brodsky’s poem line by line to a RabbitMQ exchange and prints received messages on the screen.
require 'queueing_rabbit'
class Reciter < QueueingRabbit::AbstractJob
def perform
puts payload
end
end
worker = QueueingRabbit::Worker.new(Reciter)
poem = <<-
I said fate plays a game without a score,
and who needs fish if you've got caviar?
The triumph of the Gothic style would come to pass
and turn you on - no need for coke, or grass.
I sit by the window. Outside, an aspen.
When I loved, I loved deeply. It wasn't often.
Thread.new {
poem.each_line { |l| Reciter.enqueue(l) }
sleep 5
worker.stop
}
worker.work!
This code has following important side effects:
- A Rabbit queue named
Reciter
is created with default options (if not exists). - 6 messages are published to the default exchange with routing key
Reciter
. - 6 messages are consumed from the
Reciter
queue. - 6 lines of the poem are printed to STDOUT.
Bunny
is a pseudo-synchronous RabbitMQ client. Amqp
is EventMachine-based and heavily asynchronous (lots of callbacks involved). Both clients are in active development, thoroughly documented and fairly stable.
Choose bunny
if you don’t want to worry about blocking I/O and EventMachine-compilant drivers. Choose amqp
if you’re familiar with EventMachine, designing a lightweight app from scratch and performance is a serious concern. Obviously there are exceptions, and no one knows your requirements better than you.
Also, you can use both of them. For example, you may decide to publish via bunny
from your Rails app and use amqp
in your background worker.
Check out the project wiki for additional guidance. If you have questions or something doesn’t work for you, feel free to file issues.
QueueingRabbit supports MRI Ruby version 1.9.3 and above. It is still compilant with Ruby 1.8.7, but some features may not work as expected and the compatibility will be removed in the near future.
Add this line to your application's Gemfile
:
gem 'queueing_rabbit'
And then execute:
$ bundle
Or install it globally as:
$ gem install queueing_rabbit
- Wildbit — for letting me open source this library initially developed for the internal use.
- RabbitMQ client libraries for Ruby — for providing outstanding well-documented gems that made this project possible.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request