Universal Messaging 10.11 | Developer Guide | Enterprise APIs | Enterprise Developer's Guide for C++ | Publish / Subscribe using Channel 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 : public nEventListener {
public void go(nConsumeEvent* evt) {
cout<<"Received an event!";
}
}

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

mySession->subscribe(arr,numChans);

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

public void handleSubscriptionFailure(nSubscriptionAttributes* subAtts){
cout<< subAtts.getException().StackTrace;
}
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();