use of com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException in project openmq by eclipse-ee4j.
the class ConsumerHandler method _createConsumer.
private Consumer[] _createConsumer(DestinationUID dest_uid, IMQConnection con, Session session, String selectorstr, String clientid, String subscriptionName, boolean durable, boolean shared, boolean jmsshare, boolean nolocal, int size, String consumerString, boolean isIndemp, boolean useFlowControl) throws BrokerException, SelectorFormatException, IOException {
Consumer c = null;
Consumer newc = null;
Subscription sub = null;
String selector = selectorstr;
if (selectorstr != null && selectorstr.trim().length() == 0) {
selector = null;
}
try {
int prefetch = -1;
if (isIndemp) {
// see if we already created it
c = Consumer.getConsumer(consumerString);
if (c != null) {
prefetch = c.getPrefetch();
}
}
if (c == null) {
c = new Consumer(dest_uid, selector, nolocal, con.getConnectionUID());
c.setCreator(consumerString);
newc = c;
newc.pause("Consumer: new consumer");
// OK, determine if we are a wildcard or not
Destination[] ds = DL.getDestination(con.getPartitionedStore(), dest_uid);
// PART
Destination d = ds[0];
boolean wildcard = dest_uid.isWildcard();
// NOTE: if d == null, wildcard == true
int cprefetch = size;
int dprefetch = (wildcard ? -1 : (!shared ? d.getMaxPrefetch() : d.getSharedConsumerFlowLimit()));
prefetch = (dprefetch == -1) ? cprefetch : (cprefetch == -1 ? cprefetch : (cprefetch > dprefetch ? dprefetch : cprefetch));
c.setPrefetch(prefetch, useFlowControl);
// actual subscription added to the destination
if (subscriptionName != null && durable) {
if (!shared) {
logger.log(Logger.INFO, br.getKString(br.I_CREATE_UNSHARED_DURA, Subscription.getDSubLogString(clientid, subscriptionName), dest_uid));
} else {
logger.log(Logger.INFO, br.getKString(br.I_CREATE_SHARED_DURA, Subscription.getDSubLogString(clientid, subscriptionName) + (jmsshare ? "jms" : "mq"), dest_uid));
}
// durable
// get the subscription ... this may throw
// an exception IF we cant
sub = Subscription.findCreateDurableSubscription(clientid, subscriptionName, shared, jmsshare, dest_uid, selector, nolocal, true);
sub.setCreator(consumerString);
sub.pause("Consumer attaching to durable");
// add the consumer .. this may throw an
// exception IF
sub.attachConsumer(c, con);
c.localConsumerCreationReady();
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
LinkedHashSet dset = null;
Iterator<LinkedHashSet<Destination>> itr = dmap.values().iterator();
boolean notify = true;
while (itr.hasNext()) {
dset = itr.next();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
Destination dd = itr1.next();
if (dd == null) {
continue;
}
Subscription oldsub = null;
try {
oldsub = (Subscription) dd.addConsumer(sub, notify, con);
} catch (ConsumerAlreadyAddedException e) {
logger.log(logger.INFO, e.getMessage());
}
if (oldsub != null) {
oldsub.purge();
}
}
notify = false;
}
sub.sendCreateSubscriptionNotification(c);
} else if ((wildcard || !d.isQueue()) && shared) {
logger.log(Logger.INFO, br.getKString(br.I_CREATE_SHARED_NONDURA_SUB, Subscription.getNDSubLongLogString(clientid, dest_uid, selectorstr, subscriptionName, nolocal), "" + dest_uid));
sub = Subscription.createAttachNonDurableSub(c, con, subscriptionName, shared, jmsshare);
c.localConsumerCreationReady();
if (sub != null) {
sub.pause("Consumer: attaching to nondurable");
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
LinkedHashSet dset = 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()) {
Destination dd = itr1.next();
if (dd != null) {
dd.addConsumer(sub, true, con);
}
}
}
}
c.attachToConnection(con.getConnectionUID());
if (sub != null) {
sub.sendCreateSubscriptionNotification(c);
}
} else {
c.localConsumerCreationReady();
// non-durable
Destination dd = null;
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
LinkedHashSet dset = 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) {
dd.addConsumer(c, true, con);
}
}
}
c.attachToConnection(con.getConnectionUID());
c.sendCreateConsumerNotification();
}
}
if (fi.FAULT_INJECTION) {
// e.g. imqcmd debug fault -n consumer.add.1 -o selector="mqDestinationName = 'T:t0.*'" -debug
HashMap fips = new HashMap();
fips.put(FaultInjection.DST_NAME_PROP, DestinationUID.getUniqueString(dest_uid.getName(), dest_uid.isQueue()));
fi.checkFaultAndSleep(FaultInjection.FAULT_CONSUMER_ADD_1, fips);
}
session.attachConsumer(c);
if (DEBUG) {
logger.log(logger.INFO, "Attached consumer " + c + "[prefetch=" + c.getPrefetch() + ", useConsumerFlowControl=" + useFlowControl + "] to session " + session);
}
Consumer[] retc = new Consumer[3];
retc[0] = c;
retc[1] = newc;
retc[2] = sub;
return retc;
} catch (Exception e) {
Object[] args = { (durable ? Subscription.getDSubLogString(clientid, subscriptionName) : (shared ? Subscription.getNDSubLongLogString(clientid, dest_uid, selector, subscriptionName, nolocal) : "")), con, dest_uid };
String emsg = Globals.getBrokerResources().getKString(BrokerResources.W_ADD_AUTO_CONSUMER_FAILED, args);
logger.logStack(logger.ERROR, emsg, e);
try {
if (c != null) {
try {
session.detatchConsumer(c.getConsumerUID(), null, false, false, false);
} catch (Exception e1) {
try {
c.destroyConsumer((new HashSet()), null, true, false, true);
} catch (Exception e2) {
}
}
}
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DL.findMatchingDestinationMap(null, dest_uid);
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 {
if (c != null) {
dd.removeConsumer(c.getConsumerUID(), true);
}
if (sub != null) {
dd.removeConsumer(sub.getConsumerUID(), true);
}
} catch (Exception e1) {
}
}
}
try {
if (sub != null && c != null) {
sub.releaseConsumer(c.getConsumerUID());
}
} catch (Exception e1) {
}
if (subscriptionName != null && durable) {
if (sub != null && sub.getCreator() != null && sub.getCreator().equals(consumerString)) {
try {
Subscription.unsubscribe(subscriptionName, clientid);
} catch (Exception e1) {
}
}
}
} catch (Exception e3) {
}
if (e instanceof BrokerException) {
throw (BrokerException) e;
}
if (e instanceof IOException) {
throw (IOException) e;
}
if (e instanceof SelectorFormatException) {
throw (SelectorFormatException) e;
}
throw new BrokerException(emsg, e);
}
}
use of com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException in project openmq by eclipse-ee4j.
the class Subscription method attachConsumer.
public void attachConsumer(Consumer consumer, Connection conn) throws BrokerException {
logger.log(Logger.DEBUG, "Attaching Consumer " + consumer + " to durable " + this + " with " + msgs.size() + " msgs " + getDestinationUID() + "[" + getConsumerUID() + "]");
synchronized (subLock) {
if (activeConsumers.get(consumer.getConsumerUID()) != null) {
throw new ConsumerAlreadyAddedException(Globals.getBrokerResources().getKString(BrokerResources.I_CONSUMER_ALREADY_ADDED, consumer.getConsumerUID(), consumer.getDestinationUID()));
}
if (maxNumActiveConsumers == 1) {
ConsumerUID kidc = consumer.getConsumerUID();
uid.setConnectionUID(kidc.getConnectionUID());
conuid = kidc.getConnectionUID();
} else {
if (!activeConsumers.isEmpty() && consumer.noLocal != noLocal) {
throw new IllegalStateException("nolocal must match on all consumers");
}
}
if (maxNumActiveConsumers != -1 && (activeConsumers.size() >= maxNumActiveConsumers)) {
String[] args = { getDestinations().toString(), this.toString(), String.valueOf(maxNumActiveConsumers), String.valueOf(activeConsumers.size()) };
throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.X_TOO_MANY_SHARED, args), BrokerResources.X_TOO_MANY_SHARED, (Throwable) null, Status.CONFLICT);
}
boolean wasActive = isActive();
consumer.setStoredConsumerUID(getConsumerUID());
consumer.getConsumerUID().setShouldStore(true);
activeConsumers.put(consumer.getConsumerUID(), consumer);
if (msgsSubset == null) {
msgsSubset = msgs.subSet((Filter) null);
}
consumer.setParentList(new NoPersistPartitionedStoreImpl(getStoredConsumerUID()), msgsSubset);
consumer.setSubscription(this);
// OK - get all matching destinations
active = !activeConsumers.isEmpty();
Map<PartitionedStore, LinkedHashSet<Destination>> dmap = DestinationList.findMatchingDestinationMap(null, getDestinationUID());
LinkedHashSet dset = null;
for (Map.Entry<PartitionedStore, LinkedHashSet<Destination>> pair : dmap.entrySet()) {
dset = pair.getValue();
if (dset == null) {
continue;
}
Iterator<Destination> itr1 = dset.iterator();
while (itr1.hasNext()) {
Destination d = itr1.next();
if (d == null) {
continue;
}
if (isActive() && !wasActive) {
if (!d.isLoaded()) {
logger.log(Logger.DEBUG, "Loading " + d);
try {
d.load();
} catch (BrokerException e) {
logger.logStack(Logger.ERROR, e.getMessage() + " [" + pair.getKey() + "]", e);
}
}
}
d.notifyConsumerAdded(consumer, conn);
}
}
}
}
use of com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException in project openmq by eclipse-ee4j.
the class ConsumerIterator method next.
/**
* Caller must catch RuntimeException and getCause
*/
@Override
public Object next() throws RuntimeException {
try {
Consumer c = ClusterConsumerInfo.readConsumer(dis);
Integer prefetch = (Integer) gp.getProp(c.getConsumerUID().longValue() + ":" + Consumer.PREFETCH);
if (prefetch != null) {
c.setRemotePrefetch(prefetch.intValue());
}
if (from != null) {
c.getConsumerUID().setBrokerAddress(from);
}
count_read++;
return c;
} catch (IOException e) {
Throwable ex = e.getCause();
if (ex instanceof ConsumerAlreadyAddedException) {
count_read++;
throw new RuntimeException(ex);
}
count_read = -1;
throw new RuntimeException(e);
}
}
use of com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException 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);
}
}
use of com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException in project openmq by eclipse-ee4j.
the class ClusterConsumerInfo method readConsumer.
public static Consumer readConsumer(DataInputStream dis) throws IOException {
Logger logger = Globals.getLogger();
ConsumerUID id = null;
String destName = null;
String clientID = null;
String durableName = null;
String selstr = null;
boolean isQueue;
boolean noLocalDelivery;
// boolean consumerReady;
int sharedcnt;
int position;
// version
long ver = dis.readLong();
if (ver != ConsumerVersionUID) {
throw new IOException("Wrong Consumer Version " + ver + " expected " + ConsumerVersionUID);
}
destName = dis.readUTF();
boolean hasId = dis.readBoolean();
if (hasId) {
id = readConsumerUID(dis);
}
boolean hasClientID = dis.readBoolean();
if (hasClientID) {
clientID = dis.readUTF();
}
boolean hasDurableName = dis.readBoolean();
if (hasDurableName) {
durableName = dis.readUTF();
}
boolean hasSelector = dis.readBoolean();
if (hasSelector) {
selstr = dis.readUTF();
}
isQueue = dis.readBoolean();
noLocalDelivery = dis.readBoolean();
// consumerReady = dis.readBoolean();
dis.readBoolean();
boolean sharedSet = false;
sharedcnt = 1;
try {
sharedSet = dis.readBoolean();
if (sharedSet == true) {
sharedcnt = dis.readInt();
}
} catch (Exception ex) {
// do nothing prevents failures with old brokers
}
position = -1;
try {
position = dis.readInt();
} catch (Exception ex) {
// do nothing prevents failures with old brokers
}
// 5.0
boolean jmsshare = false;
String ndsubname = null;
try {
jmsshare = dis.readBoolean();
boolean hasndsubname = dis.readBoolean();
if (hasndsubname) {
ndsubname = dis.readUTF();
}
} catch (Exception ex) {
// do nothing prevents failures with old brokers
}
try {
DestinationUID dest = DestinationUID.getUID(destName, isQueue);
if (durableName != null) {
Subscription sub = Subscription.findCreateDurableSubscription(clientID, durableName, (sharedcnt != 1), jmsshare, dest, selstr, noLocalDelivery, false, id, Integer.valueOf(sharedcnt));
return sub;
} else {
if (sharedSet) {
/* non-durable subscriber */
Subscription sub = Subscription.findCreateNonDurableSubscription(clientID, selstr, ndsubname, (sharedcnt != 1), jmsshare, dest, noLocalDelivery, id, Integer.valueOf(sharedcnt));
return sub;
} else {
Consumer c = Consumer.newConsumer(dest, selstr, noLocalDelivery, id);
c.setLockPosition(position);
return c;
}
}
} catch (SelectorFormatException ex) {
logger.logStack(Logger.WARNING, "Got bad selector[" + selstr + "] ", ex);
IOException ioe = new IOException(ex.getMessage());
ioe.initCause(ex);
throw ioe;
} catch (BrokerException ex) {
if (ex.getStatusCode() == Status.CONFLICT || ex instanceof ConsumerAlreadyAddedException) {
logger.log(Logger.WARNING, ex.getMessage());
} else {
logger.logStack(Logger.WARNING, ex.getMessage(), ex);
}
IOException ioe = new IOException(ex.getMessage());
ioe.initCause(ex);
throw ioe;
}
}
Aggregations