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

play install rabbitmq

Configuration

module.rabbitmq=${play.path}/modules/rabbitmq-0.0.1
rabbitmq.host=localhost
rabbitmq.port=5672
rabbitmq.userName=guest
rabbitmq.password=guest
rabbitmq.vhost=/
rabbitmq.exchangeType=direct
rabbitmq.durable=true
rabbitmq.autoAck=false
rabbitmq.basicQos=true

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";
    }

    /**
     * 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 at Asynchronous Jobs documentation.

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 Asynchronous Jobs documentation. Of course the code is available on Github.

Now Go Play!

Reference : RabbitMQ Module for Play! Framework from our JCG partner Felipe Oliveira at Geeks Are Totally In.

Related Articles:

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


three × = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books