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);
   }
 }
  public synchronized void warmUp() throws Exception {
    if (endpointDone.compareAndSet(false, true)) {
      // endpoints should only be started once as they can be reused on other routes
      // 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) {
        // 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);
    }
  }
 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);
   }
 }
  @Override
  protected void doShutdown() throws Exception {
    for (Route route : routes) {
      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);
  }
  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) {
      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);
  }