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.HashMap;
020 import java.util.Map;
021
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.ServiceHelper;
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030
031 /**
032 * Cache containing created {@link org.apache.camel.Consumer}.
033 *
034 * @version $Revision: 778782 $
035 */
036 public class ConsumerCache extends ServiceSupport {
037 private static final transient Log LOG = LogFactory.getLog(ConsumerCache.class);
038
039 private final Map<String, PollingConsumer> consumers = new HashMap<String, PollingConsumer>();
040
041 public synchronized PollingConsumer getConsumer(Endpoint endpoint) {
042 String key = endpoint.getEndpointUri();
043 PollingConsumer answer = consumers.get(key);
044 if (answer == null) {
045 try {
046 answer = endpoint.createPollingConsumer();
047 answer.start();
048 } catch (Exception e) {
049 throw new FailedToCreateConsumerException(endpoint, e);
050 }
051
052 boolean singleton = true;
053 if (answer instanceof IsSingleton) {
054 singleton = ((IsSingleton)answer).isSingleton();
055 }
056
057 if (singleton) {
058 if (LOG.isDebugEnabled()) {
059 LOG.debug("Adding to consumer cache with key: " + endpoint + " for consumer: " + answer);
060 }
061 consumers.put(key, answer);
062 } else {
063 if (LOG.isDebugEnabled()) {
064 LOG.debug("Consumer for endpoint: " + key + " is not singleton and thus not added to producer cache");
065 }
066 }
067 }
068 return answer;
069 }
070
071 public Exchange receive(Endpoint endpoint) {
072 if (LOG.isDebugEnabled()) {
073 LOG.debug("<<<< " + endpoint);
074 }
075
076 PollingConsumer consumer = getConsumer(endpoint);
077 return consumer.receive();
078 }
079
080 public Exchange receive(Endpoint endpoint, long timeout) {
081 if (LOG.isDebugEnabled()) {
082 LOG.debug("<<<< " + endpoint);
083 }
084
085 PollingConsumer consumer = getConsumer(endpoint);
086 return consumer.receive(timeout);
087 }
088
089 public Exchange receiveNoWait(Endpoint endpoint) {
090 if (LOG.isDebugEnabled()) {
091 LOG.debug("<<<< " + endpoint);
092 }
093
094 PollingConsumer consumer = getConsumer(endpoint);
095 return consumer.receiveNoWait();
096 }
097
098 protected void doStop() throws Exception {
099 ServiceHelper.stopServices(consumers.values());
100 consumers.clear();
101 }
102
103 protected void doStart() throws Exception {
104 }
105
106 }