Search in sources :

Example 1 with ServicePoolAware

use of org.apache.camel.ServicePoolAware in project camel by apache.

the class ProducerCache method doInProducer.

/**
     * Sends an exchange to an endpoint using a supplied callback, using the synchronous processing.
     * <p/>
     * If an exception was thrown during processing, it would be set on the given Exchange
     *
     * @param endpoint  the endpoint to send the exchange to
     * @param exchange  the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
     * @param pattern   the exchange pattern, can be <tt>null</tt>
     * @param callback  the callback
     * @return the response from the callback
     * @see #doInAsyncProducer(org.apache.camel.Endpoint, org.apache.camel.Exchange, org.apache.camel.ExchangePattern, org.apache.camel.AsyncCallback, org.apache.camel.AsyncProducerCallback)
     */
public <T> T doInProducer(Endpoint endpoint, Exchange exchange, ExchangePattern pattern, ProducerCallback<T> callback) {
    T answer = null;
    // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
    Producer producer = doGetProducer(endpoint, true);
    if (producer == null) {
        if (isStopped()) {
            LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
            return null;
        } else {
            throw new IllegalStateException("No producer, this processor has not been started: " + this);
        }
    }
    try {
        // invoke the callback
        answer = callback.doInProducer(producer, exchange, pattern);
    } catch (Throwable e) {
        if (exchange != null) {
            exchange.setException(e);
        }
    } finally {
        if (producer instanceof ServicePoolAware) {
            // release back to the pool
            pool.release(endpoint, producer);
        } else if (!producer.isSingleton()) {
            // stop and shutdown non-singleton producers as we should not leak resources
            try {
                ServiceHelper.stopAndShutdownService(producer);
            } catch (Exception e) {
                // ignore and continue
                LOG.warn("Error stopping/shutting down producer: " + producer, e);
            }
        }
    }
    return answer;
}
Also used : Producer(org.apache.camel.Producer) ServicePoolAware(org.apache.camel.ServicePoolAware) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Example 2 with ServicePoolAware

use of org.apache.camel.ServicePoolAware in project camel by apache.

the class ProducerCache method doInAsyncProducer.

/**
     * Sends an exchange to an endpoint using a supplied callback supporting the asynchronous routing engine.
     * <p/>
     * If an exception was thrown during processing, it would be set on the given Exchange
     *
     * @param endpoint         the endpoint to send the exchange to
     * @param exchange         the exchange, can be <tt>null</tt> if so then create a new exchange from the producer
     * @param pattern          the exchange pattern, can be <tt>null</tt>
     * @param callback         the asynchronous callback
     * @param producerCallback the producer template callback to be executed
     * @return (doneSync) <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
     */
public boolean doInAsyncProducer(final Endpoint endpoint, final Exchange exchange, final ExchangePattern pattern, final AsyncCallback callback, final AsyncProducerCallback producerCallback) {
    Producer target;
    try {
        // get the producer and we do not mind if its pooled as we can handle returning it back to the pool
        target = doGetProducer(endpoint, true);
        if (target == null) {
            if (isStopped()) {
                LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange);
                callback.done(true);
                return true;
            } else {
                exchange.setException(new IllegalStateException("No producer, this processor has not been started: " + this));
                callback.done(true);
                return true;
            }
        }
    } catch (Throwable e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    final Producer producer = target;
    // record timing for sending the exchange using the producer
    final StopWatch watch = eventNotifierEnabled && exchange != null ? new StopWatch() : null;
    try {
        if (eventNotifierEnabled && exchange != null) {
            EventHelper.notifyExchangeSending(exchange.getContext(), exchange, endpoint);
        }
        // invoke the callback
        AsyncProcessor asyncProcessor = AsyncProcessorConverterHelper.convert(producer);
        return producerCallback.doInAsyncProducer(producer, asyncProcessor, exchange, pattern, doneSync -> {
            try {
                if (eventNotifierEnabled && watch != null) {
                    long timeTaken = watch.stop();
                    EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
                }
                if (producer instanceof ServicePoolAware) {
                    pool.release(endpoint, producer);
                } else if (!producer.isSingleton()) {
                    try {
                        ServiceHelper.stopAndShutdownService(producer);
                    } catch (Exception e) {
                        LOG.warn("Error stopping/shutting down producer: " + producer, e);
                    }
                }
            } finally {
                callback.done(doneSync);
            }
        });
    } catch (Throwable e) {
        // ensure exceptions is caught and set on the exchange
        if (exchange != null) {
            exchange.setException(e);
        }
        callback.done(true);
        return true;
    }
}
Also used : Producer(org.apache.camel.Producer) AsyncProcessor(org.apache.camel.AsyncProcessor) ServicePoolAware(org.apache.camel.ServicePoolAware) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) StopWatch(org.apache.camel.util.StopWatch)

Example 3 with ServicePoolAware

use of org.apache.camel.ServicePoolAware in project camel by apache.

the class ProducerCache method doGetProducer.

