O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
The 2PC protocol is used by a transaction manager to coordinate the interactions of<br />
resources in a global transaction. A resource can only participate in a global transaction if<br />
it supports the 2PC protocol, which is usually implemented using the XA interface<br />
developed by The Open Group. In the <strong>Java</strong> enterprise technologies, the XA interface is<br />
implemented by the <strong>Java</strong> Transaction API and XA interfaces (javax.transaction and<br />
javax.transaction.xa). Any resource that implements these interfaces can be enrolled in a<br />
global transaction by a transaction manager that supports these interfaces.<br />
JMS providers that implement the JTA XA APIs can therefore participate as a resource in<br />
two-phase commit. The JMS specification provides XA versions of the following JMS<br />
objects: XAConnectionFactory, XAQueueConnection, XAQueueConnectionFactory,<br />
XAQueueSession, XASession, XATopicConnection, XATopicConnectionFactory, and<br />
XATopicSession.<br />
Each of these objects works like its corresponding non-XA-compliant object. The<br />
XATopicSession, for example, provides the same methods as the TopicSession. An<br />
application server's transaction manager uses these XA interfaces directly, but a JMS client<br />
only sees the nontransactional versions. The following code shows a JMS client that uses<br />
JDBC and JMS together in one transaction managed by an external transaction manager.<br />
JNDI is used to obtain all the resources including the JTA TransactionManager, the JDBC<br />
XADataSource, and the JMS XATopicConnectionFactory:<br />
InitialContext jndiContext = new InitialContext(env);<br />
// Obtain the JTA TransactionManager a JNDI namespace<br />
TransactionManager txMngr =jndiContext.lookup("../../tx/txmngr");<br />
// Start the Global Transaction<br />
txMngr.begin( );<br />
// Get the transaction object that represents the new global transaction<br />
Transaction transaction = txMnger.getTransaction( );<br />
// Obtain the JDBC DataSource from a JNDI namespace<br />
XADataSource dataSrc = jndiContext.lookup("../../jdbc/data");<br />
// Obtain an XA-compliant Connection<br />
XAConnection jdbcCon = dataSrc.getConnection( );<br />
// Obtain the XAResource from the connection<br />
XAResource jdbcXA = jdbcCon.getXAResource( );<br />
/*<br />
* Enlist the XAResource in the transaction. This adds the JDBC<br />
* resource to the global transaction.<br />
*/<br />
transaction.enlist(jdbcXA);<br />
//... do some JDBC work<br />
// Obtain an XA-compliant TopicConnectionFactory from a JNDI namespace<br />
XATopicConnectionFactory factory = jndiContext.lookup("../.../jms/factory");<br />
// Obtain an XA-compliant TopicConnection<br />
XATopicConnection jmsCon = factory.createXATopicConnection( );<br />
// Obtain an XA-compliant Session<br />
XATopicSession session = jmsCon.createXATopicSession( );<br />
// Obtain the XAResource from the session<br />
103