use of org.apache.camel.impl.BridgeExceptionHandlerToErrorHandler in project camel by apache.
the class PollEnricher method process.
/**
* Enriches the input data (<code>exchange</code>) by first obtaining
* additional data from an endpoint represented by an endpoint
* <code>producer</code> and second by aggregating input data and additional
* data. Aggregation of input data and additional data is delegated to an
* {@link org.apache.camel.processor.aggregate.AggregationStrategy} object set at construction time. If the
* message exchange with the resource endpoint fails then no aggregation
* will be done and the failed exchange content is copied over to the
* original message exchange.
*
* @param exchange input data.
*/
@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
try {
preCheckPoll(exchange);
} catch (Exception e) {
exchange.setException(new CamelExchangeException("Error during pre poll check", exchange, e));
callback.done(true);
return true;
}
// which consumer to use
PollingConsumer consumer;
Endpoint endpoint;
// use dynamic endpoint so calculate the endpoint to use
Object recipient = null;
try {
recipient = expression.evaluate(exchange, Object.class);
endpoint = resolveEndpoint(exchange, recipient);
// acquire the consumer from the cache
consumer = consumerCache.acquirePollingConsumer(endpoint);
} catch (Throwable e) {
if (isIgnoreInvalidEndpoint()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Endpoint uri is invalid: " + recipient + ". This exception will be ignored.", e);
}
} else {
exchange.setException(e);
}
callback.done(true);
return true;
}
// grab the real delegate consumer that performs the actual polling
Consumer delegate = consumer;
if (consumer instanceof EventDrivenPollingConsumer) {
delegate = ((EventDrivenPollingConsumer) consumer).getDelegateConsumer();
}
// is the consumer bridging the error handler?
boolean bridgeErrorHandler = false;
if (delegate instanceof DefaultConsumer) {
ExceptionHandler handler = ((DefaultConsumer) delegate).getExceptionHandler();
if (handler != null && handler instanceof BridgeExceptionHandlerToErrorHandler) {
bridgeErrorHandler = true;
}
}
Exchange resourceExchange;
try {
if (timeout < 0) {
LOG.debug("Consumer receive: {}", consumer);
resourceExchange = consumer.receive();
} else if (timeout == 0) {
LOG.debug("Consumer receiveNoWait: {}", consumer);
resourceExchange = consumer.receiveNoWait();
} else {
LOG.debug("Consumer receive with timeout: {} ms. {}", timeout, consumer);
resourceExchange = consumer.receive(timeout);
}
if (resourceExchange == null) {
LOG.debug("Consumer received no exchange");
} else {
LOG.debug("Consumer received: {}", resourceExchange);
}
} catch (Exception e) {
exchange.setException(new CamelExchangeException("Error during poll", exchange, e));
callback.done(true);
return true;
} finally {
// return the consumer back to the cache
consumerCache.releasePollingConsumer(endpoint, consumer);
}
// remember current redelivery stats
Object redeliveried = exchange.getIn().getHeader(Exchange.REDELIVERED);
Object redeliveryCounter = exchange.getIn().getHeader(Exchange.REDELIVERY_COUNTER);
Object redeliveryMaxCounter = exchange.getIn().getHeader(Exchange.REDELIVERY_MAX_COUNTER);
// if we are bridging error handler and failed then remember the caused exception
Throwable cause = null;
if (resourceExchange != null && bridgeErrorHandler) {
cause = resourceExchange.getException();
}
try {
if (!isAggregateOnException() && (resourceExchange != null && resourceExchange.isFailed())) {
// copy resource exchange onto original exchange (preserving pattern)
// and preserve redelivery headers
copyResultsPreservePattern(exchange, resourceExchange);
} else {
prepareResult(exchange);
// prepare the exchanges for aggregation
ExchangeHelper.prepareAggregation(exchange, resourceExchange);
// must catch any exception from aggregation
Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
if (aggregatedExchange != null) {
// copy aggregation result onto original exchange (preserving pattern)
copyResultsPreservePattern(exchange, aggregatedExchange);
// handover any synchronization
if (resourceExchange != null) {
resourceExchange.handoverCompletions(exchange);
}
}
}
// if we failed then restore caused exception
if (cause != null) {
// restore caused exception
exchange.setException(cause);
// remove the exhausted marker as we want to be able to perform redeliveries with the error handler
exchange.removeProperties(Exchange.REDELIVERY_EXHAUSTED);
// preserve the redelivery stats
if (redeliveried != null) {
if (exchange.hasOut()) {
exchange.getOut().setHeader(Exchange.REDELIVERED, redeliveried);
} else {
exchange.getIn().setHeader(Exchange.REDELIVERED, redeliveried);
}
}
if (redeliveryCounter != null) {
if (exchange.hasOut()) {
exchange.getOut().setHeader(Exchange.REDELIVERY_COUNTER, redeliveryCounter);
} else {
exchange.getIn().setHeader(Exchange.REDELIVERY_COUNTER, redeliveryCounter);
}
}
if (redeliveryMaxCounter != null) {
if (exchange.hasOut()) {
exchange.getOut().setHeader(Exchange.REDELIVERY_MAX_COUNTER, redeliveryMaxCounter);
} else {
exchange.getIn().setHeader(Exchange.REDELIVERY_MAX_COUNTER, redeliveryMaxCounter);
}
}
}
// set header with the uri of the endpoint enriched so we can use that for tracing etc
if (exchange.hasOut()) {
exchange.getOut().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
} else {
exchange.getIn().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
}
} catch (Throwable e) {
exchange.setException(new CamelExchangeException("Error occurred during aggregation", exchange, e));
callback.done(true);
return true;
}
callback.done(true);
return true;
}
Aggregations