use of com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore in project openmq by eclipse-ee4j.
the class BrokerConsumers method addConsumer.
public void addConsumer(Consumer c) throws BrokerException {
if (getDEBUG()) {
logger.log(logger.INFO, "BrokerConsumers.addConsumer: " + c);
}
com.sun.messaging.jmq.jmsserver.core.ConsumerUID cuid = c.getConsumerUID();
if (consumers.get(cuid) != null) {
String emsg = Globals.getBrokerResources().getKString(BrokerResources.I_CONSUMER_ALREADY_ADDED, cuid, c.getDestinationUID());
logger.log(logger.INFO, emsg + " (CLUSTER_ROUTER)");
throw new ConsumerAlreadyAddedException(emsg);
}
DL.acquirePartitionLock(true);
try {
if (!(c instanceof Subscription)) {
consumers.put(cuid, c);
pendingConsumerUIDs.put(cuid, null);
listeners.put(cuid, c.addEventListener(this, EventType.BUSY_STATE_CHANGED, null));
}
DestinationUID duid = c.getDestinationUID();
int type = (duid.isQueue() ? DestType.DEST_TYPE_QUEUE : DestType.DEST_TYPE_TOPIC);
Destination d = null;
try {
if (!duid.isWildcard()) {
for (int i = 0; i < 2; i++) {
Destination[] ds = DL.getDestination(Globals.getStore().getPrimaryPartition(), duid.getName(), type, true, true);
d = ds[0];
try {
// is not removed by autocreate prematurely
if (d != null) {
d.incrementRefCount();
// well we should break anyway, but
break;
// this makes it explicit
}
} catch (BrokerException ex) {
// incrementRefCount throws a BrokerException
// if the destination was destroyed
// if we are here then the destination went away
// try to get the destination again
d = null;
}
}
if (d == null) {
throw new BrokerException("Unable to attach to destination " + duid);
}
}
} catch (IOException ex) {
throw new BrokerException("Unable to autocreate destination " + duid, ex);
}
try {
// before we exit (so cleanup can occur)
if (!c.getDestinationUID().isQueue() && (!(c instanceof Subscription)) && c.getSubscription() == null) {
// directly send messages
c.setFalconRemote(true);
} else {
int mp = (d == null ? -1 : d.getMaxPrefetch());
if (mp <= 0 || mp > BTOBFLOW) {
mp = BTOBFLOW;
}
int prefetch = c.getRemotePrefetch();
if (prefetch <= 0 || prefetch > mp) {
prefetch = mp;
}
Subscription sub = c.getSubscription();
if (sub != null && sub.getShared()) {
prefetch = 1;
}
c.setPrefetch(prefetch);
}
try {
if (d == null && c.getSubscription() == null) {
// deal with wildcard subscription
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, c.getDestinationUID());
LinkedHashSet dset = null;
Destination dd = null;
Iterator<LinkedHashSet<Destination>> itr = dmap.values().iterator();
while (itr.hasNext()) {
dset = itr.next();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
dd = itr1.next();
if (dd == null) {
continue;
}
try {
dd.addConsumer(c, false);
} catch (ConsumerAlreadyAddedException e) {
logger.log(logger.INFO, e.getMessage() + " (CLUSTER_ROUTER)");
}
}
}
} else if (c.getSubscription() == null) {
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, c.getDestinationUID());
LinkedHashSet dset = null;
Destination dd = null;
Iterator<LinkedHashSet<Destination>> itr = dmap.values().iterator();
while (itr.hasNext()) {
dset = itr.next();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
dd = itr1.next();
if (dd == null) {
continue;
}
try {
dd.addConsumer(c, false);
} catch (ConsumerAlreadyAddedException e) {
logger.log(logger.INFO, e.getMessage() + " (CLUSTER_ROUTER)");
}
}
}
}
} catch (SelectorFormatException ex) {
throw new BrokerException("unable to add destination " + d, ex);
}
if (!(c instanceof Subscription)) {
if (c.isBusy()) {
synchronized (activeConsumers) {
activeConsumers.add(c);
activeConsumers.notifyAll();
}
}
}
} finally {
// processing the add consumer
if (d != null) {
// not wildcard
d.decrementRefCount();
}
}
} finally {
DL.releasePartitionLock(true);
}
}
Aggregations