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.

Else, commit (that's what just happened):<br />

else<br />

session.commit( );<br />

}<br />

return;<br />

Otherwise, just continue on and process the message.<br />

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

Point-to-point queues have the same transactional capabilities as publish-and-subscribe,<br />

with one caveat: when a rollback( ) occurs on a QueueSession, the messages are placed<br />

back on the queue. Because a queue may have multiple recipients, the messages could now<br />

go to another consuming client. This may result in messages being processed out of order,<br />

since the other client could have processed newer messages while the older messages were<br />

being rolled back. This is an area where behavior may vary, depending on how your<br />

vendor interprets the standard.<br />

Finally, you may have noticed that we used Wholesaler and Retailer for this example<br />

instead of their "Q" counterparts. This is because a publish-and-subscribe session and<br />

point-to-point session cannot jointly particpate in a JMS transaction. JMS transactions<br />

cannot span multiple sessions without the use of a JTA-compliant transaction manager (see<br />

the next section, Section 6.4.3). We wanted to show the use of JMS as first-class<br />

middleware in its own right, without using a supporting application server just to provide a<br />

shared transaction between a p2p and a pub/sub session. The inability of p2p and pub/sub<br />

sessions to share a transaction is a weakness in JMS that may be addressed in a future<br />

version of the specification.<br />

6.4.3 Distributed Transactions<br />

Distributed systems sometimes use a two-phase commit (2PC) process that allows multiple<br />

distributed resources to participate in one transaction. This typically involves an<br />

underlying transaction manager that takes care of coordinating the prepare, commit, or<br />

rollback of each resource participating in the transaction. In most cases, the resources<br />

involved in the transaction are databases, but they can be other things, like JMS providers.<br />

Transactions can be either local transactions or global transactions. Local transactions<br />

involve work performed on a single resource: one database or JMS provider. Global<br />

transactions involve work performed across several different resources, i.e., some<br />

combination of databases and JMS providers. JMS provides transaction facilities for both<br />

local and global transactions. The transacted sessions discussed previously are local<br />

transactions in JMS; they involve a single JMS provider.<br />

The 2PC protocol is designed to facilitate global transactions, transactions that span<br />

multiple resources. As an example, an enterprise application may need to process<br />

(consume and produce) messages as well as make changes to a database. In some cases,<br />

the processing of messages and database updates needs to be treated as a single unit of<br />

work, so that a failure to update the database or consume a message will cause the entire<br />

unit of work to fail. This is the basic premise behind a transaction: all the tasks must<br />

complete or fail together. To create a unit of work that spans different resources, the<br />

resources must be able to cooperate with a transaction manager in a 2PC.<br />

102

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

Saved successfully!

Ooh no, something went wrong!