001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.impl;
018    
019    import java.util.Map;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.Exchange;
024    import org.apache.camel.FailedToCreateConsumerException;
025    import org.apache.camel.IsSingleton;
026    import org.apache.camel.PollingConsumer;
027    import org.apache.camel.util.CamelContextHelper;
028    import org.apache.camel.util.LRUCache;
029    import org.apache.camel.util.ServiceHelper;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * Cache containing created {@link org.apache.camel.Consumer}.
035     *
036     * @version $Revision: 937935 $
037     */
038    public class ConsumerCache extends ServiceSupport {
039        private static final transient Log LOG = LogFactory.getLog(ConsumerCache.class);
040        private final CamelContext camelContext;
041        private final Map<String, PollingConsumer> consumers;
042    
043        public ConsumerCache(CamelContext camelContext) {
044            this(camelContext, CamelContextHelper.getMaximumCachePoolSize(camelContext));
045        }
046    
047        public ConsumerCache(CamelContext camelContext, int maximumCacheSize) {
048            this(camelContext, new LRUCache<String, PollingConsumer>(maximumCacheSize));
049        }
050    
051        public ConsumerCache(CamelContext camelContext, Map<String, PollingConsumer> cache) {
052            this.camelContext = camelContext;
053            this.consumers = cache;
054        }
055    
056        public synchronized PollingConsumer getConsumer(Endpoint endpoint) {
057            String key = endpoint.getEndpointUri();
058            PollingConsumer answer = consumers.get(key);
059            if (answer == null) {
060                try {
061                    answer = endpoint.createPollingConsumer();
062                    answer.start();
063                } catch (Exception e) {
064                    throw new FailedToCreateConsumerException(endpoint, e);
065                }
066    
067                boolean singleton = true;
068                if (answer instanceof IsSingleton) {
069                    singleton = ((IsSingleton) answer).isSingleton();
070                }
071    
072                if (singleton) {
073                    if (LOG.isDebugEnabled()) {
074                        LOG.debug("Adding to consumer cache with key: " + endpoint + " for consumer: " + answer);
075                    }
076                    consumers.put(key, answer);
077                } else {
078                    if (LOG.isDebugEnabled()) {
079                        LOG.debug("Consumer for endpoint: " + key + " is not singleton and thus not added to consumer cache");
080                    }
081                }
082            }
083            return answer;
084        }
085    
086        public Exchange receive(Endpoint endpoint) {
087            if (LOG.isDebugEnabled()) {
088                LOG.debug("<<<< " + endpoint);
089            }
090    
091            PollingConsumer consumer = getConsumer(endpoint);
092            return consumer.receive();
093        }
094    
095        public Exchange receive(Endpoint endpoint, long timeout) {
096            if (LOG.isDebugEnabled()) {
097                LOG.debug("<<<< " + endpoint);
098            }
099    
100            PollingConsumer consumer = getConsumer(endpoint);
101            return consumer.receive(timeout);
102        }
103    
104        public Exchange receiveNoWait(Endpoint endpoint) {
105            if (LOG.isDebugEnabled()) {
106                LOG.debug("<<<< " + endpoint);
107            }
108    
109            PollingConsumer consumer = getConsumer(endpoint);
110            return consumer.receiveNoWait();
111        }
112        
113        public CamelContext getCamelContext() {
114            return camelContext;
115        }
116    
117        protected void doStart() throws Exception {
118            ServiceHelper.startServices(consumers);
119        }
120    
121        protected void doStop() throws Exception {
122            ServiceHelper.stopServices(consumers);
123            consumers.clear();
124        }
125    
126        /**
127         * Returns the current size of the consumer cache
128         *
129         * @return the current size
130         */
131        int size() {
132            return consumers.size();
133        }
134    
135    }