Search in sources :

Example 66 with Route

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

the class RouteBuilderTest method testSplitter.

public void testSplitter() throws Exception {
    List<Route> routes = buildSplitter();
    log.debug("Created routes: " + routes);
    assertEquals("Number routes created", 1, routes.size());
    for (Route route : routes) {
        Endpoint key = route.getEndpoint();
        assertEquals("From endpoint", "direct://a", key.getEndpointUri());
        EventDrivenConsumerRoute consumer = assertIsInstanceOf(EventDrivenConsumerRoute.class, route);
        Channel channel = unwrapChannel(consumer.getProcessor());
        assertIsInstanceOf(Splitter.class, channel.getNextProcessor());
    }
}
Also used : Endpoint(org.apache.camel.Endpoint) Channel(org.apache.camel.Channel) DeadLetterChannel(org.apache.camel.processor.DeadLetterChannel) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute) Route(org.apache.camel.Route) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute)

Example 67 with Route

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

the class ContextErrorHandlerTest method testOverloadingTheDefaultErrorHandler.

public void testOverloadingTheDefaultErrorHandler() throws Exception {
    RouteBuilder builder = new RouteBuilder() {

        public void configure() {
            errorHandler(loggingErrorHandler("FOO.BAR"));
            from("seda:a").to("seda:b");
        }
    };
    List<Route> list = getRouteListWithCurrentContext(builder);
    assertEquals("Number routes created" + list, 1, list.size());
    for (Route route : list) {
        Endpoint key = route.getEndpoint();
        assertEquals("From endpoint", "seda://a", key.getEndpointUri());
        EventDrivenConsumerRoute consumerRoute = assertIsInstanceOf(EventDrivenConsumerRoute.class, route);
        Processor processor = consumerRoute.getProcessor();
        Channel channel = unwrapChannel(processor);
        assertIsInstanceOf(LoggingErrorHandler.class, channel.getErrorHandler());
        SendProcessor sendProcessor = assertIsInstanceOf(SendProcessor.class, channel.getNextProcessor());
        log.debug("Found sendProcessor: " + sendProcessor);
    }
}
Also used : Processor(org.apache.camel.Processor) SendProcessor(org.apache.camel.processor.SendProcessor) Endpoint(org.apache.camel.Endpoint) Channel(org.apache.camel.Channel) DeadLetterChannel(org.apache.camel.processor.DeadLetterChannel) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute) SendProcessor(org.apache.camel.processor.SendProcessor) Route(org.apache.camel.Route) EventDrivenConsumerRoute(org.apache.camel.impl.EventDrivenConsumerRoute)

Example 68 with Route

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

the class DefaultCamelContext method start.

@Override
public void start() throws Exception {
    try (MDCHelper mdcHelper = new MDCHelper()) {
        vetoStated.set(false);
        startDate = new Date();
        stopWatch.restart();
        log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") is starting");
        // Note: This is done on context start as we want to avoid doing it during object construction
        // where we could be dealing with CDI proxied camel contexts which may never be started (CAMEL-9657)
        // [TODO] Remove in 3.0
        Container.Instance.manage(this);
        doNotStartRoutesOnFirstStart = !firstStartDone && !isAutoStartup();
        // then we may need to start the routes on the 2nd start call
        if (firstStartDone && !isAutoStartup() && isStarted()) {
            // invoke this logic to warm up the routes and if possible also start the routes
            doStartOrResumeRoutes(routeServices, true, true, false, true);
        }
        // super will invoke doStart which will prepare internal services and start routes etc.
        try {
            firstStartDone = true;
            super.start();
        } catch (VetoCamelContextStartException e) {
            // mark we veto against starting Camel
            vetoStated.set(true);
            if (e.isRethrowException()) {
                throw e;
            } else {
                log.info("CamelContext ({}) vetoed to not start due {}", getName(), e.getMessage());
                // swallow exception and change state of this camel context to stopped
                stop();
                return;
            }
        }
        stopWatch.stop();
        if (log.isInfoEnabled()) {
            // count how many routes are actually started
            int started = 0;
            for (Route route : getRoutes()) {
                if (getRouteStatus(route.getId()).isStarted()) {
                    started++;
                }
            }
            log.info("Total " + getRoutes().size() + " routes, of which " + started + " are started.");
            log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") started in " + TimeUtils.printDuration(stopWatch.taken()));
        }
        EventHelper.notifyCamelContextStarted(this);
    }
}
Also used : VetoCamelContextStartException(org.apache.camel.VetoCamelContextStartException) Date(java.util.Date) Endpoint(org.apache.camel.Endpoint) ShutdownRoute(org.apache.camel.ShutdownRoute) Route(org.apache.camel.Route)

Example 69 with Route

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

the class DefaultCamelContext method doStartOrResumeRouteConsumers.

