23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.4.7 Read-Only <strong>Message</strong>s<br />

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

When messages are delivered, the body of the message is made read-only. Any attempt to<br />

alter a message body after it has been delivered results in a<br />

javax.jms.<strong>Message</strong>NotWriteableException. The only way to change the body of a message<br />

after it has been delivered is to invoke the clearBody( ) method, which is defined in the<br />

<strong>Message</strong> interface. The clearBody( ) method empties the message's payload so that a new<br />

payload can be added.<br />

Properties are also read-only after a message is delivered. Why are both the body and<br />

properties made read-only after delivery? It allows the JMS provider more flexibility in<br />

implementing the <strong>Message</strong> object. For example, a JMS provider may choose to stream a<br />

Bytes<strong>Message</strong> or Stream<strong>Message</strong> as it is read, rather than all at once. Another vendor may<br />

choose to keep properties or body data in an internal buffer so that it can be read directly<br />

without the need to make a copy, which is especially useful with multiple consumers on<br />

the same client.<br />

3.4.8 Client-Acknowledged <strong>Message</strong>s<br />

The acknowledge( ) method, defined in the <strong>Message</strong> interface, is used when the consumer<br />

has chosen CLIENT_ACKNOWLEDGE as its acknowledgment mode. There are three<br />

acknowledgment modes that may be set by the JMS consumer when its session is created:<br />

AUTO_ACKNOWLEDGE, DUPS_OK_ACKNOWLEDGE, and CLIENT_ACKNOWLEDGE. Here is how a pub/sub<br />

consumer sets one of the three acknowledgment modes:<br />

TopicSession topic =<br />

topicConnection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);<br />

In CLIENT_ACKNOWLEDGE mode, the JMS client explicitly acknowledges each message as it is<br />

received. The acknowledge( ) method on the <strong>Message</strong> interface is used for this purpose. For<br />

example:<br />

public void on<strong>Message</strong>(<strong>Message</strong> message){<br />

message.acknowledge( );<br />

...<br />

}<br />

The other acknowledgment modes do not require the use of this method and are covered in<br />

more detail in Chapter 6 and Appendix B.<br />

Any acknowledgment mode specified for a transacted session is<br />

ignored. When a session is transacted, the acknowledgment is part of<br />

the transaction and is executed automatically prior to the commit of<br />

the transaction. If the transaction is rolled back, no acknowledgment<br />

is given. Transactions are covered in more detail in Chapter 6.<br />

51

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

Saved successfully!

Ooh no, something went wrong!