Community contributed extensions

Play! Framework RabbitMQ Module

Felipe Oliveira

1) Installation

Under dependencies.yml:

- play -> rabbitmq

2) RabbitMQ

Download RabbitMQ from
Unzip the distribution, go to folder sbin and run “./rabbitmq-server”.
It should be running on port 5762 by default. The default user “guest” and password is “guest”.

3) Configuration
rabbitmq.msgmapper=json -> I am sure you can guess this one
rabbitmq.port -> I am sure you can guess this one
rabbitmq.vhost -> If you don’t know what this is I recommend reading the Admin Guide for RabbitMQ (
rabbitmq.user -> I am sure you can guess this one. guest is the default user on a fresh RabbitMQ install.
rabbitmq.password -> I am sure you can guess this one. guest is the default value on a fresh RabbitMQ install.
rabbitmq.exchangeType -> These are the different exchangeTypes provided by RabbitMQ like direct, fanout, etc. If you are not familiar with them, please checkout RabbitMQ’s doc (
rabbitmq.durable -> Durable message. You most likely want to keep that as true if your app requires reliability in your message deliveries.
rabbitmq.autoAck -> If true you are basically turning off any form of retry so you probabily don’t want that.
rabbitmq.basicQos -> Please read RabbitMQ’s documentation for more information on basicQos (
rabbitmq.retries -> Max number of retries per message. This number can be overwritten on a queue level by overriding the retries() method on your consumer class.
rabbitmq.msgmapper -> Possible values are “pojo” or “json”. These are the different implementations used to store and retrieve messages, “json” uses Jackson’s ObjectMapper and “pojo” uses Java’s serialization.

4) Define Message that will be used by the Queue (just a simple POJO)

public class SampleMessage implements Serializable {

private String field1;

private String field2;

public SampleMessage() {


public SampleMessage(String field1, String field2) {
this.field1 = field1;
this.field2 = field2;

public String getField1() {
return field1;

public void setField1(String field1) {
this.field1 = field1;

public String getField2() {
return field2;

public void setField2(String field2) {
this.field2 = field2;

public String toString() {
return “SampleMessage [field1=” + field1 + “, field2=” + field2 + “]”;


5) Publish a Message

public static void publish(SampleMessage q) {
RabbitMQPublisher.publish(“myQueue”, q);

6) Creating a Message Consumer

@OnApplicationStart(async = true)
public class RabbitMQSampleConsumer extends RabbitMQConsumer {

protected void consume(SampleMessage message) {
System.out.println(“* Message Consumed: ” + message);

protected String queue() {
return “myQueue”;

protected String routingKey() {
return this.queue();

protected int retries() {
// This is the default value defined by “rabbitmq.retries” on
// application.conf (please override if you need a new value)
return RabbitMQPlugin.retries();

protected Class getMessageType() {
return SampleMessage.class;

First of all, we avoid infiniate loops by retrying only a certain amount of times, defined by the method retries() on the consumer class.
For the cases where you don’t want to retry at all simply throw a RabbitMQNotRetriableException. The RabbitMQConsumer base class will catch it and acknowledge to RabbitMQ, instead of trying to reprocess the message.

7) Firehose – Another way to publish messages in batch

@OnApplicationStart(async = true)
public class RabbitMQSampleFirehose extends RabbitMQFirehose {

public int count = 0;

protected List getData(int n) throws Exception {
if ( count >= 10 ) {
return null;
List results = new ArrayList();
for (int i = 0; i < n; i++) {
results.add(new SampleMessage(“field1”, “field2”));
return results;

protected int batchSize() {
return 2;

protected String queueName() {
return “myQueue”;


8) Live Stats and Live Streaming

Add the module routes in your routes.conf

Live Stats and Live Streaming of messages produced and consumed are available at http://localhost:9000/rabbitmq/.
Live Stats uses Ajax to display the number of successful messages produced and consumed to a queue, errors as well.
Live Streaming uses WebSockets (requires newer browsers) to stream messages consumed and produced.

Source Code

The source code is available on Github at


May 11th 2011 – Version 0.0.5: Fixed bugs on stats and added average time to stats service, stats page for a queue and also to the WebSocket live streaming.

May 18th 2011 – Version 0.0.6: Adding routingKey to Publisher, Consumer, Firehose, RabbitMQPlugin, etc to add support for RabbitMQ Topic Exchange.

May 26th 2011 – Version 0.0.8: Adding more stats

May 28th 2011 – Version 0.0.9: Adding ability for consumers to pause.