(Quick Reference)

4 Sending Messages

Version: 2.0.0.RC2

4 Sending Messages

This plugin adds a service called jmsService to your application that can be used to send JMS messages.

The send(destination, message, jmsTemplateName, postProcessor) method.

destination

An instance of javax.jms.Destination , javax.jms.Topic , a String or a Map .

A String destination argument will be interpreted as the name of a destination queue .

A Map destination argument can be used in the following ways:

jmsService.send(queue: "aQueue", msg, "standard", null) // send to a literal queue

jmsService.send(topic: "aTopic", msg, "standard", null) // send to a literal topic

jmsService.send(service: "person", msg, "standard", null) // send to the queue '«appname».person'

jmsService.send(service: "person", method: "doIt", msg, "standard", null) // send to the queue '«appname».person.doIt'

jmsService.send(app: "remote", service: "person", method: "doIt", msg, "standard", null) // send to the queue 'remote.person.doIt'

The app/service/method convention makes a lot more sense if you read the section below on service method listener queue subscribers.

message

This is the message payload. By default this can be any Java/Groovy object or a javax.jms.Message. How it gets converted into a message is handled by the underlying jms template's message converter.

jmsTemplateName

The name of the template that should be used to send the message. If this value is null , the standard template will be used (called "standard").

postProcessor

An optional closure that can be used to "post process" the message after it has been converted into a message but before it has been sent. This closure acts as the implementation of the postProcessMessage() method of the MessagePostProcessor class.

send() method variants

There are variations of the send() method for convenience...

jmsService.send(destination, message) // use the standard template and no post processor
jmsService.send(destination, message, postProcessor) // use the standard template

4.1 Post Processing Messages

Message post processors can either augment the passed Message object, or create a new one. Because of this, the post processor must return the message object that is to be sent.

import javax.jms.Message

jmsService.send(topic: 'somethingHappened', 1) { Message msg -> msg.JMSCorrelationID = "correlate" msg }

Setting destinations

Post processors can use the createDestination() method in post processor implementations to create destinations using the same API style as jmsService.send() method

jmsService.send(service: 'initial', 1) {
    it.JMSReplyTo = createDestination(service: 'reply')
    it
}

4.2 Using Other Templates

Here is an example of using a custom template that uses a different connection factory.

resources.groovy

import org.apache.activemq.ActiveMQConnectionFactory
import org.springframework.jms.connection.SingleConnectionFactory

beans = { // used by the standard template by convention jmsConnectionFactory(SingleConnectionFactory) { targetConnectionFactory = { ActiveMQConnectionFactory cf -> brokerURL = 'vm://localhost' } }

otherJmsConnectionFactory(SingleConnectionFactory) { targetConnectionFactory = { ActiveMQConnectionFactory cf -> brokerURL = // … something else } } }

application.yml

jms:
    templates:
        other:
            meta:
                parentBean: standardJmsTemplate
            connectionFactoryBean: otherJmsConnectionFactory // use different connection factory

Sending messages

jmsService.send(topic: "stuffHappened", message, "other")

The third argument of "other" to the send() method specifies to use the "other" template.