Search in sources :

Example 51 with AsyncCallback

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 + "]";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) DelegateAsyncProcessor(org.apache.camel.processor.DelegateAsyncProcessor) StopWatch(org.apache.camel.util.StopWatch)

Example 52 with AsyncCallback

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());
            }
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) HashMap(java.util.HashMap) Map(java.util.Map)

Example 53 with AsyncCallback

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());
            }
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback)

Example 54 with AsyncCallback

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;
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback) Synchronization(org.apache.camel.spi.Synchronization)

Example 55 with AsyncCallback

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;
}
Also used : Exchange(org.apache.camel.Exchange) AsyncCallback(org.apache.camel.AsyncCallback)

Aggregations

AsyncCallback (org.apache.camel.AsyncCallback)67 Exchange (org.apache.camel.Exchange)47 AsyncProcessor (org.apache.camel.AsyncProcessor)12 CamelExchangeException (org.apache.camel.CamelExchangeException)8 Message (org.apache.camel.Message)5 Processor (org.apache.camel.Processor)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 Endpoint (org.apache.camel.Endpoint)4 Producer (org.apache.camel.Producer)4 Synchronization (org.apache.camel.spi.Synchronization)4 StopWatch (org.apache.camel.util.StopWatch)4 InetSocketAddress (java.net.InetSocketAddress)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ExchangePattern (org.apache.camel.ExchangePattern)3 ChannelHandler (io.netty.channel.ChannelHandler)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ConnectException (java.net.ConnectException)2