RabbitMQ Module for Play! Framework

RabbitMQ offers a highly available, scalable and portable messaging system with predictable and consistent throughput and latency. RabbitMQ is the leading implementation of AMQP, the open standard for business messaging, and, through adapters, supports XMPP, SMTP, STOMP and HTTP for lightweight web messaging. This new module allows you to consume and produce messages on a RabbitMQ instance from your Play! Framework application.

Installation

Add to your conf/dependencies.yml the following:

require: - play -> rabbitmq 0.0.5

Sample Configuration

rabbitmq.host=localhost rabbitmq.port=5672 rabbitmq.vhost=/ rabbitmq.user=guest rabbitmq.password=guest rabbitmq.exchangeType=direct rabbitmq.durable=true rabbitmq.autoAck=false rabbitmq.basicQos=true rabbitmq.retries=5 rabbitmq.msgmapper=json

Configuration Details

Here’s what each configuration parameter means:

  • rabbitmq.host -> 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 (http://www.rabbitmq.com/admin-guide.html)
  • 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 (http://www.rabbitmq.com/getstarted.html).
  • 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 (https://dev.rabbitmq.com/wiki/BasicQosDesign?version=15b1bab0ac73).
  • 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.

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

public class SampleMessage implements Serializable { /* The field1. */ private String field1; /* The field2. / private String field2; /* * Instantiates a new sample message. / public SampleMessage() { } /* * Instantiates a new sample message. * * @param field1 the field1 * @param field2 the field2 / public SampleMessage(String field1, String field2) { super(); this.field1 = field1; this.field2 = field2; } /* * Gets the field1. * * @return the field1 / public String getField1() { return field1; } /* * Sets the field1. * * @param field1 the new field1 / public void setField1(String field1) { this.field1 = field1; } /* * Gets the field2. * * @return the field2 / public String getField2() { return field2; } /* * Sets the field2. * * @param field2 the new field2 / public void setField2(String field2) { this.field2 = field2; } /* * To String * * @see java.lang.Object#toString() */ @Override public String toString() { return "SampleMessage [field1=" + field1 + ", field2=" + field2 + "]"; } }

Publish a Message

public static void publish(String q) { RabbitMQPublisher.publish("myQueue", new SampleMessage(q, q)); render(q); }

Creating a Message Consumer

@OnApplicationStart(async=true) public class RabbitMQSampleConsumer extends RabbitMQConsumer { /* * Consume Message * * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#consume(T) */ @Override protected void consume(SampleMessage message) { System.out.println("********"); System.out.println(" Message Consumed: " + message); System.out.println("*********"); } /* * Name of the Queue that this consumer will be listening to. * * @return the string * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#queue() / @Override protected String queue() { return "myQueue"; } /* * Number of times we'll try to re-deliver the message if any exception * happens * * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#retries() / @Override 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(); } /* * Return message type. * * @return the message type * @see play.modules.rabbitmq.consumer.RabbitMQConsumer#getMessageType() */ protected Class getMessageType() { return SampleMessage.class; } }

* Please note this is a Play! job so you can start it manualy or you can use the other annotations provided by Play! like @On or @Every. More information available athttp://www.playframework.org/documentation/1.2/jobs.

* There are some cases where you don’t want to retry re-processing the message, for example in cases you know you will keep getting the same error no matter how many times you retry that message.*

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.

Firehose – Another way to publish messages in batch

@OnApplicationStart(async = true) public class RabbitMQSampleFirehose extends RabbitMQFirehose { /* The count. */ public int count = 0; /* * Get data to be loaded. * * @param n the n * @return the data * @throws Exception the exception * @see play.modules.rabbitmq.producer.RabbitMQFirehose#getData(int) / @Override 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")); count++; } return results; } /* * Batch Size - How many records we will select at the time?. * * @return the int * @see play.modules.rabbitmq.producer.RabbitMQFirehose#batchSize() / @Override protected int batchSize() { return 2; } /* * Queue Name. * * @return the string * @see play.modules.rabbitmq.producer.RabbitMQFirehose#queueName() */ @Override protected String queueName() { return "myQueue"; } }

* Please note this is a Play! job so you can start it manualy or you can use the other annotations provided by Play! like @On or @Every. More information available at http://www.playframework.org/documentation/1.2/jobs.

Of course the code is available on Github.

Now Go Play!