use of org.apache.camel.Producer in project camel by apache.
the class DataSetEndpoint method createProducer.
@Override
public Producer createProducer() throws Exception {
Producer answer = super.createProducer();
long size = getDataSet().getSize();
expectedMessageCount((int) size);
return answer;
}
use of org.apache.camel.Producer 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;
}
use of org.apache.camel.Producer 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;
}
}
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;
}
Aggregations