Grails & JMS Revisited - Topic instead of Queue

Following 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 included much of my post. Right down to the xfire reference, setting up Apache ActiveMQ and my service name typo… Sample was meant to be Simple. Unfortunately no citation, though there was quite a bit of other information added too.

Thomas Huang’s question:

It 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?

got me to thinking about publish subscribe as opposed to point to point and how that might be set up using Grails.

No changes were needed to the Apache ActiveMQ set up or the spring configuration in the grails app. I simply added another service to the mix.

class SampleTopicService { 
  static expose = ['jms'] 
  static pubSub = true 
  static destination = 'test/topic' 
  
  def onMessage(messageObject) { 
    println "GOT PUBLISHED MESSAGE: $messageObject" 
  } 
}

The destination bit was a little allusive at first. Without it or trying the static “topic” results in the default configuration whereby the service name “sampleTopic” is used for the destination (topic or queue).

To execute this from the grails console needed one extra line to tell the JMS template to use a topic rather than a queue:

def connectionFactory = ctx.getBean("connectionFactory")
def template = new org.springframework.jms.core.JmsTemplate(connectionFactory)
template.setPubSubDomain(true)
template.convertAndSend("test/topic", "Message Posted!")

et voilà

GOT PUBLISHED MESSAGE: Message Posted!

Set the pub sub domain to false and no message is received for SampleTopicService though the previous SampleQueueService works on its destination and visa versa.

Not really the answer to the question Thomas posted here, but it may help with the aspect he mentioned here.

comments powered by Disqus