Search in sources :

Example 1 with ServiceStatus

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

the class ScheduledRoutePolicy method onJobExecute.

protected void onJobExecute(Action action, Route route) throws Exception {
    LOG.debug("Scheduled Event notification received. Performing action: {} on route: {}", action, route.getId());
    ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
    if (action == Action.START) {
        if (routeStatus == ServiceStatus.Stopped) {
            startRoute(route);
        // here we just check the states of the Consumer
        } else if (ServiceHelper.isSuspended(route.getConsumer())) {
            startConsumer(route.getConsumer());
        }
    } else if (action == Action.STOP) {
        if ((routeStatus == ServiceStatus.Started) || (routeStatus == ServiceStatus.Suspended)) {
            stopRoute(route, getRouteStopGracePeriod(), getTimeUnit());
        } else {
            LOG.warn("Route is not in a started/suspended state and cannot be stopped. The current route state is {}", routeStatus);
        }
    } else if (action == Action.SUSPEND) {
        if (routeStatus == ServiceStatus.Started) {
            stopConsumer(route.getConsumer());
        } else {
            LOG.warn("Route is not in a started state and cannot be suspended. The current route state is {}", routeStatus);
        }
    } else if (action == Action.RESUME) {
        if (routeStatus == ServiceStatus.Started) {
            if (ServiceHelper.isSuspended(route.getConsumer())) {
                startConsumer(route.getConsumer());
            } else {
                LOG.warn("The Consumer {} is not suspended and cannot be resumed.", route.getConsumer());
            }
        } else {
            LOG.warn("Route is not in a started state and cannot be resumed. The current route state is {}", routeStatus);
        }
    }
}
Also used : ServiceStatus(org.apache.camel.ServiceStatus)

Example 2 with ServiceStatus

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

the class ScheduledRoutePolicy method onJobExecute.

protected void onJobExecute(Action action, Route route) throws Exception {
    LOG.debug("Scheduled Event notification received. Performing action: {} on route: {}", action, route.getId());
    ServiceStatus routeStatus = route.getRouteContext().getCamelContext().getRouteStatus(route.getId());
    if (action == Action.START) {
        if (routeStatus == ServiceStatus.Stopped) {
            startRoute(route);
        // here we just check the states of the Consumer
        } else if (ServiceHelper.isSuspended(route.getConsumer())) {
            startConsumer(route.getConsumer());
        }
    } else if (action == Action.STOP) {
        if ((routeStatus == ServiceStatus.Started) || (routeStatus == ServiceStatus.Suspended)) {
            stopRoute(route, getRouteStopGracePeriod(), getTimeUnit());
        } else {
            LOG.warn("Route is not in a started/suspended state and cannot be stopped. The current route state is {}", routeStatus);
        }
    } else if (action == Action.SUSPEND) {
        if (routeStatus == ServiceStatus.Started) {
            stopConsumer(route.getConsumer());
        } else {
            LOG.warn("Route is not in a started state and cannot be suspended. The current route state is {}", routeStatus);
        }
    } else if (action == Action.RESUME) {
        if (routeStatus == ServiceStatus.Started) {
            if (ServiceHelper.isSuspended(route.getConsumer())) {
                startConsumer(route.getConsumer());
            } else {
                LOG.warn("The Consumer {} is not suspended and cannot be resumed.", route.getConsumer());
            }
        } else {
            LOG.warn("Route is not in a started state and cannot be resumed. The current route state is {}", routeStatus);
        }
    }
}
Also used : ServiceStatus(org.apache.camel.ServiceStatus)

Example 3 with ServiceStatus

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

the class CronScheduledRoutePolicyTest method testScheduledStartAndStopRoutePolicy.

@Test
public void testScheduledStartAndStopRoutePolicy() throws Exception {
    MockEndpoint success = context.getEndpoint("mock:success", MockEndpoint.class);
    success.expectedMessageCount(1);
    final CountDownLatch startedLatch = new CountDownLatch(1);
    final CountDownLatch stoppedLatch = new CountDownLatch(1);
    context.getComponent("quartz2", QuartzComponent.class).setPropertiesFile("org/apache/camel/routepolicy/quartz2/myquartz.properties");
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            CronScheduledRoutePolicy policy = new CronScheduledRoutePolicy() {

                @Override
                public void onStart(final Route route) {
                    super.onStart(route);
                    startedLatch.countDown();
                }

                @Override
                public void onStop(final Route route) {
                    super.onStop(route);
                    stoppedLatch.countDown();
                }
            };
            policy.setRouteStartTime("*/3 * * * * ?");
            policy.setRouteStopTime("*/6 * * * * ?");
            policy.setRouteStopGracePeriod(0);
            from("direct:start").routeId("test").routePolicy(policy).noAutoStartup().to("mock:success");
        }
    });
    context.start();
    startedLatch.await(5000, TimeUnit.SECONDS);
    ServiceStatus startedStatus = context.getRouteStatus("test");
    assertTrue(startedStatus == ServiceStatus.Started || startedStatus == ServiceStatus.Starting);
    template.sendBody("direct:start", "Ready or not, Here, I come");
    stoppedLatch.await(5000, TimeUnit.SECONDS);
    ServiceStatus stoppedStatus = context.getRouteStatus("test");
    assertTrue(stoppedStatus == ServiceStatus.Stopped || stoppedStatus == ServiceStatus.Stopping);
    success.assertIsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ServiceStatus(org.apache.camel.ServiceStatus) CountDownLatch(java.util.concurrent.CountDownLatch) Route(org.apache.camel.Route) QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent) Test(org.junit.Test)

Example 4 with ServiceStatus

use of org.apache.camel.ServiceStatus 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 5 with ServiceStatus

use of org.apache.camel.ServiceStatus in project ddf by codice.

the class ContentDirectoryMonitor method dumpCamelContext.

private void dumpCamelContext(String msg) {
    LOGGER.debug("\n\n***************  START: {}  *****************", msg);
    List<RouteDefinition> routeDefinitions = camelContext.adapt(ModelCamelContext.class).getRouteDefinitions();
    if (routeDefinitions != null) {
        LOGGER.debug("Number of routes = {}", routeDefinitions.size());
        for (RouteDefinition routeDef : routeDefinitions) {
            String routeId = routeDef.getId();
            LOGGER.debug("route ID = {}", routeId);
            FromDefinition routeInput = routeDef.getInput();
            LOGGER.debug("route input URI = {}", routeInput);
            ServiceStatus routeStatus = camelContext.getRouteController().getRouteStatus(routeId);
            if (routeStatus != null) {
                LOGGER.debug("Route ID {} is started = {}", routeId, routeStatus.isStarted());
            } else {
                LOGGER.debug("routeStatus is NULL for routeId = {}", routeId);
            }
        }
    }
    LOGGER.debug("***************  END: {}  *****************\n\n", msg);
}
Also used : FromDefinition(org.apache.camel.model.FromDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) ServiceStatus(org.apache.camel.ServiceStatus) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Aggregations

ServiceStatus (org.apache.camel.ServiceStatus)5 Route (org.apache.camel.Route)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (org.apache.camel.Consumer)1 Endpoint (org.apache.camel.Endpoint)1 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)1 PollingConsumer (org.apache.camel.PollingConsumer)1 ShutdownRoute (org.apache.camel.ShutdownRoute)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 QuartzComponent (org.apache.camel.component.quartz2.QuartzComponent)1 FromDefinition (org.apache.camel.model.FromDefinition)1