10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

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.

are acknowledged. A transaction rollback means that all produced messages are destroyed and<br />

all consumed messages are recovered and redelivered unless they have expired (see “Allowing<br />

Messages to Expire” on page 935).<br />

A transacted session is always involved in a transaction. As soon as the commit or the rollback<br />

method is called, one transaction ends and another transaction begins. Closing a transacted<br />

session rolls back its transaction in progress, including any pending sends and receives.<br />

In an Enterprise <strong>Java</strong>Beans component, you cannot use the Session.commit and<br />

Session.rollback methods. Instead, you use distributed transactions, which are described in<br />

“Using the JMS API in a <strong>Java</strong> <strong>EE</strong> Application” on page 948.<br />

You can combine several sends and receives in a single JMS API local transaction. If you do so,<br />

you need to be careful about the order of the operations. You will have no problems if the<br />

transaction consists of all sends or all receives or if the receives come before the sends. But if you<br />

try to use a request/reply mechanism, whereby you send a message and then try to receive a<br />

reply to the sent message in the same transaction, the program will hang, because the send<br />

cannot take place until the transaction is committed. <strong>The</strong> following code fragment illustrates<br />

the problem:<br />

// Don’t do this!<br />

outMsg.setJMSReplyTo(replyQueue);<br />

producer.send(outQueue, outMsg);<br />

consumer = session.createConsumer(replyQueue);<br />

inMsg = consumer.receive();<br />

session.commit();<br />

Because a message sent during a transaction is not actually sent until the transaction is<br />

committed, the transaction cannot contain any receives that depend on that message’s having<br />

been sent.<br />

In addition, the production and the consumption of a message cannot both be part of the same<br />

transaction. <strong>The</strong> reason is that the transactions take place between the clients and the JMS<br />

provider, which intervenes between the production and the consumption of the message.<br />

Figure 31–9 illustrates this interaction.<br />

FIGURE 31–9 Using JMS API Local Transactions<br />

Sends<br />

Client 1 Queue<br />

Consumes<br />

Transaction 1 Transaction 2<br />

Client 2<br />

Creating Robust JMS Applications<br />

Chapter 31 • <strong>The</strong> <strong>Java</strong> Message Service API 941

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

Saved successfully!

Ooh no, something went wrong!