|
Summary: Creating a QueueSender, sending messages, creating a QueueReceiver, receiving messages (sync/async), browsing messages.
Creating QueueSenderA JMS client sends messages to queue using a QueueSender. The QueueSender is created from the QueueSession object with the method createSender(). This method has one argument; usually a Queue object which may be null. // queueSession is an instance of QueueSession // queue is an instance of Queue QueueSender sender = queueSession.createSender(queue); After you've created a QueueSender, you can configure the default values for deliveryMode, priority, and timeToLive parameters for all messages published from this QueueSender.
Sending MessagesAfter you've obtained a QueueSender, you can send messages with one of the send() methods. The most common methods are: void send(Message message) void send(Message message,int deliveryMode, int priority, long timeToLive) // sender is an instance of QueueSender // message is an instance of Message (or one of its subtypes) sender.send(message); If the QueueSender was developed using a null topic it's called an 'unidentified producer'. In such a case, you must supply the queue for every message published using one of the following methods: void publish(Queue queue, Message message) void publish(Queue queue, Message message,int deliveryMode, int priority, long timeToLive) If you use methods with the deliveryMode, priority, and timeToLive parameters, these override any defaults configured on the QueueSender. Creating QueueReceiverA JMS client receives messages from a queue with a QueueReceiver, created from the QueueSession object. QueueReceiver createReceiver(Queue queue) // queueSession is an instance of QueueSession // queue is an instance of Queue QueueReceiver receiver = queueSession.createReceiver(queue); To specify a message selector, use the following text string. QueueReceiver createReceiver(Queue queue, String
messageSelector)
After you've created a QueueReceiver, you can receive messages either synchronously or asynchronously. Receiving JMS Messages SynchronouslyYou can receive messages synchronously by calling one of the receive() methods on the TopicSubscriber. The first method blocks any further incoming messages until either the message is received or a timeout expires: Message receive() throws JMSException The examples below demonstrate parameters on the receive() method. With this parameter, you can receive the next message. This call blocks messages indefinitely until a message is produced. // subscriber is an instance of TopicSubscriber Message message = subscriber.receive(); Message receive(long timeout) throws JMSException To specify the next message to receive that arrives within the specified timeout interval (given in milliseconds), use the following parameter. This call blocks until either a message arrives or the timeout expires. When the timeout is zero, it never expires and the call blocks indefinitely. Message receiveNoWait() throws JMSException This parameter receives the next message, if one is immediately available. If a message is not immediately available, this call returns null. Receiving JMS Messages AsynchronouslyTo receive a message asynchronously, create an object that implements the MessageListener interface and register it with the TopicSubscriber using the method setMessageListener(). See the example below: void setMessageListener(MessageListener listener) The MessageListener interface describes one method, onMessage(), called whenever a message arrives. // subscriber is an instance of TopicSubscriber
subscriber.setMessageListener(new MessageListener() {
public void onMessage(javax.jms.Message message) {
// process the message
}
});
Browsing JMS QueuesA client application might create a QueueBrowser to examine the messages on a queue without actually deleting them. The QueueBrowser contains a method, getEnumeration(), which returns an enumeration of the queue's messages: QueueBrowser browser=session.createBrowser(queue); Enumeration enum=browser.getEnumeration(); while (enum.hasMoreElements()) { System.out.println("Message on queue is: "+iter.nextElement()); } Another possibility is to specify a message selector. With a message selector, the Enumeration only contains messages that satisfy conditions previously defined in the message selector: String selector="price BETWEEN 10 and 100"; QueueBrowser browser=session.createBrowser(queue,selector);
JMS specifications do not define whether the QueueBrowser represents a snapshot of the queue or dynamically updates it. However, with the Enterprise Messaging Grid, a snapshot is taken when the call is made to getEnumeration(). |
Wiki Content Tree
Your Feedback Needed!
We need your help to improve this wiki site. If you have any suggestions or corrections, write to us at techw@gigaspaces.com. Please provide a link to the wiki page you are referring to.
Add Comment