📜 ⬆️ ⬇️

Introduction to JMS 2.0

Not so long ago, on June 12, 2013, the release of Java EE 7 was presented to the world. One of the key points in this release was the appearance of JMS version 2.0, which has not been updated since 2002.

This text is a free translation of the beginning of the article by Nigel Dikin . The text is intended to familiarize the interested reader with the new API.

The JMS 1.1 API requires a lot of code to work, but has managed to recommend itself well, because it has not changed since 2002. In JMS 2.0, the new API is designed to simplify the sending and receiving of JMS messages. The previous API is not abolished and continues to work on a par with the new one.
')
The JMS 2.0 API has new interfaces: JMSContext, JMSProducer and JMSConsumer.


Sending JMS

For comparison. JMS 1.1:
public void sendMessageJMS11(ConnectionFactory connectionFactory, Queue queue, String text) { try { Connection connection = connectionFactory.createConnection(); try { Session session =connection.createSession(false,Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session.createProducer(queue); TextMessage textMessage = session.createTextMessage(text); messageProducer.send(textMessage); } finally { connection.close(); } } catch (JMSException ex) { // handle exception (details omitted) } } 


JMS 2.0:
 public void sendMessageJMS20(ConnectionFactory connectionFactory, Queue queue, String text) { try (JMSContext context = connectionFactory.createContext();){ context.createProducer().send(queue, text); } catch (JMSRuntimeException ex) { // handle exception (details omitted) } } 




A distinctive feature of the new API is that its methods throw a RuntimeException - JMSRuntimeException, instead of the JMSException checked-exception. This makes it possible, if desired, not to handle JMS exceptions.

JMS Sync Receive

Compare the difference when receiving messages synchronously.
JMS 1.1:
 public String receiveMessageJMS11(ConnectionFactory connectionFactory,Queue queue){ String body=null; try { Connection connection = connectionFactory.createConnection(); try { Session session =connection.createSession(false,Session.AUTO_ACKNOWLEDGE); MessageConsumer messageConsumer = session.createConsumer(queue); connection.start(); TextMessage textMessage = (TextMessage)messageConsumer.receive(); body = textMessage.getText(); } finally { connection.close(); } } catch (JMSException ex) { // handle exception (details omitted) } return body; } 


JMS 2.0:
 public String receiveMessageJMS20(ConnectionFactory connectionFactory,Queue queue){ String body=null; try (JMSContext context = connectionFactory.createContext();){ JMSConsumer consumer = context.createConsumer(queue); body = consumer.receiveBody(String.class); } catch (JMSRuntimeException ex) { // handle exception (details omitted) } return body; } 



Asynchronous JMS retrieval

In JavaSE, to receive messages asynchronously, the following code is used in JMS 1.1:

 MessageConsumer messageConsumer = session.createConsumer(queue); messageConsumer.setMessageListener(messageListener); connection.start(); 


In JMS 2.0, it looks like this:

 JMSConsumer consumer = context.createConsumer(queue); consumer.setMessageListener(messageListener); 


Instead of MessageConsumer - JMSConsumer. Connection starts automatically.

In Java EE Web or EJB applications, as before, you must use the message-driven bean, instead of the setMessageListener method

Inserting a JMSContext object in a Java EE application


In a Java EE application, JMSContext can be inserted using the Inject annotation. Once inserted, the JMSContext will be managed by the application server.

The following code snippet allows you to insert a JMSContext into a session bean or servlet.
 @Inject @JMSConnectionFactory( "jms/connectionFactory") private JMSContext context; @Resource(lookup = "jms/dataQueue") private Queue dataQueue; public void sendMessageJavaEE7(String body) { context.send(dataQueue, body); } 


JMSContext closes automatically by the application server. If a JTA transaction is executed during the request, the JMSContext will close automatically after the commit, if without a transaction, it will close at the end of the request.

On this I decided to stop. This information should be sufficient for familiarization and start of work. Many details and additional information here:
What's New in JMS 2.0, Part One: Ease of Use
What's New in JMS 2.0, Part Two — New Messaging Features

Source: https://habr.com/ru/post/184460/


All Articles