80 Enabling SOA Using WebSphere Messaging
Figure 4-10 Activity diagram of the request handling using a request log
4.5.2 Message consumers
This section contains design guidelines and best practices applied to message
consumers. It discusses the synchronous as well as the asynchronous
implementation approach. A behavior that needs to be supported by both types
is the ability to select specific messages from a queue, for example, to get just
the corresponding reply for a request.
Selective consumer
The selective consumer enables selective reading of specific messages in a
queue. This ability is required because it is often not possible to create a specific
queue for each message type, either because this would involve too many
queues or because dynamic queues are preferred and not known in advance.
Messaging addresses this issue by providing a mechanism that allows message
producers to set message header properties that can be used from message
consumers as selection criteria for specific messages.
Selective consumers can be used to implement filtering, dispatching, and
ordering functionality.
Message ID not found Message ID found
Get request
Check log
Message ID found Message ID not found
Get reply from log
Process request
Rollback
processing
Log record
Put reply
Chapter 4. Application design 81
Consider, for example, a car purchase process. An employee may buy cars
without telling someone until a specific price limit; above that limit there is
notification to his manager needed. The notification process can be implemented
using messaging by copying the purchase price as a property into the message
header, thus allowing two different consumers to select messages based on the
price. One consumer would have to consume the messages below the critical
price, the other one above this price, providing notification functionality.
Example 4-1 shows a code sample of a producer that sets the price as a header
property in the message.
Example 4-1 Message producer
public class JmsSelectionSupportedProducer {
public void sendMessage(String msg, String price, String conFactoryName,
String destName) throws NamingException, JMSException {
...
//Create the message, set the property and send the message
TextMessage message = session.createTextMessage(msg);
message.setIntProperty(“price“, price);
producer.send(message);
...
}
}
Example 4-2 shows a code sample of a selective consumer. The consumer only
reads messages whose value of the price attribute in the message header is
higher than 10000.
Example 4-2 Selective consumer
public class JmsSelectiveConsumer {
public String receiveMessage(String conFactoryName, String destName)
throws NamingException, JMSException {
String messageSelector = “price > 10000”;
//Use the session, destination and selector to create the consumer
consumer = session.createConsumer(destination, messageSelector);
//Reveive the message
TextMessage message = consumer.receive();
...
}
}
82 Enabling SOA Using WebSphere Messaging
The selection criteria should be chosen in a way that guarantees all possible
variants are served. If this is not the case messages that do not expire may stay
in the queue without ever being read by a consumer.
Polling consumer
The polling consumer acts in a synchronous manner since the receiver thread is
blocked until a message is available. Normally, it is the application or service that
controls polling consumers by telling them when to start polling.
A pseudo-synchronous request-reply scenario is a sample where polling
consumers are applied. The service consumer triggers the receiver functionality
immediately after sending the request. The receiver thread is then blocked until
either the reply message arrives or a receiver timeout occurs.
Design considerations that should be made during the design of polling
consumers involve the definition of receiver timeouts as well as definitions about
exception paths to take if expected messages do not arrive. Additional design is
needed for consumers that may just read specific messages, for example,
messages with a specific correlation ID.
Example 4-3 shows a code sample of a simple polling consumer. The consumer
could be used as a receiver on the service consumer side to support a
pseudo-synchronous request-reply scenario. Note the selector, which enables
the consumer to listen for a message with a specific correlation ID.
Example 4-3 Polling consumer
public class JmsPollingConsumer {
public String receiveMessage(String conFactoryName, String correlationId,
String destName, int timeout) throws NamingException, JMSException {
Session session = null;
Connection connection = null;
MessageConsumer consumer = null;
String msg = null;
String messageSelector = “JMSCorrelationID=’ID:“ + correlationId + “‘“;
try {
//Get the specified connection factory and queue
Context jndiConext = new InitialContext();
ConnectionFactory factory = (ConnectionFactory)
jndiContext.lookup(conFactoryName);
Destination destination = (Destination)jndiContext.lookup(destName);
//Create the connection and session
Connection conenction = factory.createConnection();
Session session = connection.createSession(false,
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.