use of org.apache.camel.AsyncProcessor 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.AsyncProcessor in project camel by apache.
the class ProducerCache method asyncSendExchange.
/**
* Asynchronously sends an exchange to an endpoint using a supplied
* {@link Processor} to populate the exchange
* <p>
* This method will <b>neither</b> throw an exception <b>nor</b> complete future exceptionally.
* If processing of the given Exchange failed then the exception is stored on the return Exchange
*
* @param endpoint the endpoint to send the exchange to
* @param pattern the message {@link ExchangePattern} such as
* {@link ExchangePattern#InOnly} or {@link ExchangePattern#InOut}
* @param processor the transformer used to populate the new exchange
* @param resultProcessor a processor to process the exchange when the send is complete.
* @param exchange an exchange to use in processing. Exchange will be created if parameter is null.
* @param future the preexisting future to complete when processing is done or null if to create new one
* @return future that completes with exchange when processing is done. Either passed into future parameter
* or new one if parameter was null
*/
public CompletableFuture<Exchange> asyncSendExchange(final Endpoint endpoint, ExchangePattern pattern, final Processor processor, final Processor resultProcessor, Exchange exchange, CompletableFuture<Exchange> future) {
AsyncCallbackToCompletableFutureAdapter<Exchange> futureAdapter = new AsyncCallbackToCompletableFutureAdapter<>(future, exchange);
doInAsyncProducer(endpoint, exchange, pattern, futureAdapter, (producer, asyncProducer, innerExchange, exchangePattern, producerCallback) -> {
if (innerExchange == null) {
innerExchange = pattern != null ? producer.getEndpoint().createExchange(pattern) : producer.getEndpoint().createExchange();
futureAdapter.setResult(innerExchange);
}
if (processor != null) {
// lets populate using the processor callback
AsyncProcessor asyncProcessor = AsyncProcessorConverterHelper.convert(processor);
try {
final Exchange finalExchange = innerExchange;
asyncProcessor.process(innerExchange, doneSync -> asyncDispatchExchange(endpoint, producer, resultProcessor, finalExchange, producerCallback));
return false;
} catch (Exception e) {
// populate failed so return
innerExchange.setException(e);
producerCallback.done(true);
return true;
}
}
return asyncDispatchExchange(endpoint, producer, resultProcessor, innerExchange, producerCallback);
});
return futureAdapter.getFuture();
}
use of org.apache.camel.AsyncProcessor 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);
}
}
}
use of org.apache.camel.AsyncProcessor in project camel by apache.
the class ChoiceProcessor method process.
public boolean process(final Exchange exchange, final AsyncCallback callback) {
Iterator<Processor> processors = next().iterator();
// callback to restore existing FILTER_MATCHED property on the Exchange
final Object existing = exchange.getProperty(Exchange.FILTER_MATCHED);
final AsyncCallback choiceCallback = new AsyncCallback() {
@Override
public void done(boolean doneSync) {
if (existing != null) {
exchange.setProperty(Exchange.FILTER_MATCHED, existing);
} else {
exchange.removeProperty(Exchange.FILTER_MATCHED);
}
callback.done(doneSync);
}
};
// and if not, we just continue without using any processor
while (processors.hasNext()) {
// get the next processor
Processor processor = processors.next();
// evaluate the predicate on filter predicate early to be faster
// and avoid issues when having nested choices
// as we should only pick one processor
boolean matches = false;
if (processor instanceof FilterProcessor) {
FilterProcessor filter = (FilterProcessor) processor;
try {
matches = filter.matches(exchange);
// as we have pre evaluated the predicate then use its processor directly when routing
processor = filter.getProcessor();
} catch (Throwable e) {
exchange.setException(e);
}
} else {
// its the otherwise processor, so its a match
notFiltered++;
matches = true;
}
// check for error if so we should break out
if (!continueProcessing(exchange, "so breaking out of choice", LOG)) {
break;
}
// if we did not match then continue to next filter
if (!matches) {
continue;
}
// okay we found a filter or its the otherwise we are processing
AsyncProcessor async = AsyncProcessorConverterHelper.convert(processor);
return async.process(exchange, choiceCallback);
}
// when no filter matches and there is no otherwise, then just continue
choiceCallback.done(true);
return true;
}
use of org.apache.camel.AsyncProcessor in project camel by apache.
the class ProcessDefinition method createProcessor.
@Override
public Processor createProcessor(RouteContext routeContext) {
Processor answer = processor;
if (processor == null) {
ObjectHelper.notNull(ref, "ref", this);
answer = routeContext.mandatoryLookup(getRef(), Processor.class);
}
// (a Processor must be a Service to be enlisted in JMX)
if (!(answer instanceof Service)) {
if (answer instanceof AsyncProcessor) {
// the processor is async by nature so use the async delegate
answer = new DelegateAsyncProcessor(answer);
} else {
// the processor is sync by nature so use the sync delegate
answer = new DelegateSyncProcessor(answer);
}
}
return answer;
}
Aggregations