RabbitMQ JRuby Client
rabbitmq-jruby-client allows you to use RabbitMQ Client from JRuby using the official Java RabbitMQ client from Rabbit Technologies See more at www.rabbitmq.com
Getting Started
-
Install and start RabbitMQ (see below)
-
Install JRuby RabbitMQ Client: jruby -S gem install rabbitmq-jruby-client
Example Usage
gem ‘rabbitmq-jruby-client’ require ‘rabbitmq_client’
# Initializes the new client and connect to the server client = RabbitMQClient.new
# Initializes a new queue queue = client.queue(‘queue_name’)
# Initializes a new exchange exchange = client.exchange(‘exchange_name’)
# Connects queue with the exchange queue.bind(exchange)
# Publish a message to the queue queue.publish(‘message body’)
# Retrieve a message from the queue message = queue.retrieve
# Subscribe to a queue with callback. (Event-driven) queue.subscribe do |message|
# do something with message
end
# Subscribe to a queue in a loop. (Polling) queue.loop_subscribe do |message|
# do something with message
end
Installing RabbitMQ on OS X
-
Install MacPorts
-
sudo port install rabbitmq-server
To run RabbitMQ
-
sudo rabbitmq-server
Marshalling
By default standard ruby marshalling is used to serialise data but this can be overridden by using a marshalling class which responds to two method calls: 1) load takes an array of java bytes and returns whatever you desrialise from this 2) dump takes a message and must produce an array of java bytes, a RabbitMQClientError is raised if the output is not a suitable array
The marshalling class can be set in several ways:
-
By instantiating a RabbitMQClient with a option of :marshaller set to the class you wish use as your default marshaller
-
By setting the 3rd argument of RabbitMQClient#queue to the class you wish to use for this queue
-
By instantiating a RabbitMQClient::QueueConsumer with a 3rd argument of the class you wish to use
-
By using RabbitMQClient#marshaller=
-
By using RabbitMQClient::Queue#marshaller=