|
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 channel has been
located, an event must
be constructed and prior to a publish call being
made to the channel.
For reliable publish, there a 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 multiple
messages in one publish call
Vector messages=new Vector();
Messages.addElement(message1);
Messages.addElement(message2);
Messages.addElement(message3);
myChannel.publish(messages);
// publishiing 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 is a code snippet for transactional publishing:
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 Nirvana session
becomes disconnected, and the commmit 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 transactional were successfully
received by the Nirvana Realm Server. This call can
be made regardless of whether the connection was lost
and a new connection was created.
boolean committed = myTransaction.isCommitted(true);
Which will query the Nirvana Realm Server to see if
the transaction was committed.
An example of reliable publishing events onto a channel
can be found here.
An example of transactional publish can be found here.
For more information on Nirvana publish / subscribe,
please see the API
documentation. |