private void doStartOrResumeRouteConsumers(Map<Integer, DefaultRouteStartupOrder> inputs, boolean resumeOnly, boolean addingRoute) throws Exception {
    List<Endpoint> routeInputs = new ArrayList<Endpoint>();
    for (Map.Entry<Integer, DefaultRouteStartupOrder> entry : inputs.entrySet()) {
        Integer order = entry.getKey();
        Route route = entry.getValue().getRoute();
        RouteService routeService = entry.getValue().getRouteService();
        // if we are starting camel, then skip routes which are configured to not be auto started
        boolean autoStartup = routeService.getRouteDefinition().isAutoStartup(this) && this.isAutoStartup();
        if (addingRoute && !autoStartup) {
            log.info("Skipping starting of route " + routeService.getId() + " as its configured with autoStartup=false");
            continue;
        }
        // start the service
        for (Consumer consumer : routeService.getInputs().values()) {
            Endpoint endpoint = consumer.getEndpoint();
            // check multiple consumer violation, with the other routes to be started
            if (!doCheckMultipleConsumerSupportClash(endpoint, routeInputs)) {
                throw new FailedToStartRouteException(routeService.getId(), "Multiple consumers for the same endpoint is not allowed: " + endpoint);
            }
            // check for multiple consumer violations with existing routes which
            // have already been started, or is currently starting
            List<Endpoint> existingEndpoints = new ArrayList<Endpoint>();
            for (Route existingRoute : getRoutes()) {
                if (route.getId().equals(existingRoute.getId())) {
                    // skip ourselves
                    continue;
                }
                Endpoint existing = existingRoute.getEndpoint();
                ServiceStatus status = getRouteStatus(existingRoute.getId());
                if (status != null && (status.isStarted() || status.isStarting())) {
                    existingEndpoints.add(existing);
                }
            }
            if (!doCheckMultipleConsumerSupportClash(endpoint, existingEndpoints)) {
                throw new FailedToStartRouteException(routeService.getId(), "Multiple consumers for the same endpoint is not allowed: " + endpoint);
            }
            // start the consumer on the route
            log.debug("Route: {} >>> {}", route.getId(), route);
            if (resumeOnly) {
                log.debug("Resuming consumer (order: {}) on route: {}", order, route.getId());
            } else {
                log.debug("Starting consumer (order: {}) on route: {}", order, route.getId());
            }
            if (resumeOnly && route.supportsSuspension()) {
                // if we are resuming and the route can be resumed
                ServiceHelper.resumeService(consumer);
                log.info("Route: " + route.getId() + " resumed and consuming from: " + endpoint);
            } else {
                // when starting we should invoke the lifecycle strategies
                for (LifecycleStrategy strategy : lifecycleStrategies) {
                    strategy.onServiceAdd(this, consumer, route);
                }
                startService(consumer);
                log.info("Route: " + route.getId() + " started and consuming from: " + endpoint);
            }
            routeInputs.add(endpoint);
            // add to the order which they was started, so we know how to stop them in reverse order
            // but only add if we haven't already registered it before (we dont want to double add when restarting)
            boolean found = false;
            for (RouteStartupOrder other : routeStartupOrder) {
                if (other.getRoute().getId().equals(route.getId())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                routeStartupOrder.add(entry.getValue());
            }
        }
        if (resumeOnly) {
            routeService.resume();
        } else {
            // and start the route service (no need to start children as they are already warmed up)
            routeService.start(false);
        }
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RouteStartupOrder(org.apache.camel.spi.RouteStartupOrder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FailedToStartRouteException(org.apache.camel.FailedToStartRouteException) Endpoint(org.apache.camel.Endpoint) PollingConsumer(org.apache.camel.PollingConsumer) Consumer(org.apache.camel.Consumer) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) ServiceStatus(org.apache.camel.ServiceStatus) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ShutdownRoute(org.apache.camel.ShutdownRoute) Route(org.apache.camel.Route)

Example 70 with Route

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

the class DefaultCamelContext method doPrepareRouteToBeStarted.

private DefaultRouteStartupOrder doPrepareRouteToBeStarted(RouteService routeService) {
    // add the inputs from this route service to the list to start afterwards
    // should be ordered according to the startup number
    Integer startupOrder = routeService.getRouteDefinition().getStartupOrder();
    if (startupOrder == null) {
        // auto assign a default startup order
        startupOrder = defaultRouteStartupOrder++;
    }
    // create holder object that contains information about this route to be started
    Route route = routeService.getRoutes().iterator().next();
    return new DefaultRouteStartupOrder(startupOrder, route, routeService);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ShutdownRoute(org.apache.camel.ShutdownRoute) Route(org.apache.camel.Route)

Aggregations

Route (org.apache.camel.Route)90 EventDrivenConsumerRoute (org.apache.camel.impl.EventDrivenConsumerRoute)27 Endpoint (org.apache.camel.Endpoint)24 Channel (org.apache.camel.Channel)17 DeadLetterChannel (org.apache.camel.processor.DeadLetterChannel)15 Processor (org.apache.camel.Processor)13 ArrayList (java.util.ArrayList)12 SendProcessor (org.apache.camel.processor.SendProcessor)11 CamelContext (org.apache.camel.CamelContext)10 ShutdownRoute (org.apache.camel.ShutdownRoute)8 FilterProcessor (org.apache.camel.processor.FilterProcessor)7 RoutePolicy (org.apache.camel.spi.RoutePolicy)6 HashMap (java.util.HashMap)5 Service (org.apache.camel.Service)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 DelegateProcessor (org.apache.camel.DelegateProcessor)4 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)4 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)4