Search in sources :

Example 6 with RouteContext

use of org.apache.camel.spi.RouteContext in project camel by apache.

the class EndpointReferenceTest method testReferenceEndpointFromOtherCamelContext.

public void testReferenceEndpointFromOtherCamelContext() throws Exception {
    CamelContext context = applicationContext.getBean("camel2", CamelContext.class);
    RouteContext routeContext = new DefaultRouteContext(context);
    try {
        routeContext.resolveEndpoint(null, "endpoint1");
        fail("Should have thrown exception");
    } catch (NoSuchEndpointException exception) {
        assertTrue("Get a wrong exception message", exception.getMessage().contains("make sure the endpoint has the same camel context as the route does"));
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteContext(org.apache.camel.spi.RouteContext) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException)

Example 7 with RouteContext

use of org.apache.camel.spi.RouteContext in project camel by apache.

the class DefaultRuntimeEndpointRegistry method getRouteId.

private String getRouteId(Exchange exchange) {
    String answer = null;
    UnitOfWork uow = exchange.getUnitOfWork();
    RouteContext rc = uow != null ? uow.getRouteContext() : null;
    if (rc != null) {
        answer = rc.getRoute().getId();
    }
    if (answer == null) {
        // fallback and get from route id on the exchange
        answer = exchange.getFromRouteId();
    }
    return answer;
}
Also used : UnitOfWork(org.apache.camel.spi.UnitOfWork) RouteContext(org.apache.camel.spi.RouteContext)

Example 8 with RouteContext

use of org.apache.camel.spi.RouteContext 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 9 with RouteContext

use of org.apache.camel.spi.RouteContext in project camel by apache.

the class MulticastProcessor method createProcessorExchangePairs.

protected Iterable<ProcessorExchangePair> createProcessorExchangePairs(Exchange exchange) throws Exception {
    List<ProcessorExchangePair> result = new ArrayList<ProcessorExchangePair>(processors.size());
    StreamCache streamCache = null;
    if (isParallelProcessing() && exchange.getIn().getBody() instanceof StreamCache) {
        // in parallel processing case, the stream must be copied, therefore get the stream
        streamCache = (StreamCache) exchange.getIn().getBody();
    }
    int index = 0;
    for (Processor processor : processors) {
        // copy exchange, and do not share the unit of work
        Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false);
        if (streamCache != null) {
            if (index > 0) {
                // copy it otherwise parallel processing is not possible,
                // because streams can only be read once
                StreamCache copiedStreamCache = streamCache.copy(copy);
                if (copiedStreamCache != null) {
                    copy.getIn().setBody(copiedStreamCache);
                }
            }
        }
        // if it is not already set.
        if (copy.getProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK) == null) {
            copy.setProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK, exchange.getUnitOfWork());
        }
        // if we share unit of work, we need to prepare the child exchange
        if (isShareUnitOfWork()) {
            prepareSharedUnitOfWork(copy, exchange);
        }
        // and add the pair
        RouteContext routeContext = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getRouteContext() : null;
        result.add(createProcessorExchangePair(index++, processor, copy, routeContext));
    }
    if (exchange.getException() != null) {
        // before returning the answer;
        throw exchange.getException();
    }
    return result;
}
Also used : AtomicExchange(org.apache.camel.util.concurrent.AtomicExchange) Exchange(org.apache.camel.Exchange) AsyncProcessor(org.apache.camel.AsyncProcessor) Processor(org.apache.camel.Processor) StreamCache(org.apache.camel.StreamCache) ArrayList(java.util.ArrayList) Endpoint(org.apache.camel.Endpoint) RouteContext(org.apache.camel.spi.RouteContext)

Example 10 with RouteContext

use of org.apache.camel.spi.RouteContext in project camel by apache.

the class TemporalRule method getOverdueAction.

public Processor getOverdueAction() throws Exception {
    if (overdueAction == null && overdueProcessors != null) {
        RouteDefinition route = new RouteDefinition();
        RouteContext routeContext = new DefaultRouteContext(first.getBuilder().getProcessBuilder().getContext(), route, null, new ArrayList<Route>());
        overdueAction = overdueProcessors.createOutputsProcessor(routeContext);
    }
    return overdueAction;
}
Also used : DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteDefinition(org.apache.camel.model.RouteDefinition) Route(org.apache.camel.Route) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteContext(org.apache.camel.spi.RouteContext)

Aggregations

RouteContext (org.apache.camel.spi.RouteContext)13 Processor (org.apache.camel.Processor)6 Exchange (org.apache.camel.Exchange)4 DefaultRouteContext (org.apache.camel.impl.DefaultRouteContext)4 ArrayList (java.util.ArrayList)3 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)3 ProcessorFactory (org.apache.camel.spi.ProcessorFactory)3 AsyncProcessor (org.apache.camel.AsyncProcessor)2 CamelContext (org.apache.camel.CamelContext)2 Endpoint (org.apache.camel.Endpoint)2 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)2 FilterProcessor (org.apache.camel.processor.FilterProcessor)2 UnitOfWork (org.apache.camel.spi.UnitOfWork)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 StringTokenizer (java.util.StringTokenizer)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AsyncCallback (org.apache.camel.AsyncCallback)1 AsyncProducerCallback (org.apache.camel.AsyncProducerCallback)1