Some great work has come out of the plugins available for Grails. One of my favorite is the Active MQ (and JMS) plugins that allow grails (very easily) to integrate Java message. I have used these before, but then come up against an issue when upgrading my projects to Grails 1.2 M2. They stopped working!!!

So with some help from @domix (behind Active MQ plugin), and @ldaley (behind JMS plugin), I managed to get this working. Essentially there were two issues;

  • the bean name had changed from “connectionFactory” to “jmsConnectionFactory”
  • The static variable to declare in your consuming service is “exposes” not “expose” as in the examples

In the spirit of making this a more complete picture, I thought I would post a quick tutorial on how I made this work;

1. Start a new project – I called mine “ActiveMQTest”

grails create-app ActiveMQTest
cd ActiveMQTest

2. Install the plugins

grails install-plugin activemq
grails install plugin jms

IMPORTANT: Make sure you get the latest plugins. Particularly 0.1 of the ActiveMQ plugin (@domix applied the fixes to this to make it work for Grails 1.2)

3. Create a controller to test the messaging service

grails create-controller notifier

open up this controller (NotifierController) and paste the following code in

class NotifierController {

     def index = {
          def message = [
               date:new Date().toString(),
               mess:"I just got posted from the Notifier",
               count:1
          ]
          sendJMSMessage("queue.notify", message)
          render "My message was sent on " + new Date()
          }
     }

4. Now create a service class that will act as your message consumer

grails create-service consumer

and paste the following code in the file (ConsumerService)

     class ConsumerService {
          boolean transactional = false
          static exposes = ['jms']
          static destination = "queue.notify"

          def onMessage = {
               println "I got the message '${it.mess}', on ${it.date}, with count = ${it.count}"
          }
     }

5. Run the app and click on the Notifier controller link

You should see the browser render something like;

My message was sent on Fri Aug 21 14:54:01 EST 2009

And almost instantaneously, your console will render something like this

I got the message 'I just got posted from the Notifier', on Fri Aug 21 14:54:01 EST 2009, with count = 1

So What Happened?

This is basically demonstrating that when the controller is called, it creates an object called ‘message’. It then populates it with some primitives (this object can only handle primitives), and by using the sendJMSMessage() method, it queues the message onto a queue called “queue.notify”.

Just to show that this didn’t throw an error, the web pages is rendered with a notification that the process completed.

At this stage, the messages is queued onto the queue “queue.notify” which is declared by the static variable “destination” in our service. The static variable is provided the name of the queue;

static destination = "queue.notify"

The other thing is to tell JMS that this service needs to be exposed as a JMS Consumable service, and you do that by declaring the static variable ;

static exposes = ['jms']

That’s it! All you have to do is create a closure called onMessage, which is what JMS is calling when instatiating the service, and passed the message into it. In this instance we are just printing out the details of what is passed in the message.