protected synchronized Producer doGetProducer(Endpoint endpoint, boolean pooled) {
    String key = endpoint.getEndpointUri();
    Producer answer = producers.get(key);
    if (pooled && answer == null) {
        // try acquire from connection pool
        answer = pool.acquire(endpoint);
    }
    if (answer == null) {
        // create a new producer
        try {
            answer = endpoint.createProducer();
            // add as service which will also start the service
            // (false => we and handling the lifecycle of the producer in this cache)
            getCamelContext().addService(answer, false);
        } catch (Exception e) {
            throw new FailedToCreateProducerException(endpoint, e);
        }
        // add producer to cache or pool if applicable
        if (pooled && answer instanceof ServicePoolAware) {
            LOG.debug("Adding to producer service pool with key: {} for producer: {}", endpoint, answer);
            answer = pool.addAndAcquire(endpoint, answer);
        } else if (answer.isSingleton()) {
            LOG.debug("Adding to producer cache with key: {} for producer: {}", endpoint, answer);
            producers.put(key, answer);
        }
    }
    if (answer != null) {
        // record statistics
        if (extendedStatistics) {
            statistics.onHit(key);
        }
    }
    return answer;
}
Also used : FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) Producer(org.apache.camel.Producer) ServicePoolAware(org.apache.camel.ServicePoolAware) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Example 4 with ServicePoolAware

use of org.apache.camel.ServicePoolAware in project camel by apache.

the class SendProcessor method doStart.

protected void doStart() throws Exception {
    if (producerCache == null) {
        // use a single producer cache as we need to only hold reference for one destination
        // and use a regular HashMap as we do not want a soft reference store that may get re-claimed when low on memory
        // as we want to ensure the producer is kept around, to ensure its lifecycle is fully managed,
        // eg stopping the producer when we stop etc.
        producerCache = new ProducerCache(this, camelContext, new HashMap<String, Producer>(1));
    // do not add as service as we do not want to manage the producer cache
    }
    ServiceHelper.startService(producerCache);
    // the destination could since have been intercepted by a interceptSendToEndpoint so we got to
    // lookup this before we can use the destination
    Endpoint lookup = camelContext.hasEndpoint(destination.getEndpointKey());
    if (lookup instanceof InterceptSendToEndpoint) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Intercepted sending to {} -> {}", URISupport.sanitizeUri(destination.getEndpointUri()), URISupport.sanitizeUri(lookup.getEndpointUri()));
        }
        destination = lookup;
    }
    // warm up the producer by starting it so we can fail fast if there was a problem
    // however must start endpoint first
    ServiceHelper.startService(destination);
    // this SendProcessor is used a lot in Camel (eg every .to in the route DSL) and therefore we
    // want to optimize for regular producers, by using the producer directly instead of the ProducerCache
    // Only for pooled and non singleton producers we have to use the ProducerCache as it supports these
    // kind of producer better (though these kind of producer should be rare)
    Producer producer = producerCache.acquireProducer(destination);
    if (producer instanceof ServicePoolAware || !producer.isSingleton()) {
        // no we cannot optimize it - so release the producer back to the producer cache
        // and use the producer cache for sending
        producerCache.releaseProducer(destination, producer);
    } else {
        // yes we can optimize and use the producer directly for sending
        this.producer = AsyncProcessorConverterHelper.convert(producer);
    }
}
Also used : InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Endpoint(org.apache.camel.Endpoint) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Producer(org.apache.camel.Producer) HashMap(java.util.HashMap) ServicePoolAware(org.apache.camel.ServicePoolAware) ProducerCache(org.apache.camel.impl.ProducerCache)

Example 5 with ServicePoolAware

use of org.apache.camel.ServicePoolAware in project camel by apache.

the class ConsumerCache method doGetPollingConsumer.

protected synchronized PollingConsumer doGetPollingConsumer(Endpoint endpoint, boolean pooled) {
    String key = endpoint.getEndpointUri();
    PollingConsumer answer = consumers.get(key);
    if (pooled && answer == null) {
        pool.acquire(endpoint);
    }
    if (answer == null) {
        try {
            answer = endpoint.createPollingConsumer();
            answer.start();
        } catch (Exception e) {
            throw new FailedToCreateConsumerException(endpoint, e);
        }
        if (pooled && answer instanceof ServicePoolAware) {
            LOG.debug("Adding to producer service pool with key: {} for producer: {}", endpoint, answer);
            answer = pool.addAndAcquire(endpoint, answer);
        } else {
            boolean singleton = false;
            if (answer instanceof IsSingleton) {
                singleton = ((IsSingleton) answer).isSingleton();
            }
            if (singleton) {
                LOG.debug("Adding to consumer cache with key: {} for consumer: {}", endpoint, answer);
                consumers.put(key, answer);
            } else {
                LOG.debug("Consumer for endpoint: {} is not singleton and thus not added to consumer cache", key);
            }
        }
    }
    if (answer != null) {
        // record statistics
        if (extendedStatistics) {
            statistics.onHit(key);
        }
    }
    return answer;
}
Also used : PollingConsumer(org.apache.camel.PollingConsumer) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException) IsSingleton(org.apache.camel.IsSingleton) ServicePoolAware(org.apache.camel.ServicePoolAware) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException)

Aggregations

ServicePoolAware (org.apache.camel.ServicePoolAware)5 Producer (org.apache.camel.Producer)4 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)3 HashMap (java.util.HashMap)1 AsyncProcessor (org.apache.camel.AsyncProcessor)1 Endpoint (org.apache.camel.Endpoint)1 FailedToCreateConsumerException (org.apache.camel.FailedToCreateConsumerException)1 IsSingleton (org.apache.camel.IsSingleton)1 PollingConsumer (org.apache.camel.PollingConsumer)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 InterceptSendToEndpoint (org.apache.camel.impl.InterceptSendToEndpoint)1 ProducerCache (org.apache.camel.impl.ProducerCache)1 StopWatch (org.apache.camel.util.StopWatch)1