[rabbitmq-discuss] New RabbitMQ Groovy DSL

Jon Brisbin jon.brisbin at npcinternational.com
Wed Mar 31 21:49:57 BST 2010


Folks,

"I'm fixin' to" (as we say here in the Midwest) upload some recent work I've done on a Groovy-language DSL for RabbitMQ that lets me integrate with our existing Groovy REST/MVC framework. A simple example looks like:

mq.on error: {err ->
  log.error(err.message)
}, myevent: {msg ->
  log.info(msg.bodyAsString)
}

// Initialization
mq {channel ->
  //channel.queueDelete("test")
  //channel.exchangeDelete("test")
}

mq.exchange(name: "test") {
  // Named, non-durable queue
  queue name: "test", routingKey: "test.key", {
    consume tag: "test", onmessage: "myevent"
  }
  // Anonymous (server-generated) non-durable queue
  queue name: null, routingKey: "test2.key", {
    consume tag: "test2", onmessage: {msg ->
      log.info(msg.bodyAsString)
      log.info("myHeaderValue=" + msg.properties.headers["myHeaderValue"])
    }
  }
  // Poke some messages
  queue routingKey: "test.key", {
    publish body: "this is a test"
  }
  queue routingKey: "test2.key", {
    publish myHeaderValue: "customHeader", body: {msg, out ->
      msg.properties.contentType = "text/plain"
      out.write("test are test bytes".bytes)
    }
  }
}


To test it, I've got a command-line utility that takes RabbitMQ connection parameters and a Groovy DSL file to operate on (it also handles stdin/stdout for piping).

Basics of the system include:

* Direct access to the channel when you need custom stuff.
* A very basic event system for dispatch error and custom application events.
* Using Groovy, I can get a lot of stuff done quickly (works great for utilities, etc...).
* On a "consume" closure, if you return a non-null/true result, it will keep listening for more messages (uses a QueuingConsumer). 
* On a "publish" node, if you specify a closure for the body, it will call() that closure, passing an output stream for you to do custom byte processing, otherwise it'll try and fit whatever you tell it into the message body (strings automagically converted, etc...).

There's more to this. This is just a simplistic example. I'll try and document some of that on my github account.

I'd be interested in some feedback. I'm wondering what might make this useful to more people than just me. I do make some assumptions about how I'm interacting with RabbitMQ because that's just how we're doing it. I'd be interested in hearing if that doesn't hold for others.

I'll post the link when I've got everything put into Git.

Jon Brisbin
Portal Webmaster
NPC International, Inc.







More information about the rabbitmq-discuss mailing list