Message Consumers

Message consumers are entities used by JMS clients for receiving messages. Message consumers used in PTP messaging are called queue receivers and those used in Pub/Sub model are called topic subscribers. JMS sessions act as factories for creating message consumers. Queue sessions are used for creating queue receivers and topic sessions are used for creating topic subscribers.

Message consumers can receive messages both synchronously asynchronously. The interface for message consumers is defined by javax.jms.MessageConsumer. The interface javax.jms.QueueReceiver defines the methods for PTP message consumers and the interface javax.jms.TopicSubscriber defines the methods for Pub/Sub message consumers.

Asynchronous Message Delivery

Message consumers can receive messages asynchronously by registering message listeners. All the message consumers associated with a single session can have different message listeners, but message delivery to these listeners is serialized. If a message listener is registered globally for the session as described in one of the earlier sections, messages intended for all the session's consumers are handled by the session itself. The interface javax.jms.MessageConsumer defines methods for setting and getting message listeners:

     public void setMessageListener(MessageListener listener) throws JMSException     public MessageListener getMessageListener() throws JMSException 

Synchronous Message Delivery

Message consumers achieve synchronous message delivery by making blocking method calls to the receive methods defined in the interface javax.jms.MessageConsumer. A call made to this method will block the thread until a message is available in the destination associated with the consumer:

     public Message receive() throws JMSException 

The next method is the same as the one above except for the fact that the client thread is blocked until a message is available or the number of milliseconds specified in the argument, whichever is the earliest:

     public Message receive(long timeout) throws JMSException 

The final receive method defined in javax.jms.MessageConsumer retrieves a message if one is immediately available:

     public Message receiveNoWait() throws JMSException 

All the above methods throw JMSException if the provider encounters any internal error. These methods don't throw an exception if there are no messages available to be delivered.



Professional JMS
Professional JMS
ISBN: 1861004931
EAN: 2147483647
Year: 2000
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net