background image

Difference of Messaging and Synchronized Processing

<< Using JMS API Local Transactions | A Local Transaction Example >>
<< Using JMS API Local Transactions | A Local Transaction Example >>

Difference of Messaging and Synchronized Processing

The sending of one or more messages to one or more destinations by client 1 can form a single
transaction, because it forms a single set of interactions with the JMS provider using a single
session. Similarly, the receiving of one or more messages from one or more destinations by
client 2 also forms a single transaction using a single session. But because the two clients have
no direct interaction and are using two different sessions, no transactions can take place
between them.
Another way of putting this is that the act of producing and/or consuming messages in a session
can be transactional, but the act of producing and consuming a specific message across different
sessions cannot be transactional.
This is the fundamental difference between messaging and synchronized processing. Instead of
tightly coupling the sending and receiving of data, message producers and consumers use an
alternative approach to reliability, one that is built on a JMS provider's ability to supply a
once-and-only-once message delivery guarantee.
When you create a session, you specify whether it is transacted. The first argument to the
createSession
method is a boolean value. A value of true means that the session is transacted;
a value of false means that it is not transacted. The second argument to this method is the
acknowledgment mode, which is relevant only to nontransacted sessions (see
"Controlling
Message Acknowledgment" on page 936
). If the session is transacted, the second argument is
ignored, so it is a good idea to specify 0 to make the meaning of your code clear. For example:
session = connection.createSession(true, 0);
The commit and the rollback methods for local transactions are associated with the session.
You can combine queue and topic operations in a single transaction if you use the same session
to perform the operations. For example, you can use the same session to receive a message from
a queue and send a message to a topic in the same transaction.
You can pass a client program's session to a message listener's constructor function and use it to
create a message producer. In this way, you can use the same session for receives and sends in
asynchronous message consumers.
The next section provides an example of the use of JMS API local transactions.
Sends
Client 1
Queue
Consumes
Client 2
Transaction 1
Transaction 2
FIGURE 31­9
Using JMS API Local Transactions
Creating Robust JMS Applications
The Java EE 5 Tutorial · September 2007
948