Message Driven POGOs: When Groovy, Spring and OpenMQ Collide (Glen Smith)
Glen Smith, Grails August 14th, 2008
Glen Smith delivers the second half of his ‘concoction’ of JMS/Grails/OpenMQ. If you remember the first time he described how the Grails JMS Plugin can be used to post messages to a queue from a service. The other half of the solution involves Message Driven POGOs (no, not the jumping sticks, but Groovy beans).
When we last left this story, I was talking about integrating Grails and JMS so that groovyblogs can put all of its feed and thumbnail fetch requests on a JMS queue. Now it’s time to look at the second half of the story. How my standalone groovy feed/thumbnail fetcher can pick requests off the queue, and put a response back on a different queue.
First a little background. Spring makes writing message driven POJOs really tidy. Groovy makes it even tidier. To implement a POGO (Plain Old Groovy Object) that listens on given Queue, I just have to implement the
MessageListenerinterface, and provide anonMessageroutine. Spring will setup the necessary background thread poller that will invoke my bean when a new message arrives on the queue. It will also look after the connection pooling to my JMS server so I’m not creating/dissolving connections every time. Nice.But if I want to put something on the queue? Then it’s time to use Spring’s
JmsTemplateclass which provides the necessaryconvertAndSendthat I’ll need to put my Map object back on the queue with my thumbnail result.
Due to the fact that Grails sits atop the Spring framework, Message Driven POGOs are trivial to implement, almost criminal as some say. As Glen describes the steps are simple:
- have your MD POGO implemente javax.jms.MessageListener
- add a jmsTemplate property, it will be injected by Spring
- call jsmTemplate.convertAndSave() when you want to post something to the queue
- don’t forget to setup the required bean mappings on your application context
Of this steps perhaps the most verbose is the setup of the application context (Glen went with the XML version) but perhaps Grails’ Spring BeanBuilder can help in reducing the angle bracket noise, don’t you think?
Tags: Glen Smith, Grails
About
Leave a Comment