Glossary:
Nirvana JavaScript : Transactional Publish
Transactional publishing provides a means of verifying that the server has received events from the publisher, and therefore provides guaranteed delivery. Clients can publish using transactions to both channels and queues in Javascript.
Transactions can be created by the user from a Nirvana Event object and a Nirvana Transaction Attributes object. The event can then be published through the transaction object to the server.
var event = new Nirvana.nConsumeEvent();
event.setData("Hello World");
var txAttributes = new NirvanaTransactionAttributes("channelName");
var transaction = new NirvanaTransaction(txAttributes,event);
transaction.publish();
Transactions contain two asynchronous callback methods; transaction.onCommit() and transaction.onError(error). These can be implemented by the developer similar to those methods attached to channels and queues.
The transaction.onCommit() method is invoked after a successful publish request once the client receives confirmation from the server that the event has been published.
transaction.onCommit = function() { document.write("Transaction Committed!"; };
The transaction.onError(error) is invoked when the client receives confirmation from the server that a problem occurred resulting in the event not being published.
transaction.onError = function(error) {
document.write("Transaction Error: " + error.name + " (" + error.message + ")";
};
In scenarios where a problem occurs, the client may not receive either of these callbacks. This may be either due to server side or client side failure. In these scenarios the state of the transaction from the clients perspective is ambiguous.
By invoking the transaction.isCommitted(callback, queryServer) the client will attempt to resolve the state of the server. It will first attempt to do this locally; if it can do this the method will instantaneously invoke the callback method with the transaction status. If it cannot do this and queryServer is set to true, it will contact the server for confirmation and pass this confirmation to the callback method. If queryServer is set to false it will immediately invoke the callback with the failure status.
var myCallback = function(status) {
document.write("Commit status returned as: " + status);
}
transaction.isCommitted(myCallback,true);
Transaction Attributes
When creating a NirvanaTransaction the client provides a NirvanaTransactionAttributes object. These attribute objects can be reused across multiple transactions. To create a NirvanaTransactionAttributes object you supply the name of the channel or queue which any transactions will be published to:
var myTxAttribs = new NirvanaTransactionalAttributes("channelName");
The client can also use the attributes object to specify the time-to-live of events published to the server in transactional form. This is specified in milliseconds and can be set by the following code:
myTxAttribs.setTTL(1000);
