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 */
017package org.apache.camel.impl;
018
019import java.util.Map;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.AsyncProcessor;
023import org.apache.camel.AsyncProducerCallback;
024import org.apache.camel.CamelContext;
025import org.apache.camel.Endpoint;
026import org.apache.camel.Exchange;
027import org.apache.camel.ExchangePattern;
028import org.apache.camel.FailedToCreateProducerException;
029import org.apache.camel.Processor;
030import org.apache.camel.Producer;
031import org.apache.camel.ProducerCallback;
032import org.apache.camel.ServicePoolAware;
033import org.apache.camel.processor.UnitOfWorkProducer;
034import org.apache.camel.spi.ServicePool;
035import org.apache.camel.support.ServiceSupport;
036import org.apache.camel.util.AsyncProcessorConverterHelper;
037import org.apache.camel.util.CamelContextHelper;
038import org.apache.camel.util.EventHelper;
039import org.apache.camel.util.LRUCache;
040import org.apache.camel.util.ServiceHelper;
041import org.apache.camel.util.StopWatch;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045/**
046 * Cache containing created {@link Producer}.
047 *
048 * @version 
049 */
050public class ProducerCache extends ServiceSupport {
051    private static final Logger LOG = LoggerFactory.getLogger(ProducerCache.class);
052
053    private final CamelContext camelContext;
054    private final ServicePool<Endpoint, Producer> pool;
055    private final Map<String, Producer> producers;
056    private final Object source;
057    private boolean eventNotifierEnabled = true;
058
059    public ProducerCache(Object source, CamelContext camelContext) {
060        this(source, camelContext, CamelContextHelper.getMaximumCachePoolSize(camelContext));
061    }
062
063    public ProducerCache(Object source, CamelContext camelContext, int cacheSize) {
064        this(source, camelContext, camelContext.getProducerServicePool(), createLRUCache(cacheSize));
065    }
066
067    public ProducerCache(Object source, CamelContext camelContext, Map<String, Producer> cache) {
068        this(source, camelContext, camelContext.getProducerServicePool(), cache);
069    }
070
071    public ProducerCache(Object source, CamelContext camelContext, ServicePool<Endpoint, Producer> producerServicePool, Map<String, Producer> cache) {
072        this.source = source;
073        this.camelContext = camelContext;
074        this.pool = producerServicePool;
075        this.producers = cache;
076    }
077
078    public boolean isEventNotifierEnabled() {
079        return eventNotifierEnabled;
080    }
081
082    public void setEventNotifierEnabled(boolean eventNotifierEnabled) {
083        this.eventNotifierEnabled = eventNotifierEnabled;
084    }
085
086    /**
087     * Creates the {@link LRUCache} to be used.
088     * <p/>
089     * This implementation returns a {@link LRUCache} instance.
090
091     * @param cacheSize the cache size
092     * @return the cache
093     */
094    protected static LRUCache<String, Producer> createLRUCache(int cacheSize) {
095        // Use a regular cache as we want to ensure that the lifecycle of the producers
096        // being cache is properly handled, such as they are stopped when being evicted
097        // or when this cache is stopped. This is needed as some producers requires to
098        // be stopped so they can shutdown internal resources that otherwise may cause leaks
099        return new LRUCache<String, Producer>(cacheSize);
100    }
101
102    public CamelContext getCamelContext() {
103        return camelContext;
104    }
105
106    /**
107     * Gets the source which uses this cache
108     *
109     * @return the source
110     */
111    public Object getSource() {
112        return source;
113    }
114
115    /**
116     * Acquires a pooled producer which you <b>must</b> release back again after usage using the
117     * {@link #releaseProducer(org.apache.camel.Endpoint, org.apache.camel.Producer)} method.
118     *
119     * @param endpoint the endpoint
120     * @return the producer
121     */
122    public Producer acquireProducer(Endpoint endpoint) {
123        return doGetProducer(endpoint, true);
124    }
125
126    /**
127     * Releases an acquired producer back after usage.
128     *
129     * @param endpoint the endpoint
130     * @param producer the producer to release
131     * @throws Exception can be thrown if error stopping producer if that was needed.
132     */
133    public void releaseProducer(Endpoint endpoint, Producer producer) throws Exception {
134        if (producer instanceof ServicePoolAware) {
135            // release back to the pool
136            pool.release(endpoint, producer);
137        } else if (!producer.isSingleton()) {
138            // stop and shutdown non-singleton producers as we should not leak resources
139            ServiceHelper.stopAndShutdownService(producer);
140        }
141    }
142
143    /**
144     * Starts the {@link Producer} to be used for sending to the given endpoint
145     * <p/>
146     * This can be used to early start the {@link Producer} to ensure it can be created,
147     * such as when Camel is started. This allows to fail fast in case the {@link Producer}
148     * could not be started.
149     *
150     * @param endpoint the endpoint to send the exchange to
151     * @throws Exception is thrown if failed to create or start the {@link Producer}
152     */
153    public void startProducer(Endpoint endpoint) throws Exception {
154        Producer producer = acquireProducer(endpoint);
155        releaseProducer(endpoint, producer);
156    }
157
158    /**
159     * Sends the exchange to the given endpoint.
160     * <p>
161     * This method will <b>not</b> throw an exception. If processing of the given
162     * Exchange failed then the exception is stored on the provided Exchange
163     *
164     * @param endpoint the endpoint to send the exchange to
165     * @param exchange the exchange to send
166     */
167    public void send(Endpoint endpoint, Exchange exchange) {
168        sendExchange(endpoint, null, null, exchange);
169    }
170
171    /**
172     * Sends an exchange to an endpoint using a supplied
173     * {@link Processor} to populate the exchange
174     * <p>
175     * This method will <b>not</b> throw an exception. If processing of the given
176     * Exchange failed then the exception is stored on the return Exchange
177     *
178     * @param endpoint the endpoint to send the exchange to
179     * @param processor the transformer used to populate the new exchange
180     * @throws org.apache.camel.CamelExecutionException is thrown if sending failed
181     * @return the exchange
182     */
183    public Exchange send(Endpoint endpoint, Processor processor) {
184        return sendExchange(endpoint, null, processor, null);
185    }
186
187    /**
188     * Sends an exchange to an endpoint using a supplied
189     * {@link Processor} to populate the exchange
190     * <p>
191     * This method will <b>not</b> throw an exception. If processing of the given
192     * Exchange failed then the exception is stored on the return Exchange
193     *
194     * @param endpoint the endpoint to send the exchange to
195     * @param pattern the message {@link ExchangePattern} such as
196     *   {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut}
197     * @param processor the transformer used to populate the new exchange
198     * @return the exchange
199     */
200    public Exchange send(Endpoint endpoint, ExchangePattern pattern, Processor processor) {
201        return sendExchange(endpoint, pattern, processor, null);
202    }
203
204    /**
205     * Sends an exchange to an endpoint using a supplied callback, using the synchronous processing.
206     * <p/>
207     * If an exception was thrown during processing, it would be set on the given Exchange
208     *
209     * @param endpoint  the endpoint to send the exchange to
210     * @param exchange  the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
211     * @param pattern   the exchange pattern, can be <tt>null</tt>
212     * @param callback  the callback
213     * @return the response from the callback
214     * @see #doInAsyncProducer(org.apache.camel.Endpoint, org.apache.camel.Exchange, org.apache.camel.ExchangePattern, org.apache.camel.AsyncCallback, org.apache.camel.AsyncProducerCallback)
215     */
216    public <T> T doInProducer(Endpoint endpoint, Exchange exchange, ExchangePattern pattern, ProducerCallback<T> callback) {
217        T answer = null;
218
219        // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
220        Producer producer = doGetProducer(endpoint, true);
221
222        if (producer == null) {
223            if (isStopped()) {
224                LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
225                return null;
226            } else {
227                throw new IllegalStateException("No producer, this processor has not been started: " + this);
228            }
229        }
230
231        try {
232            // invoke the callback
233            answer = callback.doInProducer(producer, exchange, pattern);
234        } catch (Throwable e) {
235            if (exchange != null) {
236                exchange.setException(e);
237            }
238        } finally {
239            if (producer instanceof ServicePoolAware) {
240                // release back to the pool
241                pool.release(endpoint, producer);
242            } else if (!producer.isSingleton()) {
243                // stop and shutdown non-singleton producers as we should not leak resources
244                try {
245                    ServiceHelper.stopAndShutdownService(producer);
246                } catch (Exception e) {
247                    // ignore and continue
248                    LOG.warn("Error stopping/shutting down producer: " + producer, e);
249                }
250            }
251        }
252
253        return answer;
254    }
255
256    /**
257     * Sends an exchange to an endpoint using a supplied callback supporting the asynchronous routing engine.
258     * <p/>
259     * If an exception was thrown during processing, it would be set on the given Exchange
260     *
261     * @param endpoint         the endpoint to send the exchange to
262     * @param exchange         the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
263     * @param pattern          the exchange pattern, can be <tt>null</tt>
264     * @param callback         the asynchronous callback
265     * @param producerCallback the producer template callback to be executed
266     * @return (doneSync) <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
267     */
268    public boolean doInAsyncProducer(final Endpoint endpoint, final Exchange exchange, final ExchangePattern pattern,
269                                     final AsyncCallback callback, final AsyncProducerCallback producerCallback) {
270
271        Producer target;
272        try {
273            // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
274            target = doGetProducer(endpoint, true);
275
276            if (target == null) {
277                if (isStopped()) {
278                    LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
279                    callback.done(true);
280                    return true;
281                } else {
282                    exchange.setException(new IllegalStateException("No producer, this processor has not been started: " + this));
283                    callback.done(true);
284                    return true;
285                }
286            }
287        } catch (Throwable e) {
288            exchange.setException(e);
289            callback.done(true);
290            return true;
291        }
292
293        final Producer producer = target;
294
295        // record timing for sending the exchange using the producer
296        final StopWatch watch = eventNotifierEnabled && exchange != null ? new StopWatch() : null;
297
298        try {
299            if (eventNotifierEnabled && exchange != null) {
300                EventHelper.notifyExchangeSending(exchange.getContext(), exchange, endpoint);
301            }
302            // invoke the callback
303            AsyncProcessor asyncProcessor = AsyncProcessorConverterHelper.convert(producer);
304            return producerCallback.doInAsyncProducer(producer, asyncProcessor, exchange, pattern, new AsyncCallback() {
305                @Override
306                public void done(boolean doneSync) {
307                    try {
308                        if (eventNotifierEnabled && watch != null) {
309                            long timeTaken = watch.stop();
310                            // emit event that the exchange was sent to the endpoint
311                            EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
312                        }
313
314                        if (producer instanceof ServicePoolAware) {
315                            // release back to the pool
316                            pool.release(endpoint, producer);
317                        } else if (!producer.isSingleton()) {
318                            // stop and shutdown non-singleton producers as we should not leak resources
319                            try {
320                                ServiceHelper.stopAndShutdownService(producer);
321                            } catch (Exception e) {
322                                // ignore and continue
323                                LOG.warn("Error stopping/shutting down producer: " + producer, e);
324                            }
325                        }
326                    } finally {
327                        callback.done(doneSync);
328                    }
329                }
330            });
331        } catch (Throwable e) {
332            // ensure exceptions is caught and set on the exchange
333            if (exchange != null) {
334                exchange.setException(e);
335            }
336            callback.done(true);
337            return true;
338        }
339    }
340
341    protected Exchange sendExchange(final Endpoint endpoint, ExchangePattern pattern,
342                                    final Processor processor, Exchange exchange) {
343        return doInProducer(endpoint, exchange, pattern, new ProducerCallback<Exchange>() {
344            public Exchange doInProducer(Producer producer, Exchange exchange, ExchangePattern pattern) {
345                if (exchange == null) {
346                    exchange = pattern != null ? producer.createExchange(pattern) : producer.createExchange();
347                }
348
349                if (processor != null) {
350                    // lets populate using the processor callback
351                    try {
352                        processor.process(exchange);
353                    } catch (Exception e) {
354                        // populate failed so return
355                        exchange.setException(e);
356                        return exchange;
357                    }
358                }
359
360                // now lets dispatch
361                LOG.debug(">>>> {} {}", endpoint, exchange);
362
363                // set property which endpoint we send to
364                exchange.setProperty(Exchange.TO_ENDPOINT, endpoint.getEndpointUri());
365
366                // send the exchange using the processor
367                StopWatch watch = null;
368                try {
369                    if (eventNotifierEnabled) {
370                        watch = new StopWatch();
371                        EventHelper.notifyExchangeSending(exchange.getContext(), exchange, endpoint);
372                    }
373                    // ensure we run in an unit of work
374                    Producer target = new UnitOfWorkProducer(producer);
375                    target.process(exchange);
376                } catch (Throwable e) {
377                    // ensure exceptions is caught and set on the exchange
378                    exchange.setException(e);
379                } finally {
380                    // emit event that the exchange was sent to the endpoint
381                    if (eventNotifierEnabled && watch != null) {
382                        long timeTaken = watch.stop();
383                        EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
384                    }
385                }
386                return exchange;
387            }
388        });
389    }
390
391    protected synchronized Producer doGetProducer(Endpoint endpoint, boolean pooled) {
392        String key = endpoint.getEndpointUri();
393        Producer answer = producers.get(key);
394        if (pooled && answer == null) {
395            // try acquire from connection pool
396            answer = pool.acquire(endpoint);
397        }
398
399        if (answer == null) {
400            // create a new producer
401            try {
402                answer = endpoint.createProducer();
403                // add as service which will also start the service
404                // (false => we and handling the lifecycle of the producer in this cache)
405                getCamelContext().addService(answer, false);
406            } catch (Exception e) {
407                throw new FailedToCreateProducerException(endpoint, e);
408            }
409
410            // add producer to cache or pool if applicable
411            if (pooled && answer instanceof ServicePoolAware) {
412                LOG.debug("Adding to producer service pool with key: {} for producer: {}", endpoint, answer);
413                answer = pool.addAndAcquire(endpoint, answer);
414            } else if (answer.isSingleton()) {
415                LOG.debug("Adding to producer cache with key: {} for producer: {}", endpoint, answer);
416                producers.put(key, answer);
417            }
418        }
419
420        return answer;
421    }
422
423    protected void doStart() throws Exception {
424        ServiceHelper.startServices(producers.values());
425        ServiceHelper.startServices(pool);
426    }
427
428    protected void doStop() throws Exception {
429        // when stopping we intend to shutdown
430        ServiceHelper.stopAndShutdownService(pool);
431        try {
432            ServiceHelper.stopAndShutdownServices(producers.values());
433        } finally {
434            // ensure producers are removed, and also from JMX
435            for (Producer producer : producers.values()) {
436                getCamelContext().removeService(producer);
437            }
438        }
439        producers.clear();
440    }
441
442    /**
443     * Returns the current size of the cache
444     *
445     * @return the current size
446     */
447    public int size() {
448        int size = producers.size();
449        size += pool.size();
450
451        LOG.trace("size = {}", size);
452        return size;
453    }
454
455    /**
456     * Gets the maximum cache size (capacity).
457     * <p/>
458     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
459     *
460     * @return the capacity
461     */
462    public int getCapacity() {
463        int capacity = -1;
464        if (producers instanceof LRUCache) {
465            LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
466            capacity = cache.getMaxCacheSize();
467        }
468        return capacity;
469    }
470
471    /**
472     * Gets the cache hits statistic
473     * <p/>
474     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
475     *
476     * @return the hits
477     */
478    public long getHits() {
479        long hits = -1;
480        if (producers instanceof LRUCache) {
481            LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
482            hits = cache.getHits();
483        }
484        return hits;
485    }
486
487    /**
488     * Gets the cache misses statistic
489     * <p/>
490     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
491     *
492     * @return the misses
493     */
494    public long getMisses() {
495        long misses = -1;
496        if (producers instanceof LRUCache) {
497            LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
498            misses = cache.getMisses();
499        }
500        return misses;
501    }
502
503    /**
504     * Gets the cache evicted statistic
505     * <p/>
506     * Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
507     *
508     * @return the evicted
509     */
510    public long getEvicted() {
511        long evicted = -1;
512        if (producers instanceof LRUCache) {
513            LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
514            evicted = cache.getEvicted();
515        }
516        return evicted;
517    }
518
519    /**
520     * Resets the cache statistics
521     */
522    public void resetCacheStatistics() {
523        if (producers instanceof LRUCache) {
524            LRUCache<String, Producer> cache = (LRUCache<String, Producer>)producers;
525            cache.resetStatistics();
526        }
527    }
528
529    /**
530     * Purges this cache
531     */
532    public synchronized void purge() {
533        producers.clear();
534        pool.purge();
535    }
536
537    @Override
538    public String toString() {
539        return "ProducerCache for source: " + source + ", capacity: " + getCapacity();
540    }
541}