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();
}
}
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;
}
}
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());
}
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());
}
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;
}
}
Aggregations