}
@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
if (!isRunAllowed()) {
exchange.setException(new RejectedExecutionException("Run is not allowed"));
callback.done(true);
return true;
}
// calculate delay and wait
long delay;
try {
delay = calculateDelay(exchange);
if (delay <= 0) {
// no delay then continue routing
log.trace("No delay for exchangeId: {}", exchange.getExchangeId());
return super.process(exchange, callback);
}
} catch (Throwable e) {
exchange.setException(e);
callback.done(true);
return true;
}
if (!isAsyncDelayed() || exchange.isTransacted()) {
// use synchronous delay (also required if using transactions)
try {
delay(delay, exchange);
// then continue routing
return super.process(exchange, callback);
} catch (Exception e) {
// exception occurred so we are done
exchange.setException(e);
callback.done(true);
return true;
}
} else {
// asynchronous delay so schedule a process call task
ProcessCall call = new ProcessCall(exchange, callback);
try {
log.trace("Scheduling delayed task to run in {} millis for exchangeId: {}",
delay, exchange.getExchangeId());
executorService.schedule(call, delay, TimeUnit.MILLISECONDS);
// tell Camel routing engine we continue routing asynchronous
return false;
} catch (RejectedExecutionException e) {
if (isCallerRunsWhenRejected()) {
if (!isRunAllowed()) {
exchange.setException(new RejectedExecutionException());
} else {
log.debug("Scheduling rejected task, so letting caller run, delaying at first for {} millis for exchangeId: {}", delay, exchange.getExchangeId());
// let caller run by processing
try {
delay(delay, exchange);