Universal Messaging 10.3 | Developer Guide | Enterprise APIs | Enterprise Developer's Guide for Java | Publish / Subscribe Using Channels/Topics | Batched Subscribe
 
Batched Subscribe
If a client application needs to subscribe to multiple channels it is more efficient to batch these subscriptions into a single server call. This is achieved using the subscribe method of nSession rather than first finding the nChannel object and then calling the subscribe method of nChannel.
The following code snippet demonstrates how to subscribe to two Universal Messaging channels in one server call:

public class myEventListener implements nEventListener {
public void go(nConsumeEvent evt) {
System.out.println("Received an event!");
}
}

public void demo(){
nSubscriptionAttributes[] arr = new nSubscriptionAttributes[2];
arr[0] = new nSubscriptionAttributes("myChan1", "", 0, myLis1);
arr[1] = new nSubscriptionAttributes("myChan2", "", 0, myLis2);

arr = mySession.subscribe(arr);

for (int i = 0; i < arr.length; i++) {
if (!arr[i].wasSuccessful()) {
handleSubscriptionFailure(arr[i]);
}
//subscription successful
}
}

public void handleSubscriptionFailure(nSubscriptionAttributes subAtts){
subAtts.getException().printStackTrace();
}
The nSubscriptionAttributes class is used to specify which channels to subscribe to. The second two parameters of the constructor represent the selector to use for the subscription and the event ID to subscribe from.
It is possible that the subscription may fail; for example, the channel may not exist or the user may not have the required privileges. In this situation, calling wasSuccessful() on the nSubscriptionAttributes will return false and getException() will return the exception that was thrown.
If the subscription is successful then the nChannel object can be obtained from the nSubscriptionAttributes as shown in the following code snippet:

nChannel chan = subAtts.getChannel();