Point-to-Point Messaging

  GigaSpaces 5.X

Documentation Home
Quick Start Guide
Release Notes

Previous release

  Search Here
Searching GigaSpaces Platform 5.X Documentation

                                               

Summary: Creating a QueueSender, sending messages, creating a QueueReceiver, receiving messages (sync/async), browsing messages.

Creating QueueSender

A 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.

For more details about the parameters and their methods, see Publishing Messages

Sending Messages

After 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 QueueReceiver

A JMS client receives messages from a queue with a QueueReceiver, created from the QueueSession object.
The most conventional method to create a QueueReceiver is:

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)
For more details on JMS message selectors, refer to Message Selectors.

After you've created a QueueReceiver, you can receive messages either synchronously or asynchronously.

Receiving JMS Messages Synchronously

You 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 Asynchronously

To 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 Queues

A 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);
For more details, refer to Message selectors.

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.

Labels

 
(None)