Few Simple steps for integrating Rabbit MQ with Grails

25 / Sep / 2012 by Mohd Farid 4 comments

Rabbit MQ as defined by rabbitmq.com is a robust messaging for applications. We used RabbitMQ to achieve asynchronous communication between two parts of the application.

I would like to share the same in a step by step fashion.

Here are few simple steps that I followed to make my grails application use rabbitmq:

1.Install rabbitmq :

Download the installable from http://www.rabbitmq.com/download.html. Install the same on your machine.

2.Install the rabbitmq grails plugin:

 grails install-plugin rabbitmq


3.Configure the rabbit server and a queue in the Config.groovy:

  // grails-app/conf/Config.groovy
rabbitmq {
    connectionfactory {
        username = 'guest'
        password = 'guest'
        hostname = 'localhost'
        consumers = 5
    }

    retryPolicy.maxAttempts = 3

    queues = {
        myTestQueue autoDelete: false, durable: true, exclusive: false
    }
}


4.Write a method to populate messages in the queue:
rabbitmq plugin injects a rabbitSend() method in all the services and controllers

//This method could be defined in any service.
populateQueue(){
  rabbitSend('myTestQueue', "some id:1,some action: do this")
  // Message has to be a String
}


5.Write a handler Service which shall handle the messages that are put in the queue:

class QueueBasedMyTestService {

    static rabbitQueue = 'myTestQueue'
    def solrSearchService

    // This method gets called by the plugin whenever there is any new message in the queue.
    // We can configure n number of parallel handlers
    void handleMessage(message){
            log.info("Message Received ${message}")
            // We have recieved the message from the rabbitmq server's queue
    }
}


Hope this helps!!!

FOUND THIS USEFUL? SHARE IT

comments (4)

  1. Pingback: Sending Scheduled/delayed messages with RabbitMQ through java client | TO THE NEW Blog

Leave a comment -