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.

Creating Robust JMS Applications<br />

942<br />

<strong>The</strong> sending of one or more messages to one or more destinations by client 1 can form a single<br />

transaction, because it forms a single set of interactions with the JMS provider using a single<br />

session. Similarly, the receiving of one or more messages from one or more destinations by<br />

client 2 also forms a single transaction using a single session. But because the two clients have<br />

no direct interaction and are using two different sessions, no transactions can take place<br />

between them.<br />

Another way of putting this is that the act of producing and/or consuming messages in a session<br />

can be transactional, but the act of producing and consuming a specific message across different<br />

sessions cannot be transactional.<br />

This is the fundamental difference between messaging and synchronized processing. Instead of<br />

tightly coupling the sending and receiving of data, message producers and consumers use an<br />

alternative approach to reliability, one that is built on a JMS provider’s ability to supply a<br />

once-and-only-once message delivery guarantee.<br />

When you create a session, you specify whether it is transacted. <strong>The</strong> first argument to the<br />

createSession method is a boolean value. A value of true means that the session is transacted;<br />

a value of false means that it is not transacted. <strong>The</strong> second argument to this method is the<br />

acknowledgment mode, which is relevant only to nontransacted sessions (see “Controlling<br />

Message Acknowledgment” on page 930). If the session is transacted, the second argument is<br />

ignored, so it is a good idea to specify 0 to make the meaning of your code clear. For example:<br />

session = connection.createSession(true, 0);<br />

<strong>The</strong> commit and the rollback methods for local transactions are associated with the session.<br />

You can combine queue and topic operations in a single transaction if you use the same session<br />

to perform the operations. For example, you can use the same session to receive a message from<br />

a queue and send a message to a topic in the same transaction.<br />

You can pass a client program’s session to a message listener’s constructor function and use it to<br />

create a message producer. In this way, you can use the same session for receives and sends in<br />

asynchronous message consumers.<br />

<strong>The</strong> next section provides an example of the use of JMS API local transactions.<br />

A LocalTransaction Example<br />

<strong>The</strong> TransactedExample.java program demonstrates the use of transactions in a JMS client<br />

application. <strong>The</strong> program is in the following directory:<br />

tut-install/javaeetutorial5/examples/jms/advanced/transactedexample/src/java/<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

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

Saved successfully!

Ooh no, something went wrong!