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.

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

received, depending on failures or downtime of other processes that are involved. It is<br />

more practical to group the receipt of a message with the send of another message.<br />

6.4.1 Creating and Using a JMS Transaction<br />

Now that you understand the concepts of transactional sends and receives, we can take a<br />

look at some code. The first step in creating a transactional message is the initialization of<br />

the Session object:<br />

// pub/sub connection creates a transacted TopicSession<br />

javax.jms.TopicSession session =<br />

connect.createTopicSession(true,Session.AUTO_ACKNOWLEDGE);<br />

// p2p connection creates a transacted QueueSession<br />

javax.jms.QueueSession =<br />

connect.createQueueSession(true,Session.AUTO_ACKNOWLEDGE);<br />

...<br />

}<br />

The first parameter of a createTopicSession( ) or createQueueSession( ) method is a<br />

boolean indicating whether this is a transacted session. That is all we need to create a<br />

transactional session. There is no explicit begin( ) method. When a session is transacted,<br />

all messages sent or received using that session are automatically grouped in a transaction.<br />

The transaction remains open until either a session.rollback( ) or a session.commit( )<br />

happens, at which point a new transaction is started. [3] An additional Session method,<br />

isTransacted( ), returns true or false indicating whether or not the current session is<br />

transactional.<br />

[3] This is called "transaction chaining," which means that the end of one transaction automatically<br />

starts another.<br />

6.4.2 The Transacted Retailer Example<br />

To demonstrate how to use transactions, we will develop a modified version of our<br />

publish-and-subscribe Wholesaler and Retailer from Chapter 4. The use of JMS<br />

transactions for sending or receiving just one message is only mildly interesting. You get<br />

one message, then decide whether it should be committed or rolled back. In this new<br />

example we will show:<br />

• A new technique for grouping multiple messages together<br />

• Multiple receives and sends grouped in one transaction<br />

• Handling of message redelivery, and how to distinguish between redelivery due to<br />

failure and redelivery due to a transaction rollback<br />

In this scenario, the wholesaler broadcasts a group of "special deals" that can only be<br />

purchased together. The separate items are each contained in their own messages, yet are<br />

related to each other. Today's special deals are surfboards and wetsuits. The retailer has<br />

logic that looks at the messages individually as they arrive. If the price is acceptable, the<br />

orders are placed for each item on a per-message basis. When the retailer sees that all of<br />

the messages in the group have an acceptable price discount, the orders are placed. If any<br />

one of the prices in the group is unacceptable, all the orders are cancelled. All this takes<br />

place without the knowledge of the wholesaler. The wholesaler never sees a thing unless<br />

the retailer decides that it is OK to place all of the orders. To accomplish this, the retailer<br />

97

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

Saved successfully!

Ooh no, something went wrong!