Search in sources :

Example 1 with StartupListener

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

the class DefaultCamelContext method startService.

private void startService(Service service) throws Exception {
    // camel context has been started
    if (service instanceof StartupListener) {
        StartupListener listener = (StartupListener) service;
        addStartupListener(listener);
    }
    if (service instanceof CamelContextAware) {
        CamelContextAware aware = (CamelContextAware) service;
        aware.setCamelContext(this);
    }
    service.start();
}
Also used : CamelContextAware(org.apache.camel.CamelContextAware) StartupListener(org.apache.camel.StartupListener)

Example 2 with StartupListener

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

the class DefaultCamelContext method safelyStartRouteServices.

/**
     * Starts the routes services in a proper manner which ensures the routes will be started in correct order,
     * check for clash and that the routes will also be shutdown in correct order as well.
     * <p/>
     * This method <b>must</b> be used to start routes in a safe manner.
     *
     * @param checkClash     whether to check for startup order clash
     * @param startConsumer  whether the route consumer should be started. Can be used to warmup the route without starting the consumer.
     * @param resumeConsumer whether the route consumer should be resumed.
     * @param addingRoutes   whether we are adding new routes
     * @param routeServices  the routes
     * @throws Exception is thrown if error starting the routes
     */
protected synchronized void safelyStartRouteServices(boolean checkClash, boolean startConsumer, boolean resumeConsumer, boolean addingRoutes, Collection<RouteService> routeServices) throws Exception {
    // list of inputs to start when all the routes have been prepared for starting
    // we use a tree map so the routes will be ordered according to startup order defined on the route
    Map<Integer, DefaultRouteStartupOrder> inputs = new TreeMap<Integer, DefaultRouteStartupOrder>();
    // figure out the order in which the routes should be started
    for (RouteService routeService : routeServices) {
        DefaultRouteStartupOrder order = doPrepareRouteToBeStarted(routeService);
        // check for clash before we add it as input
        if (checkClash) {
            doCheckStartupOrderClash(order, inputs);
        }
        inputs.put(order.getStartupOrder(), order);
    }
    // warm up routes before we start them
    doWarmUpRoutes(inputs, startConsumer);
    // sort the startup listeners so they are started in the right order
    startupListeners.sort(new OrderedComparator());
    // (only the actual route consumer has not yet been started)
    for (StartupListener startup : startupListeners) {
        startup.onCamelContextStarted(this, isStarted());
    }
    // because the consumers may also register startup listeners we need to reset
    // the already started listeners
    List<StartupListener> backup = new ArrayList<>(startupListeners);
    startupListeners.clear();
    // now start the consumers
    if (startConsumer) {
        if (resumeConsumer) {
            // and now resume the routes
            doResumeRouteConsumers(inputs, addingRoutes);
        } else {
            // and now start the routes
            // and check for clash with multiple consumers of the same endpoints which is not allowed
            doStartRouteConsumers(inputs, addingRoutes);
        }
    }
    // sort the startup listeners so they are started in the right order
    startupListeners.sort(new OrderedComparator());
    // so we need to ensure they get started as well
    for (StartupListener startup : startupListeners) {
        startup.onCamelContextStarted(this, isStarted());
    }
    // and add the previous started startup listeners to the list so we have them all
    startupListeners.addAll(0, backup);
    // inputs no longer needed
    inputs.clear();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OrderedComparator(org.apache.camel.util.OrderedComparator) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) StartupListener(org.apache.camel.StartupListener)

Aggregations

StartupListener (org.apache.camel.StartupListener)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CamelContextAware (org.apache.camel.CamelContextAware)1 OrderedComparator (org.apache.camel.util.OrderedComparator)1