Section 11.7. Transactional Messaging


11.7. Transactional Messaging

JMS supports transactional messaging in two ways. In its simplest form, a Session is created with the transactional option (the first argument to QueueConnection.createQueueSession( ) and TopicConnection.createTopicSession( )):

     QueueSession xactSession =       qConn.createQueueSession(TRUE, Session.AUTO_ACKNOWLEDGE); 

When using a transactional Session, the client performs a series of "transactions" with the Session (sends or receives messages from consumers and producers associated with the Session). These sends and receives are either committed by calling the Session's commit( ) method or cancelled by calling rollback( ). If a Session is committed, all of the sends and receives are committed to the JMS provider, which causes the new state of the Destination(s) affected to be committed. If a Session is rolled back, all changes to the resources on the JMS provider are rolled back. In either case, the transaction is closed and a new one is started automatically, for any subsequent messaging actions.

JMS providers can also support transactional messaging through the Java Transaction API (JTA), which allows messaging transactions to be integrated with other resources, like databases. For broader coverage of JTA in a variety of contexts, see Chapter 16. The following material highlights some transactional details specific to JMS messaging.

JTA-based transactions are distributed: the underlying transactional resources can be distributed across the enterprise. The JMS API supports transactional messaging with a set of interfaces that provide access to JTA-aware Connections and Sessions. If a JMS provider supports JTA, it can export an XAConnectionFactory in its JNDI space. An XAConnectionFactory is used to create XAConnections, and XAConnections are used to create XASessions. An XASession is a specialization of Session that overloads the commit( ) and rollback( ) methods to implement them within a JTA context. There are subclasses of these XA interfaces for point-to-point and publish-subscribe messaging. For example, to create a JTA-aware TopicSession:

     XATopicConnectionFactory xFactory =       (XATopicConnectionFactory)ctx.lookup("xact-factory");     XATopicConnection xConn = xFactory.createXATopicConnection( );     XATopicSession xSession = xConn.createXATopicSession( ); 

When a client performs a series of sends/receives with a JTA-aware Session, these actions are performed in the context of the surrounding UserTransaction, if one exists. For example, if we use our XATopicSession to create a TopicPublisher (xPublisher) and a TopicSubscriber (xSubscriber), we can create our own JTA transaction and use it to commit or roll back a series of message operations:

     javax.transaction.UserTransaction xaction = ...;     xaction.start( );     try {       Message request = ...;       Message response = ...;       xPublisher.publish(request);       response = xSubscriber.receive( );       // Made it here, so commit the topic changes       xaction.commit( );     }     catch (JMSException je) {       // Something bad happened, so cancel the topic changes caused       // by our message sends/receives       xaction.rollback( );     } 



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