23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

XAResource jmsXA = session.getXAResource( );<br />

/*<br />

* Enlist the XAResource in the transaction. This adds the JMS<br />

* resource to the global transaction.<br />

*/<br />

transaction.enlist(jmsXA);<br />

// ... do some JMS work<br />

/*<br />

* Committing the global transaction will allow both the JDBC<br />

* work (all updates and inserts) as well as the JMS work<br />

* (all the message sends and receives) to be commited as a single<br />

* unit of work. They all succeed or fail together.<br />

*/<br />

transaction.commit( );<br />

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

All XA-compliant resources ( JDBC or JMS) provide an XAResource object that is an<br />

interface to the underlying resource (in JMS, the JMS provider). The XAResource object is<br />

used by the TransactionManager to coordinate the 2PC commit. In the previous example,<br />

the application associates the XAResource for the JDBC driver and the JMS provider with<br />

the current transaction so that all the work performed using those resources is bound<br />

together in one transaction. When the transaction is committed, all the work performed by<br />

the JDBC connection and JMS session is committed. If the transaction had been rolled<br />

back, all the work performed by the JDBC connection and JMS session would have been<br />

rolled back. All the work performed across these two resources either succeeds together or<br />

fails together.<br />

An application server, such as an EJB server, may itself be a JMS client. In this case,<br />

whether the interfaces are exposed depends on how the JMS server and the application<br />

server are integrated. If the integration is hidden within the implementation, as is the case<br />

with EJB, then the container may use the XA-compliant version of these objects directly.<br />

Since the XA interfaces in JMS are not intended for application developers - they are<br />

intended to be implemented by vendors - we will not go into them in detail in this book.<br />

The important thing to understand is that JMS providers that implement the XA interfaces<br />

properly can be used in a 2PC transaction. If your application server (i.e., EJB server)<br />

supports 2PC, then these kinds of JMS providers can be used with other resources in<br />

global transactions.<br />

6.5 Lost Connections<br />

When the network connection between the client and server is lost, a JMS provider must<br />

make every reasonable attempt to re-establish the connection. In the event that the JMS<br />

provider cannot automatically reconnect, the provider must notify the client of this<br />

condition by throwing an exception when the client invokes a method that would cause<br />

network traffic to occur. However, it is reasonable to expect that a JMS client may only be<br />

a receiver using the <strong>Message</strong>Listener interface, and not a producer that makes any<br />

outbound publish( ) or send( ) calls. In this case, the client is not invoking JMS methods<br />

- it is just listening for messages - so a dropped connection won't be detected.<br />

104

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!