Search in sources :

Example 1 with LifecycleStrategy

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

the class RouteService method doWarmUp.

protected synchronized void doWarmUp() throws Exception {
    if (endpointDone.compareAndSet(false, true)) {
        // and whatnot, thus their lifecycle is to start once, and only to stop when Camel shutdown
        for (Route route : routes) {
            // ensure endpoint is started first (before the route services, such as the consumer)
            ServiceHelper.startService(route.getEndpoint());
        }
    }
    if (warmUpDone.compareAndSet(false, true)) {
        for (Route route : routes) {
            try (MDCHelper mdcHelper = new MDCHelper(route.getId())) {
                // warm up the route first
                route.warmUp();
                LOG.debug("Starting services on route: {}", route.getId());
                List<Service> services = route.getServices();
                // callback that we are staring these services
                route.onStartingServices(services);
                // gather list of services to start as we need to start child services as well
                Set<Service> list = new LinkedHashSet<Service>();
                for (Service service : services) {
                    list.addAll(ServiceHelper.getChildServices(service));
                }
                // split into consumers and child services as we need to start the consumers
                // afterwards to avoid them being active while the others start
                List<Service> childServices = new ArrayList<Service>();
                for (Service service : list) {
                    // inject the route
                    if (service instanceof RouteAware) {
                        ((RouteAware) service).setRoute(route);
                    }
                    if (service instanceof Consumer) {
                        inputs.put(route, (Consumer) service);
                    } else {
                        childServices.add(service);
                    }
                }
                startChildService(route, childServices);
                // fire event
                EventHelper.notifyRouteAdded(camelContext, route);
            }
        }
        // ensure lifecycle strategy is invoked which among others enlist the route in JMX
        for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
            strategy.onRoutesAdd(routes);
        }
        // add routes to camel context
        camelContext.addRouteCollection(routes);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Consumer(org.apache.camel.Consumer) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) RouteAware(org.apache.camel.RouteAware) ArrayList(java.util.ArrayList) Service(org.apache.camel.Service) Route(org.apache.camel.Route)

Example 2 with LifecycleStrategy

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

the class RouteService method doShutdown.

@Override
protected void doShutdown() throws Exception {
    for (Route route : routes) {
        try (MDCHelper mdcHelper = new MDCHelper(route.getId())) {
            LOG.debug("Shutting down services on route: {}", route.getId());
            // gather list of services to stop as we need to start child services as well
            Set<Service> services = gatherChildServices(route, true);
            // shutdown services
            stopChildService(route, services, true);
            // shutdown the route itself
            ServiceHelper.stopAndShutdownServices(route);
            // endpoints should only be stopped when Camel is shutting down
            // see more details in the warmUp method
            ServiceHelper.stopAndShutdownServices(route.getEndpoint());
            // invoke callbacks on route policy
            if (route.getRouteContext().getRoutePolicyList() != null) {
                for (RoutePolicy routePolicy : route.getRouteContext().getRoutePolicyList()) {
                    routePolicy.onRemove(route);
                }
            }
            // fire event
            EventHelper.notifyRouteRemoved(camelContext, route);
        }
    }
    // need to call onRoutesRemove when the CamelContext is shutting down or Route is shutdown
    for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
        strategy.onRoutesRemove(routes);
    }
    // remove the routes from the inflight registry
    for (Route route : routes) {
        camelContext.getInflightRepository().removeRoute(route.getId());
    }
    // remove the routes from the collections
    camelContext.removeRouteCollection(routes);
    // clear inputs on shutdown
    inputs.clear();
    warmUpDone.set(false);
    endpointDone.set(false);
}
Also used : LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service) RoutePolicy(org.apache.camel.spi.RoutePolicy) Route(org.apache.camel.Route)

Example 3 with LifecycleStrategy

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

the class RouteService method doStop.

protected void doStop() throws Exception {
    // if we are stopping CamelContext then we are shutting down
    boolean isShutdownCamelContext = camelContext.isStopping();
    if (isShutdownCamelContext || isRemovingRoutes()) {
        // need to call onRoutesRemove when the CamelContext is shutting down or Route is shutdown
        for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
            strategy.onRoutesRemove(routes);
        }
    }
    for (Route route : routes) {
        try (MDCHelper mdcHelper = new MDCHelper(route.getId())) {
            LOG.debug("Stopping services on route: {}", route.getId());
            // gather list of services to stop as we need to start child services as well
            Set<Service> services = gatherChildServices(route, true);
            // stop services
            stopChildService(route, services, isShutdownCamelContext);
            // stop the route itself
            if (isShutdownCamelContext) {
                ServiceHelper.stopAndShutdownServices(route);
            } else {
                ServiceHelper.stopServices(route);
            }
            // invoke callbacks on route policy
            if (route.getRouteContext().getRoutePolicyList() != null) {
                for (RoutePolicy routePolicy : route.getRouteContext().getRoutePolicyList()) {
                    routePolicy.onStop(route);
                }
            }
            // fire event
            EventHelper.notifyRouteStopped(camelContext, route);
        }
    }
    if (isRemovingRoutes()) {
        camelContext.removeRouteCollection(routes);
    }
    // need to warm up again
    warmUpDone.set(false);
}
Also used : LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service) RoutePolicy(org.apache.camel.spi.RoutePolicy) Route(org.apache.camel.Route)

Example 4 with LifecycleStrategy

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

the class RouteService method stopChildService.

protected void stopChildService(Route route, Set<Service> services, boolean shutdown) throws Exception {
    for (Service service : services) {
        LOG.debug("{} child service on route: {} -> {}", new Object[] { shutdown ? "Shutting down" : "Stopping", route.getId(), service });
        if (service instanceof ErrorHandler) {
            // special for error handlers
            for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
                strategy.onErrorHandlerRemove(route.getRouteContext(), (Processor) service, route.getRouteContext().getRoute().getErrorHandlerBuilder());
            }
        } else {
            for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
                strategy.onServiceRemove(camelContext, service, route);
            }
        }
        if (shutdown) {
            ServiceHelper.stopAndShutdownService(service);
        } else {
            ServiceHelper.stopService(service);
        }
        removeChildService(service);
    }
}
Also used : ErrorHandler(org.apache.camel.processor.ErrorHandler) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service)

Example 5 with LifecycleStrategy

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

the class RouteService method startChildService.

protected void startChildService(Route route, List<Service> services) throws Exception {
    for (Service service : services) {
        LOG.debug("Starting child service on route: {} -> {}", route.getId(), service);
        for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
            strategy.onServiceAdd(camelContext, service, route);
        }
        ServiceHelper.startService(service);
        addChildService(service);
    }
}
Also used : LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service)

Aggregations

LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)20 Service (org.apache.camel.Service)8 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Endpoint (org.apache.camel.Endpoint)4 Route (org.apache.camel.Route)4 HandleFault (org.apache.camel.processor.interceptor.HandleFault)4 EventNotifier (org.apache.camel.spi.EventNotifier)4 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 Consumer (org.apache.camel.Consumer)3 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)3 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)3 RoutePolicy (org.apache.camel.spi.RoutePolicy)3 IOException (java.io.IOException)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2