Section 11.6. Unified Messaging


11.6. 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 = connFactory.createConnection( );     Destination mDestination =         (Destination)ctx.lookup(destJNDIName);     Session mSession =         mConn.createSession(false,                             javax.jms.Session.AUTO_ACKNOWLEDGE); 

This generic version of the code to create a Session is made possible by the addition of the ConnectionFactory.createConnection( ) method and the Connection.createSession( ) methods to the JMS API in Version 1.1. Other new elements in the API, not shown here, allow you to create MessageConsumers, MessageProducers, and other messaging entities in a generic way.

These new unified messaging aspects in JMS 1.1 allow you to simplify your messaging code quite a bit. You can create connections and exchange messages the same way no matter which messaging model is in use. The unified API elements also make it much easier to mix point-to-point and publish-subscribe messaging operations within the same session/transaction context. The downloadable source code for this book includes an example called MixedMessagingClient.java that shows how to merge our point-to-point and publish-subscribe client using the unified API elements in JMS 1.1.

Note that any earlier code that used the explicit Queue and Topic subclasses of the various JMS interfaces (such as our PTPMessagingClient and PubSubMessagingClient examples) will still work just fine in a JMS 1.1 environment. But any code that uses the new unified API elements, such as the preceding example, will work only in JMS 1.1 and later.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net