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.ExchangePattern;
025 import org.apache.camel.FailedToCreateProducerException;
026 import org.apache.camel.Processor;
027 import org.apache.camel.Producer;
028 import org.apache.camel.ProducerCallback;
029 import org.apache.camel.ServicePoolAware;
030 import org.apache.camel.spi.ServicePool;
031 import org.apache.camel.util.LRUCache;
032 import org.apache.camel.util.ServiceHelper;
033 import org.apache.commons.logging.Log;
034 import org.apache.commons.logging.LogFactory;
035 import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
036
037 /**
038 * Cache containing created {@link Producer}.
039 *
040 * @version $Revision: 891183 $
041 */
042 public class ProducerCache extends ServiceSupport {
043 private static final transient Log LOG = LogFactory.getLog(ProducerCache.class);
044
045 private final Map<String, Producer> producers;
046 private final ServicePool<Endpoint, Producer> pool;
047 private final CamelContext context;
048
049 // TODO: Have easy configuration of pooling in Camel
050
051 public ProducerCache(CamelContext context) {
052 this(context, context.getProducerServicePool());
053 }
054
055 public ProducerCache(CamelContext context, ServicePool<Endpoint, Producer> producerServicePool) {
056 this(context, producerServicePool, new LRUCache<String, Producer>(1000));
057 }
058
059 public ProducerCache(CamelContext context, ServicePool<Endpoint, Producer> producerServicePool, Map<String, Producer> cache) {
060 this.context = context;
061 this.pool = producerServicePool;
062 this.producers = cache;
063 }
064
065 public Producer getProducer(Endpoint endpoint) {
066 // As the producer is returned outside this method we do not want to return pooled producers
067 // so we pass in false to the method. if we returned pooled producers then the user had
068 // to remember to return it back in the pool.
069 // See method doInProducer that is safe template pattern where we handle the lifecycle and
070 // thus safely can use pooled producers there
071 return doGetProducer(endpoint, false);
072 }
073
074 /**
075 * Sends the exchange to the given endpoint
076 *
077 * @param endpoint the endpoint to send the exchange to
078 * @param exchange the exchange to send
079 */
080 public void send(Endpoint endpoint, Exchange exchange) {
081 try {
082 sendExchange(endpoint, null, null, exchange);
083 } catch (Exception e) {
084 throw wrapRuntimeCamelException(e);
085 }
086 }
087
088 /**
089 * Sends an exchange to an endpoint using a supplied
090 * {@link Processor} to populate the exchange
091 *
092 * @param endpoint the endpoint to send the exchange to
093 * @param processor the transformer used to populate the new exchange
094 * @return the exchange
095 */
096 public Exchange send(Endpoint endpoint, Processor processor) {
097 try {
098 return sendExchange(endpoint, null, processor, null);
099 } catch (Exception e) {
100 throw wrapRuntimeCamelException(e);
101 }
102 }
103
104 /**
105 * Sends an exchange to an endpoint using a supplied
106 * {@link Processor} to populate the exchange
107 *
108 * @param endpoint the endpoint to send the exchange to
109 * @param pattern the message {@link ExchangePattern} such as
110 * {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut}
111 * @param processor the transformer used to populate the new exchange
112 * @return the exchange
113 */
114 public Exchange send(Endpoint endpoint, ExchangePattern pattern, Processor processor) {
115 try {
116 return sendExchange(endpoint, pattern, processor, null);
117 } catch (Exception e) {
118 throw wrapRuntimeCamelException(e);
119 }
120 }
121
122 /**
123 * Sends an exchange to an endpoint using a supplied callback
124 *
125 * @param endpoint the endpoint to send the exchange to
126 * @param exchange the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
127 * @param pattern the exchange pattern, can be <tt>null</tt>
128 * @return the response from the callback
129 * @throws Exception if an internal processing error has occurred.
130 */
131 public <T> T doInProducer(Endpoint endpoint, Exchange exchange, ExchangePattern pattern, ProducerCallback<T> callback) throws Exception {
132 // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
133 Producer producer = doGetProducer(endpoint, true);
134
135 if (producer == null) {
136 if (isStopped()) {
137 LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
138 return null;
139 } else {
140 throw new IllegalStateException("No producer, this processor has not been started: " + this);
141 }
142 }
143
144 try {
145 // invoke the callback
146 return callback.doInProducer(producer, exchange, pattern);
147 } finally {
148 if (producer instanceof ServicePoolAware) {
149 // release back to the pool
150 pool.release(endpoint, producer);
151 } else if (!producer.isSingleton()) {
152 // stop non singleton producers as we should not leak resources
153 producer.stop();
154 }
155 }
156 }
157
158 protected Exchange sendExchange(final Endpoint endpoint, ExchangePattern pattern,
159 final Processor processor, Exchange exchange) throws Exception {
160 return doInProducer(endpoint, exchange, pattern, new ProducerCallback<Exchange>() {
161 public Exchange doInProducer(Producer producer, Exchange exchange, ExchangePattern pattern) throws Exception {
162 if (exchange == null) {
163 exchange = pattern != null ? producer.createExchange(pattern) : producer.createExchange();
164 }
165
166 if (processor != null) {
167 // lets populate using the processor callback
168 processor.process(exchange);
169 }
170
171 // now lets dispatch
172 if (LOG.isDebugEnabled()) {
173 LOG.debug(">>>> " + endpoint + " " + exchange);
174 }
175
176 // set property which endpoint we send to
177 exchange.setProperty(Exchange.TO_ENDPOINT, endpoint.getEndpointUri());
178
179 producer.process(exchange);
180 return exchange;
181 }
182 });
183 }
184
185 protected synchronized Producer doGetProducer(Endpoint endpoint, boolean pooled) {
186 String key = endpoint.getEndpointUri();
187 Producer answer = producers.get(key);
188 if (pooled && answer == null) {
189 // try acquire from connection pool
190 answer = pool.acquire(endpoint);
191 }
192
193 if (answer == null) {
194 // create a new producer
195 try {
196 answer = endpoint.createProducer();
197 // add it as service to camel context so it can be managed as well
198 context.addService(answer);
199 } catch (Exception e) {
200 throw new FailedToCreateProducerException(endpoint, e);
201 }
202
203 // add producer to cache or pool if applicable
204 if (pooled && answer instanceof ServicePoolAware) {
205 if (LOG.isDebugEnabled()) {
206 LOG.debug("Adding to producer service pool with key: " + endpoint + " for producer: " + answer);
207 }
208 answer = pool.addAndAcquire(endpoint, answer);
209 } else if (answer.isSingleton()) {
210 if (LOG.isDebugEnabled()) {
211 LOG.debug("Adding to producer cache with key: " + endpoint + " for producer: " + answer);
212 }
213 producers.put(key, answer);
214 }
215 }
216
217 return answer;
218 }
219
220 protected void doStop() throws Exception {
221 ServiceHelper.stopServices(producers);
222 producers.clear();
223
224 ServiceHelper.stopServices(pool);
225 }
226
227 protected void doStart() throws Exception {
228 ServiceHelper.startServices(pool);
229 }
230
231 /**
232 * Returns the current size of the producer cache
233 *
234 * @return the current size
235 */
236 int size() {
237 return producers.size();
238 }
239
240 }