Grails and Rabbits

by

Tim: There he is!
King Arthur: Where?
Tim: There!
King Arthur: What? Behind the rabbit?
Tim: It *is* the rabbit!
-Monty Python and the Holy Grail

This. Is. Ridiculous.

After SpringOne I was anxious to try Grails with RabbitMQ on my own.  I downloaded the complete bundle for Windows and set it up.  I’ve never run an Erlang application so I felt a bit funny, but it was simple and painless.  I set an environment variable for Erlang (ERLANG_HOME).  Then I just hard coded a ‘base’ directory for logging and whatnot in Rabbit (server.bat).  Yeah, I could have set another env var there too, but I was too anxious.

I started the server.bat file and had RabbitMQ running.  Though it was lonely.

Sir Bedevere: Well, now, uh, Lancelot, Galahad, and I, wait until nightfall, and then leap out of the rabbit, taking the French by surprise – not only by surprise, but totally unarmed!
-Monty Python and the Holy Grail

I jumped into STS and ran ‘install-plugin rabbitmq’
A simple config setting (Config.groovy):

rabbitmq {
   connectionfactory {
      username = 'guest'
      password = 'guest'
      hostname = 'localhost'
      consumers = 5
   }
   queues = {
      jimski()
   }
}

My Controller only needed to call:

rabbitSend 'jimski', msg

…and for fun I setup a Service to pull messages off the queue:

static rabbitQueue = "jimski"
void handleMessage(msg) {
   println "received message: $msg"
}

Voila! Messages taken in by the Controller make a call to rabbitSend and magically the Service sees them and pulls them out to display.  Crazy easy.  Crazy cool.

I’m going to keep at it.  Let me know your experiences with queuing.

Advertisements

3 Responses to “Grails and Rabbits”

  1. Calvin Hill Says:

    That has not been my experience with Queuing. I have setup JMS with EJB 1 and EJB 3. And although EJB 3 was much easier, it doesn’t sound as easy as what you just did. I will definitely look into RabbitMQ. Very cool!

  2. Neil Says:

    I’m very interested in checking out grails sometime. I was so awed by the power of rails i almost wish I’d never tried it. (ie anything else for “simple” projects seems wrong – it says it’s good at CRUD and it made me wonder what else an app would do besides CRUD 🙂 )

  3. eggmatters Says:

    From your blog: “Messages taken in by the Controller make a call to rabbitSend and magically the Service sees them and pulls them out to display.”
    Since grails codes by “convention”, The operative term in your service is the string: rabbitQueue. With the addition of a default “handleMessage” action, the MQ can delegate and process the message. From http://grails-plugins.github.com/grails-rabbitmq/docs/manual/guide/single.pdf it states:
    “The plugin provides a convention based mechanism for associating a listener with a Queue. Any Grails Service may
    express that it wants to receive messages on a specific Queue by defining a static property named rabbitQueue
    and assigning the property a String which represents the name of a Queue.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: