use of org.apache.camel.AsyncCallback in project camel by apache.
the class AsyncProcessorHelper method process.
/**
* Calls the async version of the processor's process method.
* <p/>
* This implementation supports transacted {@link Exchange}s which ensure those are run in a synchronous fashion.
* See more details at {@link org.apache.camel.AsyncProcessor}.
*
* @param processor the processor
* @param exchange the exchange
* @param callback the callback
* @return <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
* @deprecated should no longer be needed, instead invoke the process method on the {@link AsyncProcessor} directly,
* instead of using this method.
*/
@Deprecated
public static boolean process(final AsyncProcessor processor, final Exchange exchange, final AsyncCallback callback) {
boolean sync;
if (exchange.isTransacted()) {
// must be synchronized for transacted exchanges
LOG.trace("Transacted Exchange must be routed synchronously for exchangeId: {} -> {}", exchange.getExchangeId(), exchange);
try {
process(processor, exchange);
} catch (Throwable e) {
exchange.setException(e);
}
callback.done(true);
sync = true;
} else {
final UnitOfWork uow = exchange.getUnitOfWork();
// allow unit of work to wrap callback in case it need to do some special work
// for example the MDCUnitOfWork
AsyncCallback async = callback;
if (uow != null) {
async = uow.beforeProcess(processor, exchange, callback);
}
// we support asynchronous routing so invoke it
sync = processor.process(exchange, async);
// execute any after processor work (in current thread, not in the callback)
if (uow != null) {
uow.afterProcess(processor, exchange, callback, sync);
}
}
if (LOG.isTraceEnabled()) {
LOG.trace("Exchange processed and is continued routed {} for exchangeId: {} -> {}", new Object[] { sync ? "synchronously" : "asynchronously", exchange.getExchangeId(), exchange });
}
return sync;
}
use of org.apache.camel.AsyncCallback in project camel by apache.
the class WebsocketConsumer method sendMessage.
public void sendMessage(final String connectionKey, Object message) {
final Exchange exchange = getEndpoint().createExchange();
// set header and body
exchange.getIn().setHeader(WebsocketConstants.CONNECTION_KEY, connectionKey);
exchange.getIn().setBody(message);
// send exchange using the async routing engine
getAsyncProcessor().process(exchange, new AsyncCallback() {
public void done(boolean doneSync) {
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
}
}
});
}
use of org.apache.camel.AsyncCallback in project camel by apache.
the class SchedulerConsumer method sendTimerExchange.
protected int sendTimerExchange() {
final Exchange exchange = getEndpoint().createExchange();
exchange.setProperty(Exchange.TIMER_NAME, getEndpoint().getName());
Date now = new Date();
exchange.setProperty(Exchange.TIMER_FIRED_TIME, now);
if (log.isTraceEnabled()) {
log.trace("Timer {} is firing", getEndpoint().getName());
}
if (!getEndpoint().isSynchronous()) {
getAsyncProcessor().process(exchange, new AsyncCallback() {
@Override
public void done(boolean doneSync) {
// handle any thrown exception
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
}
}
});
} else {
try {
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
}
// handle any thrown exception
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
}
}
// a property can be used to control if the scheduler polled a message or not
// for example to overrule and indicate no message was polled, which can affect the scheduler
// to leverage backoff on idle etc.
boolean polled = exchange.getProperty(Exchange.SCHEDULER_POLLED_MESSAGES, true, boolean.class);
return polled ? 1 : 0;
}
use of org.apache.camel.AsyncCallback in project camel by apache.
the class TimerConsumer method sendTimerExchange.
protected void sendTimerExchange(long counter) {
final Exchange exchange = endpoint.createExchange();
exchange.setProperty(Exchange.TIMER_COUNTER, counter);
exchange.setProperty(Exchange.TIMER_NAME, endpoint.getTimerName());
exchange.setProperty(Exchange.TIMER_TIME, endpoint.getTime());
exchange.setProperty(Exchange.TIMER_PERIOD, endpoint.getPeriod());
Date now = new Date();
exchange.setProperty(Exchange.TIMER_FIRED_TIME, now);
// also set now on in header with same key as quartz to be consistent
exchange.getIn().setHeader("firedTime", now);
if (LOG.isTraceEnabled()) {
LOG.trace("Timer {} is firing #{} count", endpoint.getTimerName(), counter);
}
if (!endpoint.isSynchronous()) {
getAsyncProcessor().process(exchange, new AsyncCallback() {
@Override
public void done(boolean doneSync) {
// handle any thrown exception
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
}
}
});
} else {
try {
getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
}
// handle any thrown exception
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
}
}
}
use of org.apache.camel.AsyncCallback in project camel by apache.
the class WsConsumer method sendMessageInternal.
private void sendMessageInternal(Object message) {
final Exchange exchange = getEndpoint().createExchange();
if (message instanceof Throwable) {
exchange.setException((Throwable) message);
} else {
exchange.getIn().setBody(message);
}
// send exchange using the async routing engine
getAsyncProcessor().process(exchange, new AsyncCallback() {
public void done(boolean doneSync) {
if (exchange.getException() != null) {
getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
}
}
});
}
Aggregations