Grails Service, JMS and an MDB
April 18, 2008 | 1:01 pmAfter I sort of set a challenge when thinking about Convention over Configuration, I had to see if it was in fact doable in a framework that exploits the principals.
This was straight from the grails site for the JMS Plugin, but in less than 30 minutes I have an exposed message driven bean receiving a JMS message and that includes downloading the plug-in and Apache ActiveMQ.
As I say, I had to go to download the plugin since, due to what I think is a proxy issue, I couldn’t directly use grails install-plugin jms
Though the following worked fine enough
grails install-plugin C:\Local\downloads\grails-jms-0.3.zip
I needed to copy over three libraries from the ActiveMQ distribution and add a little bit of Spring
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "connectionFactory" class = "org.apache.activemq.ActiveMQConnectionFactory">
<property name = "brokerURL" value = "tcp://localhost:61616"/>
</bean>
</beans>
I then added the service (much like the xfire one) to receive messages (the message driven bean).
class SampleQueueService {
static expose = ['jms']
def onMessage(messageObject) {
println "GOT MESSAGE: $messageObject"
}
}
Then it was just a matter of running the grails console and entering three lines to send a messge:
def connectionFactory = ctx.getBean("connectionFactory")
def template = new org.springframework.jms.core.JmsTemplate(connectionFactory)
template.convertAndSend("sampleQueue", "Message Posted!")
et voilà
GOT MESSAGE: Message Posted!
I may be easily pleased, but I found that quite satisfying.






It does look simple to do. It pretty much
Thomas Huang | June 25, 2008 | 8:31 pmIt does look simple to do. It pretty much simplified the process of initialize context and JNDI lookups. If the service is to subscribe to a JMS topic that requires user credential, do you know if there is a way to do this?
Hi Thomas, I haven't looked at this much but you'd
Ian | June 26, 2008 | 5:20 pmHi Thomas, I haven’t looked at this much but you’d usually provide these credentials to the factory used to create the connections. In this case the connectionFactory bean.
My assumption (which may well be incorrect) was that in my example the connection factory was being used by both sides, producer and consumer. It was just convenient to use it in the groovy script to post the message. Grails can’t be just making that stuff up!
[...] a question onto my post Grails Service, JMS and
englishteeth.co.uk » Grails & JMS Revisited: Topic instead of Queue | June 27, 2008 | 11:00 am[…] a question onto my post Grails Service, JMS and an MDB, I was amused and pleased to find that the grails site for the JMS Plugin had been updated and […]