Search in sources :

Example 1 with AsyncProducerCallback

use of org.apache.camel.AsyncProducerCallback in project camel by apache.

the class SendDynamicProcessor method process.

public boolean process(Exchange exchange, final AsyncCallback callback) {
    if (!isStarted()) {
        exchange.setException(new IllegalStateException("SendProcessor has not been started: " + this));
        callback.done(true);
        return true;
    }
    // we should preserve existing MEP so remember old MEP
    // if you want to permanently to change the MEP then use .setExchangePattern in the DSL
    final ExchangePattern existingPattern = exchange.getPattern();
    // which endpoint to send to
    final Endpoint endpoint;
    final ExchangePattern destinationExchangePattern;
    // use dynamic endpoint so calculate the endpoint to use
    Object recipient = null;
    try {
        recipient = expression.evaluate(exchange, Object.class);
        endpoint = resolveEndpoint(exchange, recipient);
        destinationExchangePattern = EndpointHelper.resolveExchangePatternFromUrl(endpoint.getEndpointUri());
    } 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;
    }
    // send the exchange to the destination using the producer cache
    return producerCache.doInAsyncProducer(endpoint, exchange, pattern, callback, new AsyncProducerCallback() {

        public boolean doInAsyncProducer(Producer producer, AsyncProcessor asyncProducer, final Exchange exchange, ExchangePattern pattern, final AsyncCallback callback) {
            final Exchange target = configureExchange(exchange, pattern, destinationExchangePattern, endpoint);
            LOG.debug(">>>> {} {}", endpoint, exchange);
            return asyncProducer.process(target, new AsyncCallback() {

                public void done(boolean doneSync) {
                    // restore previous MEP
                    target.setPattern(existingPattern);
                    // signal we are done
                    callback.done(doneSync);
                }
            });
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncProducerCallback(org.apache.camel.AsyncProducerCallback) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) ExchangePattern(org.apache.camel.ExchangePattern) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback)

Example 2 with AsyncProducerCallback

use of org.apache.camel.AsyncProducerCallback in project camel by apache.

the class RoutingSlip method processExchange.

protected boolean processExchange(final Endpoint endpoint, final Exchange exchange, final Exchange original, final AsyncCallback callback, final RoutingSlipIterator iter) {
    // this does the actual processing so log at trace level
    log.trace("Processing exchangeId: {} >>> {}", exchange.getExchangeId(), exchange);
    boolean sync = producerCache.doInAsyncProducer(endpoint, exchange, null, callback, new AsyncProducerCallback() {

        public boolean doInAsyncProducer(Producer producer, AsyncProcessor asyncProducer, final Exchange exchange, ExchangePattern exchangePattern, final AsyncCallback callback) {
            // rework error handling to support fine grained error handling
            RouteContext routeContext = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getRouteContext() : null;
            AsyncProcessor target = createErrorHandler(routeContext, exchange, asyncProducer, endpoint);
            // set property which endpoint we send to
            exchange.setProperty(Exchange.TO_ENDPOINT, endpoint.getEndpointUri());
            exchange.setProperty(Exchange.SLIP_ENDPOINT, endpoint.getEndpointUri());
            boolean answer = target.process(exchange, new AsyncCallback() {

                public void done(boolean doneSync) {
                    // we only have to handle async completion of the routing slip
                    if (doneSync) {
                        callback.done(doneSync);
                        return;
                    }
                    // continue processing the routing slip asynchronously
                    Exchange current = prepareExchangeForRoutingSlip(exchange, endpoint);
                    while (iter.hasNext(current)) {
                        // we ignore some kind of exceptions and allow us to continue
                        if (isIgnoreInvalidEndpoints()) {
                            FailedToCreateProducerException e = current.getException(FailedToCreateProducerException.class);
                            if (e != null) {
                                if (log.isDebugEnabled()) {
                                    log.debug("Endpoint uri is invalid: " + endpoint + ". This exception will be ignored.", e);
                                }
                                current.setException(null);
                            }
                        }
                        // check for error if so we should break out
                        if (!continueProcessing(current, "so breaking out of the routing slip", log)) {
                            break;
                        }
                        Endpoint endpoint;
                        try {
                            endpoint = resolveEndpoint(iter, exchange);
                            // if no endpoint was resolved then try the next
                            if (endpoint == null) {
                                continue;
                            }
                        } catch (Exception e) {
                            // error resolving endpoint so we should break out
                            exchange.setException(e);
                            break;
                        }
                        // prepare and process the routing slip
                        boolean sync = processExchange(endpoint, current, original, callback, iter);
                        current = prepareExchangeForRoutingSlip(current, endpoint);
                        if (!sync) {
                            log.trace("Processing exchangeId: {} is continued being processed asynchronously", original.getExchangeId());
                            return;
                        }
                    }
                    // logging nextExchange as it contains the exchange that might have altered the payload and since
                    // we are logging the completion if will be confusing if we log the original instead
                    // we could also consider logging the original and the nextExchange then we have *before* and *after* snapshots
                    log.trace("Processing complete for exchangeId: {} >>> {}", original.getExchangeId(), current);
                    // copy results back to the original exchange
                    ExchangeHelper.copyResults(original, current);
                    if (target instanceof DeadLetterChannel) {
                        Processor deadLetter = ((DeadLetterChannel) target).getDeadLetter();
                        try {
                            ServiceHelper.stopService(deadLetter);
                        } catch (Exception e) {
                            log.warn("Error stopping DeadLetterChannel error handler on routing slip. This exception is ignored.", e);
                        }
                    }
                    callback.done(false);
                }
            });
            // stop error handler if we completed synchronously
            if (answer) {
                if (target instanceof DeadLetterChannel) {
                    Processor deadLetter = ((DeadLetterChannel) target).getDeadLetter();
                    try {
                        ServiceHelper.stopService(deadLetter);
                    } catch (Exception e) {
                        log.warn("Error stopping DeadLetterChannel error handler on routing slip. This exception is ignored.", e);
                    }
                }
            }
            return answer;
        }
    });
    return sync;
}
Also used : Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) RouteContext(org.apache.camel.spi.RouteContext) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) AsyncProducerCallback(org.apache.camel.AsyncProducerCallback) Producer(org.apache.camel.Producer) Endpoint(org.apache.camel.Endpoint) ExchangePattern(org.apache.camel.ExchangePattern) AsyncProcessor(org.apache.camel.AsyncProcessor)

