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