Sending 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: After started a local transaction, you can create a QueueSender or TopicPublisher and send() or publish() messages.

Overview

After you've started a local transaction, through creating a transacted session, you can create a QueueSender or a TopicPublisher. With these methods you can send() or publish() messages, similar to a non-transacted session.
However, since the session is in a transaction, employing the calls QueueSender.send() or TopicPublisher.publish() is not enough to deliver the message. You must commit the transaction with the call Session.commit() method in order to send the messages to the topic or queue.

Such a commit is atomic. In other words, if the commit fails none of the messages are sent. An atomic commit is an "all or nothing" action.

The code below demonstrates two text messages sent to a queue within one local transaction (topics work in a similar way). Only when commit() occurs are the two text messages actually sent.

// 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
// GSJMSAdmin.getQueue is a javax.naming.Context
Queue queue = GSJMSAdmin.getQueue(queueName);
// create a QueueSender on the queue
QueueSender queueSender = queueSession.createSender(queue);
// start the JMS connection
queueConnection.start();
// create a TextMessage
TextMessage tm1 = queueSession.createTextMessage
    ("This is message 1");
// Send the message
queueSender.send(tm1);
// create a second TextMessage
TextMessage tm2 = queueSession.createTextMessage
    ("This is message 2");
// Send the message
queueSender.send(tm2);
// commit the transaction - this actually sends 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 the client crashes without committing the session is automatically rolled back.

// roll back the transaction
queueSession.rollback()

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)