Chapter 4. Application design 85
the listener port automatically stops listening. Unfortunately, this stops all
processing. In fact, there are two more properties needed, and provided by JMS:
The
redelivery count message property
This property defines how many times a message has already been read
from the queue.
The
backout threshold destination property
When the same message has been read a number of times equal to the
backout threshold, the infrastructure moves the message to a default queue
for messages that could not be delivered. The queue is called the dead letter
queue.
A rollback behavior that supports multiple (but not unlimited) processing retries
without stopping the listener can be achieved by setting the maximum retries
higher than the backout threshold. This allows the consumer to try to process a
message for the number of times specified by the backout threshold without
reaching the maximum retries number. Each time the redelivery fails the
redelivery count is incremented. When the redelivery count equals the backout
threshold, the message is moved to the dead letter queue. Maximum retries is
never reached and the listener port is not stopped.
4.5.3 Message producers
Message producers are triggered by programs to deliver messages. Producers
wrap messages they get from the application as payload in messaging-specific
messages; make sure the header attributes like expiry, reply address, message
ID, and maybe correlation ID are set correctly; and put the messages onto a
queue.
Example 4-5 shows a code sample of a simple message producer that does not
set any header attributes on the message.
Example 4-5 Simple message producer
public class JmsProducer {
public void sendMessage(String msg, String conFactoryName, String destName)
throws NamingException, JMSException {
Session session = null;
Connection connection = null;
MessageProducer producer = null;
try {
//Get the specified connection factory and queue
Context jndiConext = new InitialContext();
ConnectionFactory factory = (ConnectionFactory)
86 Enabling SOA Using WebSphere Messaging
jndiContext.lookup(conFactoryName);
Destination destination = (Destination)jndiContext.lookup(destName);
//Create the connection and session
Connection conenction = factory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
//Use the session and destination to create the producer
producer = session.createProducer(destination);
//Create and send the message
TextMessage message = session.createTextMessage(msg);
message.setJMSMessageID(messageId);
producer.send(message);
}
finally {
if (producer != null) {
producer.close();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
}
}
Example 4-6 shows an extension of the previous example by setting some
header attributes on the message. This producer implementation could be used
as a sender on the service consumer side to support a pseudo-synchronous
request-reply scenario. Note the reply queue and the message ID are set.
Example 4-6 Message producer setting message header attributes
public class JmsProducer {
public void sendMessage(String msg, String conFactoryName, String destName,
String replyDestName, long messageTimeout, String messageId)
throws NamingException, JMSException {
...
//Set header properties
Destination replyDestination = (Destination)
jndiContext.lookup(replyDestName);
message.setJMSReplyTo(replyDestination)
Get Enabling SOA Using WebSphere Messaging now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.