The JmsTemplate
contains many convenience methods to send a message. There are send methods that specify the destination using a javax.jms.Destination
object and those that specify the destination using a string for use in a JNDI lookup. The send method that takes no destination argument uses the default destination. Here is an example that sends a message to a queue using the 1.0.2 implementation.
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.JmsTemplate;
public class JmsQueueSender {
private JmsTemplate jmsTemplate;
private Queue queue;
public void setConnectionFactory(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate(cf, false);
}
public void setQueue(Queue queue) {
this.queue = queue;
}
public void simpleSend() {
this.jmsTemplate.send(this.queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
}
});
}
}
This example uses the MessageCreator
callback to create a text message from the supplied Session
object and the JmsTemplate
is constructed by passing a reference to a ConnectionFactory
and a boolean specifying the messaging domain. A zero argument constructor and connectionFactory / queue bean properties are provided and can be used for constructing the instance (using a BeanFactory or plain Java code). Alternatively, consider deriving from Spring's JmsGatewaySupport
convenience base class, which provides pre-built bean properties for JMS configuration.
The method send(String destinationName, MessageCreator creator)
lets you send to a message using the string name of the destination. If these names are registered in JNDI, you should set the destinationResolver property of the template to an instance of JndiDestinationResolver
.
If you created the JmsTemplate
and specified a default destination, the send(MessageCreator c)
sends a message to that destination.
From:http://docs.spring.io/spring/docs/3.0.0.RC2/reference/html/ch21s03.html