Monday, July 27, 2009

Key Interfaces in EMS - MessageProducer, MessageConsumer and MessageListner

MessageProducer:



A MessageProducer object is created by a Session object and is used for sending messages to destinations. For example, using the mySession object created in Session , the following creates a MessageProducer that sends messages to a queue named myQueue:

MessageProducer myQueueSender = mySession.createProducer(myQueue);



Once you have created a MessageProducer, you can use it to send messages. For example, the following sends a message using the queueSender created above:


myQueueSender.send(message);



You can create MessageProducers that do not identify a destination. When the sender or publisher does not specify a destination, you must specify the destination when you send or publish a message as the first parameter of the send() or publish() method.



MessageConsumer:



A MessageConsumer object is created by a Session object and is used for receiving messages sent to destinations. For example, using the mySession object, the following creates a MessageConsumer that retrieves messages from a queue named myQueue:



MessageConsumer myQueueReceiver =
mySession.createConsumer(myQueue);



For queues, messages remain on the queue until they are consumed by a MessageConsumer, the message expiration time has been reached, or the maximum size of the queue is reached.



a) Durable Subscribers for Topics: Only MessageConsumers whose client applications are running receive messages published on a topic. Optionally, Sessions can create durable subscribers to ensure that messages are received, even if the application is not currently running.



(b) Synchronous or Asynchronous Messages: The API allows for synchronous or asynchronous message consumption. For synchronous consumption, the MessageConsumer explicitly calls the receive() method on the topic or queue. For asynchronous consumption, the client registers a MessageListener for the topic or queue. When a message arrives at the destination, the TIBCO Enterprise Message Service server delivers the message by calling the listener’s onMessage() method.



MessageListner:

A MessageListener object acts as an asynchronous event handler for messages. This object implements the MessageListener interface and has one method, onMessage().The onMessage() method is called by the TIBCO Enterprise Message Service server when a message arrives on a destination. You implement the onMessage() method in your MessageListener class to perform the desired actions when a message arrives. Your implementation should handle all exceptions, and it should not throw any exceptions.



Once you create a MessageListener object, you must register it with a specific MessageConsumer. The following creates a queueListener (an implementation of the MessageListener interface) and registers it with the QueueReceiver object:



MessageListener queueListener = new MessageListener();
myQueueReceiver.setMessageListener(queueListener);



You should register the MessageListener with the MessageConsumer before calling the Connection’s start () method to begin receiving messages.

0 comments:

Popular Posts

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP