How to publish events to a Nirvana Queue

There are 2 types of publish available in Nirvana for queues:

Reliable Publish is simply a one way push to the Nirvana Server. This means that the server does not send a response to the client to indicate whether the event was successfully received by the server from the publish call.

Transactional Publish involves creating a transaction object to which events are published, and then committing the transaction. The server responds to the transaction commit call indicating if it was successful. There are also means for transactions to be checked for status after application crashes or disconnects.

Reliable Publish

Once the session has been established with the Nirvana realm server and the queue has been located, an event must be constructed prior to a publish call being made to the queue.

The following code snippet shows how to reliably publish events to a queue. Further examples can be found in the API documentation.

// Publishing a simple byte array message
myChannel.publish(new nConsumeEvent("TAG", (new UTF8Encoding()).GetBytes(message)));

// Publishing multiple messages in one publish call
List Messages = new List();
Messages.Add(message1);
Messages.Add(message2);
Messages.Add(message3);
myChannel.publish(Messages);

Transactional Publish

Transactional publishing provides us with a method of verifying that the server receives the events from the publisher, and provides guaranteed delivery.

There are similar prototypes available to the developer for transaction publishing. Once we have established our session and our queue, we then need to construct our events and our transaction, then publish these events to the transaction. The transaction will then be committed and the events available to consumers to the queue.

Below is a code snippet demonstrating ransactional publishing:

List Messages = new List();
Messages.Add(message1);
nTransactionAttributes tattrib = new nTransasctionAttributes(myChannel);
nTransaction myTransaction = nTransactionFactory.create(tattrib);
myTransaction.publish(Messages);
myTransaction.commit();

If during the transaction commit your Nirvana session becomes disconnected, and the commmit call throws an exception, the state of the transaction may be unclear. To verify whether a transaction has been committed or aborted, the transaction can be queried to determine whether the events within the transactional were successfully received by the Nirvana Realm Server:

bool committed = myTransaction.isCommitted(true);

Examples

The following examples show full application source code for reliable and transanctional publishing clients:

For more information on Nirvana Message Queues, please see the API documentation.