use of org.apache.camel.AsyncCallback in project camel by apache.
the class Debug method wrapProcessorInInterceptors.
public Processor wrapProcessorInInterceptors(final CamelContext context, final ProcessorDefinition<?> definition, final Processor target, final Processor nextTarget) throws Exception {
return new DelegateAsyncProcessor(target) {
@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
debugger.beforeProcess(exchange, target, definition);
final StopWatch watch = new StopWatch();
return processor.process(exchange, new AsyncCallback() {
public void done(boolean doneSync) {
long diff = watch.stop();
debugger.afterProcess(exchange, processor, definition, diff);
// must notify original callback
callback.done(doneSync);
}
});
}
@Override
public String toString() {
return "Debug[" + target + "]";
}
};
}
use of org.apache.camel.AsyncCallback in project camel by apache.
the class WebsocketConsumer method sendEventNotification.
public void sendEventNotification(String connectionKey, int eventType) {
final Exchange exchange = getEndpoint().createExchange();
// set header
exchange.getIn().setHeader(WebsocketConstants.CONNECTION_KEY, connectionKey);
exchange.getIn().setHeader(WebsocketConstants.EVENT_TYPE, eventType);
for (Map.Entry<String, String> param : queryMap.entrySet()) {
exchange.getIn().setHeader(param.getKey(), param.getValue());
}
// 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 WebsocketConsumer method sendNotDeliveredMessage.
public void sendNotDeliveredMessage(List<String> failedConnectionKeys, Object message) {
final Exchange exchange = getEndpoint().createExchange();
// set header and body
exchange.getIn().setHeader(WebsocketConstants.CONNECTION_KEY_LIST, failedConnectionKeys);
exchange.getIn().setHeader(WebsocketConstants.ERROR_TYPE, WebsocketConstants.MESSAGE_NOT_SENT_ERROR_TYPE);
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 SqsConsumer method processBatch.
public int processBatch(Queue<Object> exchanges) throws Exception {
int total = exchanges.size();
for (int index = 0; index < total && isBatchAllowed(); index++) {
// only loop if we are started (allowed to run)
final Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll());
// add current index and total as properties
exchange.setProperty(Exchange.BATCH_INDEX, index);
exchange.setProperty(Exchange.BATCH_SIZE, total);
exchange.setProperty(Exchange.BATCH_COMPLETE, index == total - 1);
// update pending number of exchanges
pendingExchanges = total - index - 1;
// schedule task to extend visibility if enabled
Integer visibilityTimeout = getConfiguration().getVisibilityTimeout();
if (this.scheduledExecutor != null && visibilityTimeout != null && (visibilityTimeout.intValue() / 2) > 0) {
int delay = visibilityTimeout.intValue() / 2;
int period = visibilityTimeout.intValue();
int repeatSeconds = new Double(visibilityTimeout.doubleValue() * 1.5).intValue();
if (LOG.isDebugEnabled()) {
LOG.debug("Scheduled TimeoutExtender task to start after {} delay, and run with {}/{} period/repeat (seconds), to extend exchangeId: {}", new Object[] { delay, period, repeatSeconds, exchange.getExchangeId() });
}
final ScheduledFuture<?> scheduledFuture = this.scheduledExecutor.scheduleAtFixedRate(new TimeoutExtender(exchange, repeatSeconds), delay, period, TimeUnit.SECONDS);
exchange.addOnCompletion(new Synchronization() {
@Override
public void onComplete(Exchange exchange) {
cancelExtender(exchange);
}
@Override
public void onFailure(Exchange exchange) {
cancelExtender(exchange);
}
private void cancelExtender(Exchange exchange) {
// cancel task as we are done
LOG.trace("Processing done so cancelling TimeoutExtender task for exchangeId: {}", exchange.getExchangeId());
scheduledFuture.cancel(true);
}
});
}
// add on completion to handle after work when the exchange is done
exchange.addOnCompletion(new Synchronization() {
public void onComplete(Exchange exchange) {
processCommit(exchange);
}
public void onFailure(Exchange exchange) {
processRollback(exchange);
}
@Override
public String toString() {
return "SqsConsumerOnCompletion";
}
});
LOG.trace("Processing exchange [{}]...", exchange);
getAsyncProcessor().process(exchange, new AsyncCallback() {
@Override
public void done(boolean doneSync) {
LOG.trace("Processing exchange [{}] done.", exchange);
}
});
}
return total;
}
use of org.apache.camel.AsyncCallback in project camel by apache.
the class KinesisConsumer method processBatch.
@Override
public int processBatch(Queue<Object> exchanges) throws Exception {
int processedExchanges = 0;
while (!exchanges.isEmpty()) {
final Exchange exchange = ObjectHelper.cast(Exchange.class, exchanges.poll());
LOG.trace("Processing exchange [{}] started.", exchange);
getAsyncProcessor().process(exchange, new AsyncCallback() {
@Override
public void done(boolean doneSync) {
LOG.trace("Processing exchange [{}] done.", exchange);
}
});
processedExchanges++;
}
return processedExchanges;
}
Aggregations