*
* @exception StateException If activating an already active durable subscription.
* @exception RequestException If the subscription parameters are not correct.
*/
private void doReact(ConsumerSubRequest req) throws StateException, RequestException {
AgentId topicId = AgentId.fromString(req.getTarget());
String subName = req.getSubName();
if (topicId == null)
throw new RequestException("Cannot subscribe to an undefined topic (null).");
if (subName == null)
throw new RequestException("Unauthorized null subscription name.");
boolean newTopic = ! topicsTable.containsKey(topicId);
boolean newSub = ! subsTable.containsKey(subName);
TopicSubscription tSub;
ClientSubscription cSub;
// true if a SubscribeRequest has been sent to the topic.
boolean sent = false;
if (newTopic) { // New topic...
tSub = new TopicSubscription();
topicsTable.put(topicId, tSub);
} else { // Known topic...
tSub = (TopicSubscription) topicsTable.get(topicId);
}
if (newSub) { // New subscription...
// state change, so save.
setSave();
cSub = new ClientSubscription(getId(),
activeCtxId,
req.getRequestId(),
req.getDurable(),
topicId,
req.getSubName(),
req.getSelector(),
req.getNoLocal(),
dmqId,
threshold,
nbMaxMsg,
messagesTable);
cSub.setProxyAgent(this);
if (logger.isLoggable(BasicLevel.DEBUG))
logger.log(BasicLevel.DEBUG, "Subscription " + subName + " created.");
subsTable.put(subName, cSub);
try {
MXWrapper.registerMBean(cSub, getSubMBeanName(subName));
} catch (Exception e) {
if (logger.isLoggable(BasicLevel.WARN))
logger.log(BasicLevel.WARN, " - Could not register ClientSubscriptionMbean", e);
}
tSub.putSubscription(subName, req.getSelector());
sent = updateSubscriptionToTopic(topicId, activeCtxId, req.getRequestId(), req.isAsyncSubscription());
} else { // Existing durable subscription...
cSub = (ClientSubscription) subsTable.get(subName);
if (cSub.getActive())
throw new StateException("The durable subscription " + subName + " has already been activated.");
// Updated topic: updating the subscription to the previous topic.
boolean updatedTopic = ! topicId.equals(cSub.getTopicId());
if (updatedTopic) {
TopicSubscription oldTSub =
(TopicSubscription) topicsTable.get(cSub.getTopicId());
oldTSub.removeSubscription(subName);
updateSubscriptionToTopic(cSub.getTopicId(), -1, -1, req.isAsyncSubscription());