Search in sources :

Example 1 with SuspendableService

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

the class DefaultCamelContext method doStartOrResumeRoutes.

/**
     * Starts or resumes the routes
     *
     * @param routeServices  the routes to start (will only start a route if its not already started)
     * @param checkClash     whether to check for startup ordering 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
     * @throws Exception is thrown if error starting routes
     */
protected void doStartOrResumeRoutes(Map<String, RouteService> routeServices, boolean checkClash, boolean startConsumer, boolean resumeConsumer, boolean addingRoutes) throws Exception {
    isStartingRoutes.set(true);
    try {
        // filter out already started routes
        Map<String, RouteService> filtered = new LinkedHashMap<String, RouteService>();
        for (Map.Entry<String, RouteService> entry : routeServices.entrySet()) {
            boolean startable = false;
            Consumer consumer = entry.getValue().getRoutes().iterator().next().getConsumer();
            if (consumer instanceof SuspendableService) {
                // consumer could be suspended, which is not reflected in the RouteService status
                startable = ((SuspendableService) consumer).isSuspended();
            }
            if (!startable && consumer instanceof StatefulService) {
                // consumer could be stopped, which is not reflected in the RouteService status
                startable = ((StatefulService) consumer).getStatus().isStartable();
            } else if (!startable) {
                // no consumer so use state from route service
                startable = entry.getValue().getStatus().isStartable();
            }
            if (startable) {
                filtered.put(entry.getKey(), entry.getValue());
            }
        }
        // the context is in last phase of staring, so lets start the routes
        safelyStartRouteServices(checkClash, startConsumer, resumeConsumer, addingRoutes, filtered.values());
    } finally {
        isStartingRoutes.remove();
    }
}
Also used : SuspendableService(org.apache.camel.SuspendableService) PollingConsumer(org.apache.camel.PollingConsumer) Consumer(org.apache.camel.Consumer) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StatefulService(org.apache.camel.StatefulService)

Example 2 with SuspendableService

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

the class ServiceHelper method suspendService.

/**
     * Suspends the given {@code service}.
     * <p/>
     * If {@code service} is both {@link org.apache.camel.Suspendable} and {@link org.apache.camel.SuspendableService} then
     * its {@link org.apache.camel.SuspendableService#suspend()} is called but
     * <b>only</b> if {@code service} is <b>not</b> already
     * {@link #isSuspended(Object) suspended}.
     * <p/>
     * If {@code service} is <b>not</b> a
     * {@link org.apache.camel.Suspendable} and {@link org.apache.camel.SuspendableService} then its
     * {@link org.apache.camel.Service#stop()} is called.
     * <p/>
     * Calling this method has no effect if {@code service} is {@code null}.
     * 
     * @param service the service
     * @return <tt>true</tt> if either the <tt>suspend</tt> method or
     *         {@link #stopService(Object)} was called, <tt>false</tt>
     *         otherwise.
     * @throws Exception is thrown if error occurred
     * @see #stopService(Object)
     */
public static boolean suspendService(Object service) throws Exception {
    if (service instanceof Suspendable && service instanceof SuspendableService) {
        SuspendableService ss = (SuspendableService) service;
        if (!ss.isSuspended()) {
            LOG.trace("Suspending service {}", service);
            ss.suspend();
            return true;
        } else {
            return false;
        }
    } else {
        stopService(service);
        return true;
    }
}
Also used : SuspendableService(org.apache.camel.SuspendableService) Suspendable(org.apache.camel.Suspendable)

Example 3 with SuspendableService

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

the class CronScheduledRoutePolicyTest method testScheduledSuspendRoutePolicy.

