Universal Messaging JavaScript : Asynchronous Transactional Queue Consuming
Transactional queue consumers have the ability to notify the server when events have been consumed (committed) or when they have been discarded (rolled back). This ensures that the server does not remove events from the queue unless notified by the consumer with a commit or rollback.
Subscribing as a Transactional Reader
This JavaScript code snippet demonstrates how to subscribe to a queue as a transactional queue reader:
var demoSession = Nirvana.createSession();
var demoQueue = demoSession.getQueue ("/some/demo/queue");
demoQueue.on(Nirvana.Observe.DATA,
function(event) {
// define what to do when we receive an event
});
You can specify the transaction flag and the window size as follows:
var demoQueue = mySession.getQueue ("/some/demo/queue", true);
// The true flag specifies that we are a transactional reader
demoQueue.setWindowSize(10); // 10 is the windowSize
demoQueue.subscribe();
Performing a Commit
As previously mentioned, the big difference between a transactional reader and a standard queue reader is that once events are consumed by the reader, the consumers need to commit the events consumed. Events will only be removed from the queue once the commit has been called.
The server will only deliver up to the specified windowSize number of events. After this the server will not deliver any more events to the client until commit has been called. The default windowSize is 5.
The JavaScript libraries provide two methods for committing events which have been consumed. demoQueue.commitAll() will commit every event which this consumer has received thus far, but has not previously committed. When the server receives this message, all these events will be removed. demoQueue.commit(event) will commit the given event and any uncommitted events occurring before.
demoQueue.on(Nirvana.Observe.DATA,
function(event) {
// process the event
demoQueue.commit(event); // Commit the event
});
Performing a Rollback
Developers can also rollback events received by the transactional reader. Uncommitted events will be redelivered by the server (possibly to other queue consumers if they exist).
The JavaScript libraries provide two methods for performing a rollback. demoQueue.rollbackAll() will roll back all previously uncommitted events which the consumer has received. demoQueue.rollback(event) will perform a rollback on all events starting from the given event.