Upgrading JMS 1.1 code

There is a lot of code out there in business that simply cannot be rewritten from scratch. Some of it will almost certainly be using JMS 1.1 from the J2EE. If we cannot chuck it away, then how do we upgrade to JMS 2.0?

Establish a JMS 1.1 connection

It is worth learning about the traditional way of building a JMS connection to the provider in order to upgrade legacy code bases to Java EE 7.

In Java EE 5, which was the first enterprise standard to support Java annotations, you might see code that looks like this:

// JMS 1.1
import javax.jms.*;
import javax.annotation.*;

public SomeMessagingClient {
  @Resource(mappedName="myQueueConnectionFactoryName")
  ConnectionFactory queueConnectionFactory;

  @Resource(mappedName="myQueue")
  Queue queue;

  @Resource(mappedName="myTopicConnectionFactoryName")
  ConnectionFactory topicConnectionFactory;

  @Resource(mappedName="myTopic")
  Topic topic;

  /* ... */

  public void processWithQueue( String text ) 
  throws Exception {
    Connection conx = 
      queueConnectionFactory.createConnection();
    
    Session session = conx.createSession(true, 0 );
    try {
      TextMessage msg = session.createTextMessage();
      msg.setText( text );
      session.createProducer(queue).send(msg);
    }
    finally {
      conx.close();
    }
  }        
  /* ... */
}

In such source code from Java EE 5 and Java EE 6, the annotation @javax.annotation.Resource injects the JMS connection factory and the message destination into the application client, assuming the code runs inside an application server. The connection factory is used in the client code to create a connection.

Here is an example of code that sends a message on the preceding queue:

  public void processWithQueue( String text ) 
  throws Exception {
    Connection conx = 
      queueConnectionFactory.createConnection();
    Session session = conx.createSession(true, 
          Session.AUTO_ACKNOWLEDGE );
    try {
      TextMessage msg = session.createTextMessage();
      msg.setText( text );
      session.createProducer(queue).send(msg);
    }
    finally {
      conx.close();
    }
  }      

The preceding code looks very verbose, and you as a developer have to deal with checked exceptions and the possibility of interpreting the outcomes around a JMSException.

The new JMSContext object now provides the same behavior, and therefore the developer can replace @Resource injections with one @Inject and one @JMSContextConnectionFactory annotation:

// JMS 2.0
public SomeMessagingClient {
  @Inject
  @JMSContextConnectionFactory(
    "jms/myQueueConnectionFactoryName")
  JMSContext queueContext
  
  @Resource(mappedName="myQueue")
  Queue queue;

  @Inject
  @JMSContextConnectionFactory(
    "jms/myTopicConnectionFactoryName")
  JMSContext queueContext
  
  @Resource(mappedName="myTopic")
  Topic topic;
  
  /* ... */

  public void processWithQueue( String text ) 
  throws Exception {
    queueContext.createProducer().send(queue, text );
  }      
  
  public void processWithTopic( String text ) 
  throws Exception {
    topicContext.createProducer().send(topic, text );
  }      
	/* ... */
}

This previous code is the form of dependency injection that is typical for Java EE 7 applications, especially those that run inside an application server or EJB container. In the next section, we will dig deeper into dependency injection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190