String destinationName = destinationResolver.resolveDestination(eventClass, null);
String subId = null;
if (destinationType == destinationType.DurableTopic) {
if (args == null || "".equals(args[0])) {
observer.onResult(new ExecutionResult(new CougarFrameworkException("Durable subscription requires a subscription Identifier to be set as the zeroth arg!")));
return;
}
subId = args[0].toString();
}
try {
final Session session = getConnection().createSession(false, acknowledgementMode);
final TopicSubscriberPingMonitor pingMonitor = setupPingConsumer(eventName, destinationName, subId, session);
MessageConsumer consumer = createConsumer(session, destinationName, subId);
consumer.setMessageListener(new SubscriptionMessageListener(observer, eventClass, pingMonitor));
subscriptionAdded(observer);
observer.onResult(new ExecutionResult(new DefaultSubscription() {
@Override
public void preClose(CloseReason reason) {
try {
if (pingMonitor != null) {
if (monitorRegistry != null) {
monitorRegistry.removeMonitor(pingMonitor);
}
subscriberMonitorsBySession.remove(pingMonitor);
}
session.close();
} catch (JMSException ex) {
logger.log(Level.SEVERE, "Exception occurred when trying to close JMSConnection", ex);
}
}
}));
} catch (JMSException ex) {
observer.onResult(new ExecutionResult(new CougarFrameworkException("Subscription exception!", ex)));
} catch (CougarException ce) {
observer.onResult(new ExecutionResult(ce));
}
}