use of org.apache.camel.Producer in project camel by apache.
the class ProducerCache method getCapacity.
/**
* Gets the maximum cache size (capacity).
* <p/>
* Will return <tt>-1</tt> if it cannot determine this if a custom cache was used.
*
* @return the capacity
*/
public int getCapacity() {
int capacity = -1;
if (producers instanceof LRUCache) {
LRUCache<String, Producer> cache = (LRUCache<String, Producer>) producers;
capacity = cache.getMaxCacheSize();
}
return capacity;
}
use of org.apache.camel.Producer 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;
}
use of org.apache.camel.Producer in project camel by apache.
the class ProducerCache method doStop.
protected void doStop() throws Exception {
// when stopping we intend to shutdown
ServiceHelper.stopAndShutdownService(statistics);
if (stopServicePool) {
ServiceHelper.stopAndShutdownService(pool);
}
try {
ServiceHelper.stopAndShutdownServices(producers.values());
} finally {
// ensure producers are removed, and also from JMX
for (Producer producer : producers.values()) {
getCamelContext().removeService(producer);
}
}
producers.clear();
if (statistics != null) {
statistics.clear();
}
}
use of org.apache.camel.Producer in project camel by apache.
the class ProducerCache method startProducer.
/**
* Starts the {@link Producer} to be used for sending to the given endpoint
* <p/>
* This can be used to early start the {@link Producer} to ensure it can be created,
* such as when Camel is started. This allows to fail fast in case the {@link Producer}
* could not be started.
*
* @param endpoint the endpoint to send the exchange to
* @throws Exception is thrown if failed to create or start the {@link Producer}
*/
public void startProducer(Endpoint endpoint) throws Exception {
Producer producer = acquireProducer(endpoint);
releaseProducer(endpoint, producer);
}
use of org.apache.camel.Producer in project camel by apache.
the class MulticastProcessor method doProcessParallel.
private void doProcessParallel(final ProcessorExchangePair pair) throws Exception {
final Exchange exchange = pair.getExchange();
Processor processor = pair.getProcessor();
Producer producer = pair.getProducer();
TracedRouteNodes traced = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getTracedRouteNodes() : null;
// compute time taken if sending to another endpoint
StopWatch watch = null;
if (producer != null) {
watch = new StopWatch();
}
try {
// prepare tracing starting from a new block
if (traced != null) {
traced.pushBlock();
}
if (producer != null) {
EventHelper.notifyExchangeSending(exchange.getContext(), exchange, producer.getEndpoint());
}
// let the prepared process it, remember to begin the exchange pair
AsyncProcessor async = AsyncProcessorConverterHelper.convert(processor);
pair.begin();
// we invoke it synchronously as parallel async routing is too hard
AsyncProcessorHelper.process(async, exchange);
} finally {
pair.done();
// pop the block so by next round we have the same staring point and thus the tracing looks accurate
if (traced != null) {
traced.popBlock();
}
if (producer != null) {
long timeTaken = watch.stop();
Endpoint endpoint = producer.getEndpoint();
// emit event that the exchange was sent to the endpoint
// this is okay to do here in the finally block, as the processing is not using the async routing engine
//( we invoke it synchronously as parallel async routing is too hard)
EventHelper.notifyExchangeSent(exchange.getContext(), exchange, endpoint, timeTaken);
}
}
}
Aggregations