O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unified Messaging

JMS 1.1 introduced new elements to the API that allow messaging clients to use the same interfaces and methods to do either point-to-point messaging or publish-subscribe messaging. In JMS 1.0 (used in J2EE 1.3 and earlier environments), a client needed to use the specific subclasses discussed in the previous sections.

As an example, our previous clients had to explicitly cast the Destination and ConnectionFactory objects retrieved from the JNDI context in order to create a Session. Our PTPMessagingClient, for example, had to perform the following sequence of steps in order to create a Session (a QueueSession, to be more precise):

    Context ctx = new InitialContext();
    QueueConnectionFactory connFactory =
        (QueueConnectionFactory)ctx.lookup(cFactoryJNDIName);
    QueueConnection mQueueConn =
        connFactory.createQueueConnection();
    Queue mQueue = (Queue)ctx.lookup(queueJNDIName);
    QueueSession mSession =
        mQueueConn.createQueueSession(false,
                                     javax.jms.Session.AUTO_ACKNOWLEDGE);

Note that we’ve simplified this code from the original code in Example 11-2 by eliminating a lot of exception handling.

With the new unified API elements added in JMS 1.1, a client can use the common base classes (Destination instead of Queue or Topic, Connection instead of QueueConnection or TopicConnection, etc.). With JMS 1.1, we can shorten that code example considerably:

 Context ctx = new InitialContext(); Connection Factory connFactory = ConnectionFactory)ctx.lookup(cFactoryJNDIName); Connection mConn ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required