Example 3 with AsyncProducerCallback

use of org.apache.camel.AsyncProducerCallback in project camel by apache.

the class SendProcessor method process.

public boolean process(Exchange exchange, final AsyncCallback callback) {
    if (!isStarted()) {
        exchange.setException(new IllegalStateException("SendProcessor has not been started: " + this));
        callback.done(true);
        return true;
    }
    // we should preserve existing MEP so remember old MEP
    // if you want to permanently to change the MEP then use .setExchangePattern in the DSL
    final ExchangePattern existingPattern = exchange.getPattern();
    counter++;
    // if we have a producer then use that as its optimized
    if (producer != null) {
        // record timing for sending the exchange using the producer
        final StopWatch watch = new StopWatch();
        final Exchange target = configureExchange(exchange, pattern);
        EventHelper.notifyExchangeSending(exchange.getContext(), target, destination);
        LOG.debug(">>>> {} {}", destination, exchange);
        boolean sync = true;
        try {
            sync = producer.process(exchange, new AsyncCallback() {

                @Override
                public void done(boolean doneSync) {
                    try {
                        // restore previous MEP
                        target.setPattern(existingPattern);
                        // emit event that the exchange was sent to the endpoint
                        long timeTaken = watch.stop();
                        EventHelper.notifyExchangeSent(target.getContext(), target, destination, timeTaken);
                    } finally {
                        callback.done(doneSync);
                    }
                }
            });
        } catch (Throwable throwable) {
            exchange.setException(throwable);
            callback.done(sync);
        }
        return sync;
    }
    // send the exchange to the destination using the producer cache for the non optimized producers
    return producerCache.doInAsyncProducer(destination, exchange, pattern, callback, new AsyncProducerCallback() {

        public boolean doInAsyncProducer(Producer producer, AsyncProcessor asyncProducer, final Exchange exchange, ExchangePattern pattern, final AsyncCallback callback) {
            final Exchange target = configureExchange(exchange, pattern);
            LOG.debug(">>>> {} {}", destination, exchange);
            return asyncProducer.process(target, new AsyncCallback() {

                public void done(boolean doneSync) {
                    // restore previous MEP
                    target.setPattern(existingPattern);
                    // signal we are done
                    callback.done(doneSync);
                }
            });
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AsyncProducerCallback(org.apache.camel.AsyncProducerCallback) Producer(org.apache.camel.Producer) ExchangePattern(org.apache.camel.ExchangePattern) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback) StopWatch(org.apache.camel.util.StopWatch)

Aggregations

AsyncCallback (org.apache.camel.AsyncCallback)3 AsyncProcessor (org.apache.camel.AsyncProcessor)3 AsyncProducerCallback (org.apache.camel.AsyncProducerCallback)3 Exchange (org.apache.camel.Exchange)3 ExchangePattern (org.apache.camel.ExchangePattern)3 Producer (org.apache.camel.Producer)3 Endpoint (org.apache.camel.Endpoint)2 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)1 Processor (org.apache.camel.Processor)1 DefaultExchange (org.apache.camel.impl.DefaultExchange)1 RouteContext (org.apache.camel.spi.RouteContext)1 StopWatch (org.apache.camel.util.StopWatch)1