{"id":8708,"date":"2012-09-25T23:04:08","date_gmt":"2012-09-25T17:34:08","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8708"},"modified":"2022-01-13T16:45:03","modified_gmt":"2022-01-13T11:15:03","slug":"few-simple-steps-for-integrating-rabbit-mq-with-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/few-simple-steps-for-integrating-rabbit-mq-with-grails\/","title":{"rendered":"Few Simple steps for integrating Rabbit MQ with Grails"},"content":{"rendered":"<p>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.<\/p>\n<p>I would like to share the same in a step by step fashion.<\/p>\n<p><strong>Here are few simple steps that I followed to make my grails application use rabbitmq:<\/strong><\/p>\n<p><strong>1.Install rabbitmq : <\/strong><\/p>\n<p>Download the installable from http:\/\/www.rabbitmq.com\/download.html. Install the same on your machine.<\/p>\n<p><strong>2.Install the rabbitmq grails plugin:<\/strong><\/p>\n<p>[java] grails install-plugin rabbitmq[\/java]<\/p>\n<p><strong>3.Configure the rabbit server and a queue in the Config.groovy:<\/strong><\/p>\n<p>[java]<br \/>\n  \/\/ grails-app\/conf\/Config.groovy<br \/>\nrabbitmq {<br \/>\n    connectionfactory {<br \/>\n        username = &#8216;guest&#8217;<br \/>\n        password = &#8216;guest&#8217;<br \/>\n        hostname = &#8216;localhost&#8217;<br \/>\n        consumers = 5<br \/>\n    }<\/p>\n<p>    retryPolicy.maxAttempts = 3<\/p>\n<p>    queues = {<br \/>\n        myTestQueue autoDelete: false, durable: true, exclusive: false<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>4.Write a method to populate messages in the queue: <\/strong><br \/>\nrabbitmq plugin injects a rabbitSend() method in all the services and controllers<\/p>\n<p>[java]<br \/>\n\/\/This method could be defined in any service.<br \/>\npopulateQueue(){<br \/>\n  rabbitSend(&#8216;myTestQueue&#8217;, &quot;some id:1,some action: do this&quot;)<br \/>\n  \/\/ Message has to be a String<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>5.Write a handler Service which shall handle the messages that are put in the queue:<\/strong><\/p>\n<p>[java]<br \/>\nclass QueueBasedMyTestService {<\/p>\n<p>    static rabbitQueue = &#8216;myTestQueue&#8217;<br \/>\n    def solrSearchService<\/p>\n<p>    \/\/ This method gets called by the plugin whenever there is any new message in the queue.<br \/>\n    \/\/ We can configure n number of parallel handlers<br \/>\n    void handleMessage(message){<br \/>\n            log.info(&quot;Message Received ${message}&quot;)<br \/>\n            \/\/ We have recieved the message from the rabbitmq server&#8217;s queue<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Hope this helps!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":13},"categories":[7],"tags":[1078,4840,186,1079,1077],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8708"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8708"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8708\/revisions"}],"predecessor-version":[{"id":54653,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8708\/revisions\/54653"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}