String clientID = spec.getClientID();
SimpleString queueName = new SimpleString(HornetQDestination.createQueueNameForDurableSubscription(true, clientID,
subscriptionName));
QueueQuery subResponse = session.queueQuery(queueName);
if (!subResponse.isExists())
{
session.createQueue(activation.getAddress(), queueName, selectorString, true);
}
else
{
// The check for already exists should be done only at the first session
// As a deployed MDB could set up multiple instances in order to process messages in parallel.
if (sessionNr == 0 && subResponse.getConsumerCount() > 0)
{
if (!spec.isShareSubscriptions())
{
throw new javax.jms.IllegalStateException("Cannot create a subscriber on the durable subscription since it already has subscriber(s)");
}
else if (HornetQRALogger.LOGGER.isDebugEnabled())
{
HornetQRALogger.LOGGER.debug("the mdb on destination " + queueName + " already had " +
subResponse.getConsumerCount() +
" consumers but the MDB is configured to share subscriptions, so no exceptions are thrown");
}
}
SimpleString oldFilterString = subResponse.getFilterString();
boolean selectorChanged = selector == null && oldFilterString != null ||
oldFilterString == null &&
selector != null ||
(oldFilterString != null && selector != null && !oldFilterString.toString()
.equals(selector));
SimpleString oldTopicName = subResponse.getAddress();
boolean topicChanged = !oldTopicName.equals(activation.getAddress());
if (selectorChanged || topicChanged)
{
// Delete the old durable sub
session.deleteQueue(queueName);
// Create the new one
session.createQueue(activation.getAddress(), queueName, selectorString, true);
}
}
consumer = (ClientConsumerInternal) session.createConsumer(queueName, null, false);
}
else
{
SimpleString tempQueueName;
if (activation.isTopic())
{
if (activation.getTopicTemporaryQueue() == null)
{
tempQueueName = new SimpleString(UUID.randomUUID().toString());
session.createTemporaryQueue(activation.getAddress(), tempQueueName, selectorString);
activation.setTopicTemporaryQueue(tempQueueName);
}
else
{
tempQueueName = activation.getTopicTemporaryQueue();
QueueQuery queueQuery = session.queueQuery(tempQueueName);
if (!queueQuery.isExists())
{
// this is because we could be using remote servers (in cluster maybe)
// and the queue wasn't created on that node yet.
session.createTemporaryQueue(activation.getAddress(), tempQueueName, selectorString);
}