Grails Service, JMS and an MDB

After 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 message:

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.

comments powered by Disqus