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.
destinationAn 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 queuejmsService.send(topic: "aTopic", msg, "standard", null) // send to a literal topicjmsService.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'
null
, the standard template will be used (called "standard").postProcessorAn 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.MessagejmsService.send(topic: 'somethingHappened', 1) { Message msg -> msg.JMSCorrelationID = "correlate" msg }
Setting destinations
Post processors can use thecreateDestination()
method in post processor implementations to create destinations using the same API style as jmsService.send()
methodjmsService.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.SingleConnectionFactorybeans = { // 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")