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.

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

are delivered once-and-only-once. The mechanics of this are covered in greater detail in<br />

Chapter 6.<br />

The vendor-supplied client runtime and the server functionality are<br />

collectively referred to as the JMS provider. A "provider failure"<br />

generically describes any failure condition that is outside of the<br />

domain of the application code. It could mean a hardware failure that<br />

occurs while the provider is entrusted with the processing of a<br />

message, or it could mean an unexpected exception or halting of a<br />

process due to a software defect. It could also mean a network failure<br />

that occurs between two processes that are part of the JMS vendor's<br />

internal architecture.<br />

Nonpersistent messages are not written to disk when they are received by the message<br />

server, so if the JMS provider fails, the message will be lost. In general nonpersistent<br />

messages perform better than persistent messages. They are delivered more quickly and<br />

require less system resources on the message server. However, nonpersistent messages<br />

should only be used when a loss of messages due to a JMS provider failures is not an issue.<br />

The chat example used in Chapter 2 is a good example of a system that doesn't require<br />

persistent delivery. It's not critical that every message be delivered to all consumers in a<br />

chat application. In most business systems, however, messages are delivered using the<br />

persistent mode, because it's important that they be successfully delivered.<br />

The delivery mode can be set using the setDeliveryMode( ) method defined in both the<br />

TopicPublisher and QueueSender message producers. The javax.jms.DeliveryMode class<br />

defines the two constants used to declare the delivery mode: PERSISTENT and<br />

NON_PERSISTENT :<br />

// Publish-and-subscribe<br />

TopicPublisher topicPublisher = topicSession.createPublisher(topic);<br />

topicPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);<br />

// Point-to-point<br />

QueueSender queueSender = queueSession.createSender(queue);<br />

queueSender.setDeliverMode(DeliveryMode.PERSISTENT);<br />

Once the delivery mode has been set on the message producer, it will be applied to all the<br />

messages delivered by that producer. The delivery mode can be changed at any time using<br />

the setDeliveryMode( ) method; the new mode will be applied to subsequent messages.<br />

The default delivery mode of a message producer is always PERSISTENT.<br />

The delivery mode of a message producer can be overridden for an individual message<br />

during the send operation, which allows a message producer to deliver a mixture of<br />

persistent and nonpersistent messages to the same destination (topic or queue):<br />

// Publish-and-subscribe<br />

<strong>Message</strong> message = topicSession.create<strong>Message</strong>( );<br />

topicPublisher.publish(message, DeliveryMode.PERSISTENT, 5, 0);<br />

// Point-to-point<br />

<strong>Message</strong> message = queueSession.create<strong>Message</strong>( );<br />

queueSender.send(message, DeliveryMode.NON_PERSISTENT, 5, 0);<br />

158

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

Saved successfully!

Ooh no, something went wrong!