Search in sources :

Example 1 with RouteAware

use of org.apache.camel.RouteAware 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 RouteAware

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

the class EventDrivenConsumerRoute method addServices.

/**
     * Factory method to lazily create the complete list of services required for this route
     * such as adding the processor or consumer
     */
@Override
protected void addServices(List<Service> services) throws Exception {
    Endpoint endpoint = getEndpoint();
    consumer = endpoint.createConsumer(processor);
    if (consumer != null) {
        services.add(consumer);
        if (consumer instanceof RouteAware) {
            ((RouteAware) consumer).setRoute(this);
        }
    }
    Processor processor = getProcessor();
    if (processor instanceof Service) {
        services.add((Service) processor);
    }
}
Also used : Processor(org.apache.camel.Processor) Endpoint(org.apache.camel.Endpoint) RouteAware(org.apache.camel.RouteAware) SuspendableService(org.apache.camel.SuspendableService) Service(org.apache.camel.Service)

Aggregations

RouteAware (org.apache.camel.RouteAware)2 Service (org.apache.camel.Service)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Consumer (org.apache.camel.Consumer)1 Endpoint (org.apache.camel.Endpoint)1 Processor (org.apache.camel.Processor)1 Route (org.apache.camel.Route)1 SuspendableService (org.apache.camel.SuspendableService)1 LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)1