Search in sources :

Example 1 with RouteContext

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

the class ExpressionBuilder method routeIdExpression.

/**
     * Returns an Expression for the route id
     */
public static Expression routeIdExpression() {
    return new ExpressionAdapter() {

        public Object evaluate(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;
        }

        @Override
        public String toString() {
            return "routeId";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) UnitOfWork(org.apache.camel.spi.UnitOfWork) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter) RouteContext(org.apache.camel.spi.RouteContext)

Example 2 with RouteContext

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

the class RecipientListProcessor method createProcessorExchangePair.

/**
     * This logic is similar to MulticastProcessor but we have to return a RecipientProcessorExchangePair instead
     */
protected ProcessorExchangePair createProcessorExchangePair(int index, Endpoint endpoint, Producer producer, Exchange exchange, ExchangePattern pattern) {
    Processor prepared = producer;
    // copy exchange, and do not share the unit of work
    Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false);
    // if we share unit of work, we need to prepare the child exchange
    if (isShareUnitOfWork()) {
        prepareSharedUnitOfWork(copy, exchange);
    }
    // set property which endpoint we send to
    setToEndpoint(copy, prepared);
    // rework error handling to support fine grained error handling
    RouteContext routeContext = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getRouteContext() : null;
    prepared = createErrorHandler(routeContext, copy, prepared);
    // invoke on prepare on the exchange if specified
    if (onPrepare != null) {
        try {
            onPrepare.process(copy);
        } catch (Exception e) {
            copy.setException(e);
        }
    }
    // and create the pair
    return new RecipientProcessorExchangePair(index, producerCache, endpoint, producer, prepared, copy, pattern);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RouteContext(org.apache.camel.spi.RouteContext)

Example 3 with RouteContext

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

the class RouteDefinition method addRoutes.

// Implementation methods
// -------------------------------------------------------------------------
protected RouteContext addRoutes(CamelContext camelContext, Collection<Route> routes, FromDefinition fromType) throws Exception {
    RouteContext routeContext = new DefaultRouteContext(camelContext, this, fromType, routes);
    // configure tracing
    if (trace != null) {
        Boolean isTrace = CamelContextHelper.parseBoolean(camelContext, getTrace());
        if (isTrace != null) {
            routeContext.setTracing(isTrace);
            if (isTrace) {
                log.debug("Tracing is enabled on route: {}", getId());
            // tracing is added in the DefaultChannel so we can enable it on the fly
            }
        }
    }
    // configure message history
    if (messageHistory != null) {
        Boolean isMessageHistory = CamelContextHelper.parseBoolean(camelContext, getMessageHistory());
        if (isMessageHistory != null) {
            routeContext.setMessageHistory(isMessageHistory);
            if (isMessageHistory) {
                log.debug("Message history is enabled on route: {}", getId());
            }
        }
    }
    // configure stream caching
    if (streamCache != null) {
        Boolean isStreamCache = CamelContextHelper.parseBoolean(camelContext, getStreamCache());
        if (isStreamCache != null) {
            routeContext.setStreamCaching(isStreamCache);
            if (isStreamCache) {
                log.debug("StreamCaching is enabled on route: {}", getId());
            }
        }
    }
    // configure handle fault
    if (handleFault != null) {
        Boolean isHandleFault = CamelContextHelper.parseBoolean(camelContext, getHandleFault());
        if (isHandleFault != null) {
            routeContext.setHandleFault(isHandleFault);
            if (isHandleFault) {
                log.debug("HandleFault is enabled on route: {}", getId());
                // only add a new handle fault if not already a global configured on camel context
                if (HandleFault.getHandleFault(camelContext) == null) {
                    addInterceptStrategy(new HandleFault());
                }
            }
        }
    }
    // configure delayer
    if (delayer != null) {
        Long delayer = CamelContextHelper.parseLong(camelContext, getDelayer());
        if (delayer != null) {
            routeContext.setDelayer(delayer);
            if (delayer > 0) {
                log.debug("Delayer is enabled with: {} ms. on route: {}", delayer, getId());
            } else {
                log.debug("Delayer is disabled on route: {}", getId());
            }
        }
    }
    // configure route policy
    if (routePolicies != null && !routePolicies.isEmpty()) {
        for (RoutePolicy policy : routePolicies) {
            log.debug("RoutePolicy is enabled: {} on route: {}", policy, getId());
            routeContext.getRoutePolicyList().add(policy);
        }
    }
    if (routePolicyRef != null) {
        StringTokenizer policyTokens = new StringTokenizer(routePolicyRef, ",");
        while (policyTokens.hasMoreTokens()) {
            String ref = policyTokens.nextToken().trim();
            RoutePolicy policy = CamelContextHelper.mandatoryLookup(camelContext, ref, RoutePolicy.class);
            log.debug("RoutePolicy is enabled: {} on route: {}", policy, getId());
            routeContext.getRoutePolicyList().add(policy);
        }
    }
    if (camelContext.getRoutePolicyFactories() != null) {
        for (RoutePolicyFactory factory : camelContext.getRoutePolicyFactories()) {
            RoutePolicy policy = factory.createRoutePolicy(camelContext, getId(), this);
            if (policy != null) {
                log.debug("RoutePolicy is enabled: {} on route: {}", policy, getId());
                routeContext.getRoutePolicyList().add(policy);
            }
        }
    }
    // configure auto startup
    Boolean isAutoStartup = CamelContextHelper.parseBoolean(camelContext, getAutoStartup());
    if (isAutoStartup != null) {
        log.debug("Using AutoStartup {} on route: {}", isAutoStartup, getId());
        routeContext.setAutoStartup(isAutoStartup);
    }
    // configure shutdown
    if (shutdownRoute != null) {
        log.debug("Using ShutdownRoute {} on route: {}", getShutdownRoute(), getId());
        routeContext.setShutdownRoute(getShutdownRoute());
    }
    if (shutdownRunningTask != null) {
        log.debug("Using ShutdownRunningTask {} on route: {}", getShutdownRunningTask(), getId());
        routeContext.setShutdownRunningTask(getShutdownRunningTask());
    }
    // should inherit the intercept strategies we have defined
    routeContext.setInterceptStrategies(this.getInterceptStrategies());
    // force endpoint resolution
    routeContext.getEndpoint();
    for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
        strategy.onRouteContextCreate(routeContext);
    }
    // validate route has output processors
    if (!ProcessorDefinitionHelper.hasOutputs(outputs, true)) {
        RouteDefinition route = routeContext.getRoute();
        String at = fromType.toString();
        Exception cause = new IllegalArgumentException("Route " + route.getId() + " has no output processors." + " You need to add outputs to the route such as to(\"log:foo\").");
        throw new FailedToCreateRouteException(route.getId(), route.toString(), at, cause);
    }
    List<ProcessorDefinition<?>> list = new ArrayList<ProcessorDefinition<?>>(outputs);
    for (ProcessorDefinition<?> output : list) {
        try {
            output.addRoutes(routeContext, routes);
        } catch (Exception e) {
            RouteDefinition route = routeContext.getRoute();
            throw new FailedToCreateRouteException(route.getId(), route.toString(), output.toString(), e);
        }
    }
    routeContext.commit();
    return routeContext;
}
Also used : DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) ArrayList(java.util.ArrayList) RoutePolicy(org.apache.camel.spi.RoutePolicy) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteContext(org.apache.camel.spi.RouteContext) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) StringTokenizer(java.util.StringTokenizer) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) RoutePolicyFactory(org.apache.camel.spi.RoutePolicyFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HandleFault(org.apache.camel.processor.interceptor.HandleFault)

Example 4 with RouteContext

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

the class RouteDefinition method addRoutes.

public List<RouteContext> addRoutes(ModelCamelContext camelContext, Collection<Route> routes) throws Exception {
    List<RouteContext> answer = new ArrayList<RouteContext>();
    @SuppressWarnings("deprecation") ErrorHandlerFactory handler = camelContext.getErrorHandlerBuilder();
    if (handler != null) {
        setErrorHandlerBuilderIfNull(handler);
    }
    for (FromDefinition fromType : inputs) {
        RouteContext routeContext;
        try {
            routeContext = addRoutes(camelContext, routes, fromType);
        } catch (FailedToCreateRouteException e) {
            throw e;
        } catch (Exception e) {
            // wrap in exception which provide more details about which route was failing
            throw new FailedToCreateRouteException(getId(), toString(), e);
        }
        answer.add(routeContext);
    }
    return answer;
}
Also used : FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) ArrayList(java.util.ArrayList) ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteContext(org.apache.camel.spi.RouteContext)

Example 5 with RouteContext

use of org.apache.camel.spi.RouteContext in project fabric8 by jboss-fuse.

the class Tracer method forceAutoAssigningIds.

private void forceAutoAssigningIds() {
    for (RouteContext routeContext : routeContexts) {
        CamelContext camelContext = routeContext.getCamelContext();
        NodeIdFactory factory = camelContext.getNodeIdFactory();
        if (factory != null) {
            for (ProcessorDefinition<?> child : processors) {
                // ensure also the children get ids assigned
                RouteDefinitionHelper.forceAssignIds(camelContext, child);
            }
        }
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) NodeIdFactory(org.apache.camel.spi.NodeIdFactory) 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