Repository: camel Updated Branches: refs/heads/master 614e52694 -> 1c17aa3f3
CAMEL-11814: makes NO_START a ThreadLocal, and ... ...changes the way CamelMainRunController is started We need to change NO_START flag back to ThreadLocal as there is a use case when it's used from a single classloader outside of the tests in wildfly-camel. CamelMainRunController would atempt to start CamelContext from a thread that does not have the NO_START flag defined (it's a ThreadLocal), so it can only run when CamelContext is started. It's main purpose is to prevent the SpringBoot application JVM from terminating so having it run when the CamelContext is started doesn't prevent that. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1c17aa3f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1c17aa3f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1c17aa3f Branch: refs/heads/master Commit: 1c17aa3f3c4cde4862bee11698c294f543182ae0 Parents: 614e526 Author: Zoran Regvart <zregv...@apache.org> Authored: Tue Sep 26 12:03:12 2017 +0200 Committer: Zoran Regvart <zregv...@apache.org> Committed: Tue Sep 26 14:52:43 2017 +0200 ---------------------------------------------------------------------- .../apache/camel/spring/boot/RoutesCollector.java | 17 ++++++++++++++--- .../apache/camel/spring/SpringCamelContext.java | 10 ++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1c17aa3f/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java index 6e4d588..4e9d891 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java @@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; +import org.apache.camel.StartupListener; import org.apache.camel.main.MainDurationEventNotifier; import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.RoutesDefinition; @@ -173,9 +174,19 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven controller.getCompleted(), controller.getLatch()); } - // controller will start Camel - LOG.info("Starting CamelMainRunController to ensure the main thread keeps running"); - controller.start(); + camelContext.addStartupListener(new StartupListener() { + @Override + public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception { + // run the CamelMainRunController after the context has been started + // this way we ensure that NO_START flag is honoured as it's set as + // a thread local variable of the thread CamelMainRunController is + // not running on + if (!alreadyStarted) { + LOG.info("Starting CamelMainRunController to ensure the main thread keeps running"); + controller.start(); + } + } + }); } else { if (applicationContext instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext; http://git-wip-us.apache.org/repos/asf/camel/blob/1c17aa3f/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java b/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java index 1134957..e32e581 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/SpringCamelContext.java @@ -16,8 +16,6 @@ */ package org.apache.camel.spring; -import java.util.concurrent.atomic.AtomicBoolean; - import org.apache.camel.Endpoint; import org.apache.camel.component.bean.BeanProcessor; import org.apache.camel.component.event.EventComponent; @@ -62,7 +60,7 @@ public class SpringCamelContext extends DefaultCamelContext implements Lifecycle ApplicationListener<ApplicationEvent>, Ordered { private static final Logger LOG = LoggerFactory.getLogger(SpringCamelContext.class); - private static final AtomicBoolean NO_START = new AtomicBoolean(); + private static final ThreadLocal<Boolean> NO_START = new ThreadLocal<>(); private ApplicationContext applicationContext; private EventComponent eventComponent; private boolean shutdownEager = true; @@ -75,7 +73,11 @@ public class SpringCamelContext extends DefaultCamelContext implements Lifecycle } public static void setNoStart(boolean b) { - NO_START.set(b); + if (b) { + NO_START.set(true); + } else { + NO_START.set(null); + } } /**