Publishing Events to a Channel
There are 2 types of publish available in Universal Messaging for channels:
Reliable Publish is simply a one way push to the Universal Messaging 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 Universal Messaging realm server and the channel has been located, an event must be constructed prior to a publish call being made to the channel.
For reliable publish, there are a number of method prototypes on a channel that allow us to publish different types of events onto a channel. Here are examples of some of them. 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 a dictionary (nEventProperties)
nEventProperties props = new nEventProperties();
props.put("bondname", "bond1");
props.put("price", 100.00);
nConsumeEvent evt = new nConsumeEvent("atag", props);
myChannel.publish(evt);
// Publishing multiple messages in one publish call
List<nConsumeEvent> Messages = new List<nConsumeEvent>();
Messages.Add(message1);
Messages.Add(message2);
Messages.Add(message3);
myChannel.publish(Messages);
Transactional Publish
Transactional publishing provides a means of verifying that the server received the events from the publisher, and therefore provides guaranteed delivery.
There are similar prototypes available to the developer for transactional publishing. Once the session is established and the channel located, we then need to construct the events for the transaction and publish these events to the transaction. Only when the transaction has been committed will the events become available to subscribers on the channel.
Below is a code snippet for transactional publishing:
//Publishing a single event in a transaction
nTransactionAttributes attrib=new nTransactionAttributes(myChannel);
nTransaction myTransaction=nTransactionFactory.create(attrib);
myTransaction.publish(new nConsumeEvent("TAG", new UTF8Encoding()).GetBytes(message)));
myTransaction.commit();
//Publising multiple events in a transaction
List<nConsumeEvent> Messages = new List<nConsumeEvent>();
Messages.Add(message1);
nTransactionAttributes tattrib = new nTransactionAttributes(myChannel);
nTransaction myTransaction = nTransactionFactory.create(tattrib);
myTransaction.publish(Messages);
myTransaction.commit();
If during the transaction commit your Universal Messaging session becomes disconnected, and the commit call throws an exception, the state of the transaction may be unclear. To verify that a transaction has been committed or aborted, a call can be made on the transaction that will determine if the events within the transaction were successfully received by the Universal Messaging Realm Server. This call can be made regardless of whether the connection was lost and a new connection was created.
The following code snippet demonstrates how to query the Universal Messaging Realm Server to see if the transaction was committed:
bool committed = myTransaction.isCommitted(true);