Java Reference
In-Depth Information
public void sendMessage() throws NamingException, JMSException {
try (QueueConnection queueConnection = queueConnFactory.createQueueConnection();) {
QueueSession queueSession =
queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
queue = queueSession.createQueue("JavaEEQueue");
// create the message to send
TextMessage textMessage = queueSession.createTextMessage("This is a test message");
QueueSender sender = queueSession.createSender(queue);
With the aid of resource injection, a number of lines can be removed from previous JMS solutions, making code
easier to read and maintain.
Similarly, the domain specific API for pub/sub messaging has been made simpler via updated technologies
such as resource injection. Although the simplified API is not used for creation of TopicPublisher object from a
TopicSession , the work involved has been reduced significantly in JMS 2.0. The following example demonstrates how
to create a TopicPublisher and send messages to a Topic .
@Resource(name = "jms/TopicConnFactory")
private TopicConnectionFactory topicConnFactory;
@Resource(lookup = "jms/Topic")
Topic topic;
public void publishMessage() throws NamingException, JMSException {
try (TopicConnection topicConnection = topicConnFactory.createTopicConnection();
TopicSession topicSession =
topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);) {
Topic topic = topicSession.createTopic("JavaEE");
// create the message to send
TextMessage textMessage = topicSession.createTextMessage("This is a test message");
javax.jms.TopicPublisher topicPublisher = topicSession.createPublisher(topic);
Search WWH ::

Custom Search