Durable Channel Consumers and Named Objects (Deprecated)
Note: The API described in this section for creating
named objects and
named objects with priority has now been deprecated and will be removed from the product in an upcoming release. If you are developing new applications, use the API described in the section
Using Durable Objects instead.
Universal Messaging provides the ability for both asynchronous and synchronous consumers to be durable. Durable consumers allow state to be kept at the server with regard to what events have been consumed by a specific consumer of data.
Universal Messaging supports durable consumers through use of Universal Messaging named objects (see
Named Objects) as shown by the following example code.
Named objects can also be managed via the Enterprise Manager.
Asynchronous
An example of how to create a named object that begins from event id 0, is persistent and is used in conjunction with an asynchronous event consumer:
public class mySubscriber : nEventListener {
public mySubscriber() {
// construct your session and channel objects here
// create the named object and begin consuming events
// from the beginning of the channel (event id 0)
nNamedObject nobj = myChannel.createNamedObject("unique1", 0, true);
myChannel.addSubscriber(this , nobj);
}
public void go(nConsumeEvent event) {
Console.WriteLine("Consumed event "+event.getEventID());
}
public static void Main(String[] args) {
new mySubscriber();
}
}
Synchronous
An example of how to create a named object that begins from event id 0, is persistent and is used in conjunction with a synchronous event consumer:
public class myIterator {
nChannelIterator iterator = null;
public myIterator() {
// construct your session and channel objects here
// start the iterator
nNamedObject nobj = myChannel.createNamedObject("unique2", 0, true);
iterator = myChannel.createIterator(0);
}
public void start() {
while (true) {
nConsumeEvent event = iterator.getNext();
go(event);
}
}
public void go(nConsumeEvent event) {
Console.WriteLine("Consumed event "+event.getEventID());
}
public static void Main(String[] args) {
myIterator itr = new myIterator();
itr.start();
}
}
Both synchronous and asynchronous channel consumers allow message selectors to be used in conjunction with named objects. Please see the API documentation for more information.
There are also different ways in which events consumed by named consumers can be acknowledged. By specifying that 'auto acknowledge' is true when constructing either the synchronous or asynchronous consumers, then each event is acknowledged as consumed automatically. If 'auto acknowledge' is set to false, then each event consumed has to be acknowledged by calling the ack() method:
public void go(nConsumeEvent event) {
Console.WriteLine("Consumed event " + event.getEventID());
event.ack();
}
Priority
Two subscribers can hold a subscription to the same named object. One is given priority and will process events during normal operation. If, however, the subscriber with priority is disconnected for whatever reason, and is unable to process events, the second subscriber to that named object will take over and continue to process events as they come in. This allows failover, with backup subscribers handling events if the subscriber with priority goes down.
To do this, we simply create the subscriber with a boolean specifying if this subscriber has priority. Only one subscriber is allowed priority at any given time. An example of a named object specifying priority is shown below:
nNamedObject named = myChannel.createNamedObject(
subname, startEid, persistent, cluster, priority);
Named Objects
Universal Messaging provides the ability for the server to maintain state for the last event that was consumed by a consumer on a channel. By providing a unique name, you can create a named object on a channel and even when your application is stopped, the next time you start your application, you will only consume available events from the last event id that the server stored as successfully consumed by that named object.
Named objects can be persistent, i.e. the last event id is written to disk, so that if the Universal Messaging Realm Server is restarted, the last event id consumed is retrievable for each named object on a channel.
An example of how to create a named object that begins from event id 0, and is persistent is shown below:
nNamedObject nobj = myChannel.createNamedObject("unique1", 0, true);