@Test
public void testScheduledSuspendRoutePolicy() throws Exception {
    context.getComponent("quartz", QuartzComponent.class).setPropertiesFile("org/apache/camel/routepolicy/quartz/myquartz.properties");
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            CronScheduledRoutePolicy policy = new CronScheduledRoutePolicy();
            policy.setRouteSuspendTime("*/3 * * * * ?");
            from("direct:start").routeId("test").routePolicy(policy).to("mock:unreachable");
        }
    });
    context.start();
    Thread.sleep(5000);
    // when suspending its only the consumer that suspends
    // there is a ticket to improve this
    Consumer consumer = context.getRoute("test").getConsumer();
    SuspendableService ss = (SuspendableService) consumer;
    assertTrue("Consumer should be suspended", ss.isSuspended());
}
Also used : SuspendableService(org.apache.camel.SuspendableService) RouteBuilder(org.apache.camel.builder.RouteBuilder) Consumer(org.apache.camel.Consumer) QuartzComponent(org.apache.camel.component.quartz.QuartzComponent) Test(org.junit.Test)

Example 4 with SuspendableService

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

the class CronScheduledRoutePolicyTest method testScheduledSuspendRoutePolicy.

@Test
public void testScheduledSuspendRoutePolicy() throws Exception {
    context.getComponent("quartz2", QuartzComponent.class).setPropertiesFile("org/apache/camel/routepolicy/quartz2/myquartz.properties");
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            CronScheduledRoutePolicy policy = new CronScheduledRoutePolicy();
            policy.setRouteSuspendTime("*/3 * * * * ?");
            from("direct:start").routeId("test").routePolicy(policy).to("mock:unreachable");
        }
    });
    context.start();
    Thread.sleep(5000);
    // when suspending its only the consumer that suspends
    // there is a ticket to improve this
    Consumer consumer = context.getRoute("test").getConsumer();
    SuspendableService ss = (SuspendableService) consumer;
    assertTrue("Consumer should be suspended", ss.isSuspended());
}
Also used : SuspendableService(org.apache.camel.SuspendableService) RouteBuilder(org.apache.camel.builder.RouteBuilder) Consumer(org.apache.camel.Consumer) QuartzComponent(org.apache.camel.component.quartz2.QuartzComponent) Test(org.junit.Test)

Example 5 with SuspendableService

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

the class ServiceHelper method resumeService.

/**
     * Resumes the given {@code service}.
     * <p/>
     * If {@code service} is both {@link org.apache.camel.Suspendable} and {@link org.apache.camel.SuspendableService} then
     * its {@link org.apache.camel.SuspendableService#resume()} is called but
     * <b>only</b> if {@code service} is already {@link #isSuspended(Object)
     * suspended}.
     * <p/>
     * If {@code service} is <b>not</b> a
     * {@link org.apache.camel.Suspendable} and {@link org.apache.camel.SuspendableService} then its
     * {@link org.apache.camel.Service#start()} is called.
     * <p/>
     * Calling this method has no effect if {@code service} is {@code null}.
     * 
     * @param service the service
     * @return <tt>true</tt> if either <tt>resume</tt> method or
     *         {@link #startService(Service)} was called, <tt>false</tt>
     *         otherwise.
     * @throws Exception is thrown if error occurred
     * @see #startService(Service)
     */
public static boolean resumeService(Object service) throws Exception {
    if (service instanceof Suspendable && service instanceof SuspendableService) {
        SuspendableService ss = (SuspendableService) service;
        if (ss.isSuspended()) {
            LOG.debug("Resuming service {}", service);
            ss.resume();
            return true;
        } else {
            return false;
        }
    } else {
        startService(service);
        return true;
    }
}
Also used : SuspendableService(org.apache.camel.SuspendableService) Suspendable(org.apache.camel.Suspendable)

Aggregations

SuspendableService (org.apache.camel.SuspendableService)9 Consumer (org.apache.camel.Consumer)3 Suspendable (org.apache.camel.Suspendable)3 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Route (org.apache.camel.Route)2 ShutdownRoute (org.apache.camel.ShutdownRoute)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 RouteStartupOrder (org.apache.camel.spi.RouteStartupOrder)2 Test (org.junit.Test)2 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 PollingConsumer (org.apache.camel.PollingConsumer)1 StatefulService (org.apache.camel.StatefulService)1 QuartzComponent (org.apache.camel.component.quartz.QuartzComponent)1 QuartzComponent (org.apache.camel.component.quartz2.QuartzComponent)1