Consuming JMS Messages Under Local Transaction

  GigaSpaces 5.X

Documentation Home
Quick Start Guide
Release Notes

Previous release

  Search Here
Searching GigaSpaces Platform 5.X Documentation

                                               

Summary: You can create a QueueReceiver or TopicSubscriber and call setMessageListener() to register a listener, or receive() to receive a message.

Overview

Another way to use a transacted session is to consume messages within a local transaction. You can create a QueueReceiver or TopicSubscriber, and call either setMessageListener() to register a message listener, or receive() to receive an individual message. This functions similarly to a non-transacted session.
Since the session is within a transaction, the client only acknowledges that it has consumed such messages when the session is committed.
The code below demonstrates two text messages received synchronously (for example, receive()) from a queue within one local transaction. Only when commit() is performed are the two text messages acknowledged.

// queueConnection is an instance of QueueConnection
// create a transacted QueueSession
boolean isTransacted = true;
int acknowledgeMode = 0;
QueueSession queueSession =
queueConnection.createQueueSession(isTransacted,acknowled
geMode);
// obtain a queue
// ctx is a javax.naming.Context
Queue queue = (Queue)ctx.lookup(queueName);
// create a QueueReceiver on the queue
QueueReceiver queueReceiver =
    queueSession.createReceiver(queue);
// start the JMS connection
queueConnection.start();
// receive a message (and block until it arrives)
Message m1 = queueReceiver.receive()
// receive a second message (and block until it arrives)
Message m2 = queueReceiver.receive()
// commit the transaction - this acknowledges the receipt of
// the messages
queueSession.commit()

In the event of an error, and you don't want to commit the transaction, use call rollback() instead to cease all message sends performed during the transaction. If you choose this option, none of the messages received during the transaction are acknowledged. The JMS provider considers them as not sent and will try to deliver them again. If the client crashes without committing the session is automatically rolled back.

// roll back the transaction
queueSession.rollback()

It's possible to receive messages asynchronously (such as an event listener) within a local transaction similar to synchronous ones. The code below demonstrates two text messages received asynchronously from a queue within one transaction. Only when onMessage() is called a second time is commit() also called. This acknowledges the receipt of the present as well as the previous message.

// queueConnection is an instance of QueueConnection
// create a transacted QueueSession
boolean isTransacted = true;
int acknowledgeMode = 0;
queueSession QueueSession =
queueConnection.createQueueSession
isTransacted,acknowledgeMode);
// obtain a queue
// ctx is a javax.naming.Context
Queue queue = (Queue)ctx.lookup(queueName);
// create a QueueReceiver on the queue
QueueReceiver queueReceiver =
    queueSession.createReceiver(queue);
// start the JMS connection
queueConnection.start();
// initialize the count of messages received
int messagesReceived=0;
// register a message listener to asynchronously receive
messages
queueReceiver.setMessageListener(new MessageListener() {
    public void onMessage(javax.jms.Message message) {
        // increment the count of messages received
        messagesReceived++;
        // process the message
        . . .
        if (messagesReceived=2) {
            // acknowledge this and the previous message
            queueSession.commit()
            // re-initialize the count of messages received
            messagesReceived=0;
        }
    }
});

If the client tries to explicitly acknowledge a message by calling Message.acknowledge() within a local transaction, it will be ignored. This will occur even if the acknowledgeMode parameter was set to Session.CLIENT_ACKNOWLEDGE.


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)