Universal Messaging Java - How to publish events to a Universal Messaging 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", message.getBytes()));
//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 an XML document
InputStream is = new FileInputStream( aFile);
DOMParser p = new DOMParser();
p.parse( new InputSource( is ) );
Document doc = p.getDocument();
myChannel.publish( "XML", doc );
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 are some code snippets for transactional publishing:
//Publishing a single event in a transaction
nTransactionAttributes tattrib=new nTransasctionAttributes(myChannel);
nTransaction myTransaction=nTransactionFactory.create(tattrib);
myTransaction.publish(new nConsumeEvent("TAG", message.getBytes()));
myTransaction.commit();
//Publising multiple events in a transaction
Vector messages=new Vector();
messages.addElement(message1);
nTransactionAttributes tattrib=new nTransasctionAttributes(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:
boolean committed = myTransaction.isCommitted(true);