Search in sources :

Example 26 with PartitionedStore

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Destination(com.sun.messaging.jmq.jmsserver.core.Destination) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) IOException(java.io.IOException) SelectorFormatException(com.sun.messaging.jmq.util.selector.SelectorFormatException) ConsumerAlreadyAddedException(com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) Iterator(java.util.Iterator) Subscription(com.sun.messaging.jmq.jmsserver.core.Subscription) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)

Aggregations

PartitionedStore (com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)26 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)13 HashMap (java.util.HashMap)11 Map (java.util.Map)10 Iterator (java.util.Iterator)9 LinkedHashMap (java.util.LinkedHashMap)8 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)7 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)7 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)7 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)7 NoPersistPartitionedStoreImpl (com.sun.messaging.jmq.jmsserver.persist.api.NoPersistPartitionedStoreImpl)7 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)6 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)6 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)6 LinkedHashSet (java.util.LinkedHashSet)6 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)4 DestinationList (com.sun.messaging.jmq.jmsserver.core.DestinationList)4