Tags

, , , , , , , ,

After a decade, JMS got an update from 1.1 to 2. Yes, JMS 1.1 was released on 2002. After all, now it got next version JMS 2.0 on April 2013 with simplified API’s.

As this api is incorporated with Java 7, both the standalone J2SE applications and J2EE applications will work with Java 7 only.

The classic old api’s in JMS 1.1 was not deprecated. It still supported by JMS 2.0

The single biggest change in JMS 2.0 is the introduction of a new API for sending and receiving messages that reduces the amount of code a developer must write. For applications that run in a Java EE application server, the new API also supports resource injection. This allows the application server to take care of the creation and management of JMS objects, simplifying the application even further.

Check out below the code usually the developer writes in JMS 1.1 and JMS 2.0

Send a text message using 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)
}
}

Send a text message using 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)
}
}

Here, creating the connection, session, text message and closing the connection are eliminated in JMS 2.0. Creating a single JMSContext object is enough to do. JMSContext implements the interface java.lang.AutoCloseable(java 7 feature) to close the context. So we need not worry about closing the connections.

Receive messages synchronously using jms 1.1

Here is the code involves receiving the message from queue/topic using 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;
}

Receive messages synchronously using jms 2.0

The above code is reduced as below.

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;
}

Creating connection, session and typecast the message steps are reduced and the context will take care everything. Also, no need of start the receiver to receive the messages. By default, it will happen in JMS 2.0

Likewise, the same above steps followed in sending/receiving messages asynchronously.

JMSContext injection for J2EE applications

The following code is a fragment from a Java EE 7 session bean or servlet that injects a JMSContext and uses it to send a message:

@Inject @JMSConnectionFactory(
“jms/connectionFactory”) private JMSContext context;

@Resource(lookup = “jms/dataQueue”) private Queue dataQueue;

public void sendMessageJavaEE7(String body) {
context.send(dataQueue, body);
}

As you can see, there is no code to create the JMSContext and no code to close it. Instead, we simply declare a field of type JMSContext and add the annotations @Inject and @JMSConnectionFactory.

The @Inject annotation tells the container to create the JMSContext when it is needed. The @JMSConnectionFactory annotation tells the container the JNDI name of the ConnectionFactory that it should use.
Also, there are other API simplifications in JMS 2.0 to reduce the coding effort and to improve the scalability and performance of the application.

Advertisements