Making Active MQ work with Grails 1.2 M2

Posted by Rob James | Posted in Software, Start Ups | Posted on 21-08-2009-05-2008

1

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.

Grails 1.2 M2 is out!

Posted by Rob James | Posted in General News, Software | Posted on 12-08-2009-05-2008

0

For those of you that know me, you will know that I am a big fan of Grails and Groovy. This week we have had 2 huge announcements. Firstly that VMWare has acquired SpringSource (Groovy and Grails custodian), and then Grails 1.2 M2 got released yesterday.

I downloaded 1.2 M2 last night and I am super keen to start using some of the new features! Find out about them here. Or download it now!

I am totally in love with the Named Queries which allows you to create pre-defined queries (with a name) in the domain class that you can consequently re-use in the application. Example from the release notes;

class Publication {
   String title
   Date datePublished
   static namedQueries = {
       recentPublications {
           def now = new Date()
           gt 'datePublished', now - 365
       }

       publicationsWithBookInTitle {
           like 'title', '%Book%'
       }

   }

}

You can do
// get all recent publications…
def recentPubs = Publication.recentPublications()
// get all recent publications (alternate syntax)…
def recentPubs = Publication.recentPublications.list()

// get up to 10 recent publications, skip the first 5…
def recentPubs = Publication.recentPublications(max: 10, offset: 5)
def recentPubs = Publication.recentPublications.list(max: 10, offset: 5)

// get the number of recent publications…
def numberOfRecentPubs = Publication.recentPublications.count()

// get a recent publication with a specific id…
def pub = Publication.recentPublications.get(42)

The other exciting changes is that tomcat is now the default container for building apps in, but this is accomplished through the plugin architecture, so it is just as easy to uninstall Tomcat and re-install Jetty again. Brilliant!!

Another win for me is the pre-compilation of GSPs in the WAR deployment. I host many of my grails apps on Virtual Private Servers, which usually have limited resource capabilities. So i find they constantly fall over at startup. This should certainly help that cause.

Other features worth a mention include;

  • Named URL Mappings
  • Support for hasOne mapping in the domain classes
  • Implementation of Spring 3 into the codebase and therefore support for all Spring 3 annotations

So it is time to get programming!!!

UPDATE: Just came across this one too. As of 1.2 Grails now creates your IntelliJ Project files too!! As a user of IDEA, this is just awesome.