CAMEL-11587: SupervisingRouteController : add option for initial delay
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ef5aa1f5 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ef5aa1f5 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ef5aa1f5 Branch: refs/heads/master Commit: ef5aa1f561a6ad4fcbf7d8b95d6ef3c4c0b39e5a Parents: b3ebc7b Author: lburgazzoli <lburgazz...@gmail.com> Authored: Mon Aug 7 11:20:01 2017 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Tue Aug 8 13:29:28 2017 +0200 ---------------------------------------------------------------------- .../camel/impl/SupervisingRouteController.java | 83 +++++++++++++++++--- ...rvisingRouteControllerAutoConfiguration.java | 4 + ...SupervisingRouteControllerConfiguration.java | 15 ++++ .../src/main/resources/application.properties | 1 + 4 files changed, 94 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ef5aa1f5/camel-core/src/main/java/org/apache/camel/impl/SupervisingRouteController.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/SupervisingRouteController.java b/camel-core/src/main/java/org/apache/camel/impl/SupervisingRouteController.java index 01396dc..8e5167e 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/SupervisingRouteController.java +++ b/camel-core/src/main/java/org/apache/camel/impl/SupervisingRouteController.java @@ -16,6 +16,7 @@ */ package org.apache.camel.impl; +import java.time.Duration; import java.util.EventObject; import java.util.HashMap; import java.util.List; @@ -74,6 +75,7 @@ public class SupervisingRouteController extends DefaultRouteController { private ScheduledExecutorService executorService; private BackOff defaultBackOff; private Map<String, BackOff> backOffConfigurations; + private Duration initialDelay; public SupervisingRouteController() { this.lock = new Object(); @@ -83,6 +85,7 @@ public class SupervisingRouteController extends DefaultRouteController { this.routeManager = new RouteManager(); this.defaultBackOff = BackOff.builder().build(); this.backOffConfigurations = new HashMap<>(); + this.initialDelay = Duration.ofMillis(0); try { this.listener = new CamelContextStartupListener(); @@ -100,6 +103,9 @@ public class SupervisingRouteController extends DefaultRouteController { return defaultBackOff; } + /** + * Sets the default back-off. + */ public void setDefaultBackOff(BackOff defaultBackOff) { this.defaultBackOff = defaultBackOff; } @@ -108,6 +114,9 @@ public class SupervisingRouteController extends DefaultRouteController { return backOffConfigurations; } + /** + * Set the back-off for the given IDs. + */ public void setBackOffConfigurations(Map<String, BackOff> backOffConfigurations) { this.backOffConfigurations = backOffConfigurations; } @@ -116,10 +125,47 @@ public class SupervisingRouteController extends DefaultRouteController { return backOffConfigurations.getOrDefault(id, defaultBackOff); } + /** + * Sets the back-off to be applied to the given <code>id</code>. + */ public void setBackOff(String id, BackOff backOff) { backOffConfigurations.put(id, backOff); } + public Duration getInitialDelay() { + return initialDelay; + } + + /** + * Set the amount of time the route controller should wait before to start + * the routes after the camel context is started or after the route is + * initialized if the route is created after the camel context is started. + * + * @param initialDelay the initial delay. + */ + public void setInitialDelay(Duration initialDelay) { + this.initialDelay = initialDelay; + } + + /** + * #see {@link this#setInitialDelay(Duration)} + * + * @param initialDelay the initial delay amount. + * @param initialDelay the initial delay time unit. + */ + public void setInitialDelay(long initialDelay, TimeUnit initialDelayUnit) { + this.initialDelay = Duration.ofMillis(initialDelayUnit.toMillis(initialDelay)); + } + + /** + * #see {@link this#setInitialDelay(Duration)} + * + * @param initialDelay the initial delay in milliseconds. + */ + public void setInitialDelay(long initialDelay) { + this.initialDelay = Duration.ofMillis(initialDelay); + } + // ********************************* // Lifecycle // ********************************* @@ -535,6 +581,19 @@ public class SupervisingRouteController extends DefaultRouteController { } private class ManagedRoutePolicy implements RoutePolicy { + + private void startRoute(RouteHolder holder) { + try { + SupervisingRouteController.this.doStartRoute( + holder, + true, + r -> SupervisingRouteController.super.startRoute(r.getId()) + ); + } catch (Exception e) { + throw new RuntimeCamelException(e); + } + } + @Override public void onInit(Route route) { if ("false".equals(route.getRouteContext().getRoute().getAutoStartup())) { @@ -549,14 +608,13 @@ public class SupervisingRouteController extends DefaultRouteController { if (contextStarted.get()) { LOGGER.info("Context is already started: attempt to start route {}", route.getId()); - try { - SupervisingRouteController.this.doStartRoute( - holder, - true, - r -> SupervisingRouteController.super.startRoute(r.getId()) - ); - } catch (Exception e) { - throw new RuntimeCamelException(e); + + // Eventually delay the startup of the route a later time + if (initialDelay.toMillis() > 0) { + LOGGER.debug("Route {} will be started in {}", holder.getId(), initialDelay); + executorService.schedule(() -> startRoute(holder), initialDelay.toMillis(), TimeUnit.MILLISECONDS); + } else { + startRoute(holder); } } else { LOGGER.info("Context is not yet started: defer route {} start", holder.getId()); @@ -633,7 +691,14 @@ public class SupervisingRouteController extends DefaultRouteController { // so start/stop of managed routes do not clash with CamelContext // startup if (contextStarted.compareAndSet(false, true)) { - startRoutes(); + + // Eventually delay the startup of the routes a later time + if (initialDelay.toMillis() > 0) { + LOGGER.debug("Routes will be started in {}", initialDelay); + executorService.schedule(SupervisingRouteController.this::startRoutes, initialDelay.toMillis(), TimeUnit.MILLISECONDS); + } else { + startRoutes(); + } } } } http://git-wip-us.apache.org/repos/asf/camel/blob/ef5aa1f5/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerAutoConfiguration.java index abb747d..9ff36bb 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerAutoConfiguration.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerAutoConfiguration.java @@ -49,6 +49,10 @@ public class SupervisingRouteControllerAutoConfiguration { public RouteController routeController() { SupervisingRouteController controller = new SupervisingRouteController(); + // Initial delay + Optional.ofNullable(configuration.getInitialDelay()).map(TimePatternConverter::toMilliSeconds).ifPresent(controller::setInitialDelay); + + // Back off controller.setDefaultBackOff(configureBackOff(Optional.empty(), configuration.getDefaultBackOff())); for (Map.Entry<String, RouteConfiguration> entry: configuration.getRoutes().entrySet()) { http://git-wip-us.apache.org/repos/asf/camel/blob/ef5aa1f5/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerConfiguration.java index fb667b3..4ab2290 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerConfiguration.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SupervisingRouteControllerConfiguration.java @@ -29,6 +29,13 @@ public class SupervisingRouteControllerConfiguration { private boolean enabled; /** + * Set the amount of time the route controller should wait before to start + * the routes after the camel context is started or after the route is + * initialized if the route is created after the camel context is started. + */ + private String initialDelay; + + /** * The default back-off configuration, back-off configuration for routes inherits from this default. */ private BackOffConfiguration defaultBackOff = new BackOffConfiguration(); @@ -46,6 +53,14 @@ public class SupervisingRouteControllerConfiguration { this.enabled = enabled; } + public String getInitialDelay() { + return initialDelay; + } + + public void setInitialDelay(String initialDelay) { + this.initialDelay = initialDelay; + } + public BackOffConfiguration getDefaultBackOff() { return defaultBackOff; } http://git-wip-us.apache.org/repos/asf/camel/blob/ef5aa1f5/examples/camel-example-spring-boot-routecontroller/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-routecontroller/src/main/resources/application.properties b/examples/camel-example-spring-boot-routecontroller/src/main/resources/application.properties index 9cd0c34..10efe1c 100644 --- a/examples/camel-example-spring-boot-routecontroller/src/main/resources/application.properties +++ b/examples/camel-example-spring-boot-routecontroller/src/main/resources/application.properties @@ -37,6 +37,7 @@ management.security.enabled = false camel.springboot.name = SampleSupervisingRouteController camel.supervising.controller.enabled = true +camel.supervising.controller.initial-delay = 5s camel.supervising.controller.default-back-off.delay = 5s camel.supervising.controller.default-back-off.max-attempts = 10 camel.supervising.controller.routes.undertow.back-off.delay = 10s