Glossary:
Nirvana J2ME : Publishing Events to a Channel Transactionally
Once the session has been established with the Nirvana realm server, and the channel has been located, a new Nirvana Event object (nConsumeEvent) must be constructed prior to use in the publish or transactional publish call being made to the channel.
Note that in this example code, we also create a Nirvana Event Dictionary object for our Nirvana Event before publishing it transactionally:
// Create a transaction attributes nObject specifying the channel and
// transaction time to live (TTL)
nObject transactionattributes = nObject.createTransactionAttributes(myRatesChannel,60000);
// Create a transaction nObject based on the above transaction attributes
nObject transaction = nObject.createTransaction(transactionattributes);
// Obtain a reference to the transaction ID
long txID=nObject.getTXID(transaction);
// Create the event properties
nEventProperties props=new nEventProperties();
String value = "Hello World";
props.put("key1",value);
// Create the event
nObject evt= nObject.createConsumeEvent(props,"".getBytes());
// Create a vector to contain all events that this transaction will consist of
Vector eventsCollection=new Vector();
// Add the event to the collection
eventsCollection.addElement(evt);
// Publish the event collection transactionally
nObject.publishTransactionally(transaction,eventsCollection);
// Commit the TX
nObject.commitTransaction(transaction);
if (nObject.isTransactionCommitted(myRatesChannel,txID)){
System.out.println("Transaction is committed")
}
To publish transactionally, we first create the transaction attributes using the channel (myRatesChannel) and a Time To Live (TTL). The TTL indicates how long the server will keep information about this transaction's state. Using the transaction attributes object we then create a transaction nObject which we use to publish a collection of events.
When we are ready, we commit the transaction and the event collection is made available to subscribers. At any point we can query the status of the transaction to see if it has been committed.
