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.Endpoint; 022 import org.apache.camel.Exchange; 023 import org.apache.camel.FailedToCreateConsumerException; 024 import org.apache.camel.IsSingleton; 025 import org.apache.camel.PollingConsumer; 026 import org.apache.camel.util.LRUCache; 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: 789593 $ 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; 040 041 public ConsumerCache() { 042 this.consumers = new LRUCache<String, PollingConsumer>(1000); 043 } 044 045 public ConsumerCache(Map<String, PollingConsumer> cache) { 046 this.consumers = cache; 047 } 048 049 public synchronized PollingConsumer getConsumer(Endpoint endpoint) { 050 String key = endpoint.getEndpointUri(); 051 PollingConsumer answer = consumers.get(key); 052 if (answer == null) { 053 try { 054 answer = endpoint.createPollingConsumer(); 055 answer.start(); 056 } catch (Exception e) { 057 throw new FailedToCreateConsumerException(endpoint, e); 058 } 059 060 boolean singleton = true; 061 if (answer instanceof IsSingleton) { 062 singleton = ((IsSingleton)answer).isSingleton(); 063 } 064 065 if (singleton) { 066 if (LOG.isDebugEnabled()) { 067 LOG.debug("Adding to consumer cache with key: " + endpoint + " for consumer: " + answer); 068 } 069 consumers.put(key, answer); 070 } else { 071 if (LOG.isDebugEnabled()) { 072 LOG.debug("Consumer for endpoint: " + key + " is not singleton and thus not added to consumer cache"); 073 } 074 } 075 } 076 return answer; 077 } 078 079 public Exchange receive(Endpoint endpoint) { 080 if (LOG.isDebugEnabled()) { 081 LOG.debug("<<<< " + endpoint); 082 } 083 084 PollingConsumer consumer = getConsumer(endpoint); 085 return consumer.receive(); 086 } 087 088 public Exchange receive(Endpoint endpoint, long timeout) { 089 if (LOG.isDebugEnabled()) { 090 LOG.debug("<<<< " + endpoint); 091 } 092 093 PollingConsumer consumer = getConsumer(endpoint); 094 return consumer.receive(timeout); 095 } 096 097 public Exchange receiveNoWait(Endpoint endpoint) { 098 if (LOG.isDebugEnabled()) { 099 LOG.debug("<<<< " + endpoint); 100 } 101 102 PollingConsumer consumer = getConsumer(endpoint); 103 return consumer.receiveNoWait(); 104 } 105 106 protected void doStop() throws Exception { 107 ServiceHelper.stopServices(consumers.values()); 108 consumers.clear(); 109 } 110 111 protected void doStart() throws Exception { 112 } 113 114 /** 115 * Returns the current size of the consumer cache 116 * 117 * @return the current size 118 */ 119 int size() { 120 return consumers.size(); 121 } 122 123 }