Skip to content

Commit

Permalink
CAMEL-11814: makes NO_START a ThreadLocal, and ...
Browse files Browse the repository at this point in the history
...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.
  • Loading branch information
zregvart committed Sep 26, 2017
1 parent 614e526 commit 1c17aa3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Expand Up @@ -28,6 +28,7 @@


import org.apache.camel.CamelContext; import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder; import org.apache.camel.RoutesBuilder;
import org.apache.camel.StartupListener;
import org.apache.camel.main.MainDurationEventNotifier; import org.apache.camel.main.MainDurationEventNotifier;
import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.RoutesDefinition; import org.apache.camel.model.RoutesDefinition;
Expand Down Expand Up @@ -173,9 +174,19 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
controller.getCompleted(), controller.getLatch()); controller.getCompleted(), controller.getLatch());
} }


// controller will start Camel camelContext.addStartupListener(new StartupListener() {
LOG.info("Starting CamelMainRunController to ensure the main thread keeps running"); @Override
controller.start(); 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 { } else {
if (applicationContext instanceof ConfigurableApplicationContext) { if (applicationContext instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext; ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
Expand Down
Expand Up @@ -16,8 +16,6 @@
*/ */
package org.apache.camel.spring; package org.apache.camel.spring;


import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.camel.Endpoint; import org.apache.camel.Endpoint;
import org.apache.camel.component.bean.BeanProcessor; import org.apache.camel.component.bean.BeanProcessor;
import org.apache.camel.component.event.EventComponent; import org.apache.camel.component.event.EventComponent;
Expand Down Expand Up @@ -62,7 +60,7 @@ public class SpringCamelContext extends DefaultCamelContext implements Lifecycle
ApplicationListener<ApplicationEvent>, Ordered { ApplicationListener<ApplicationEvent>, Ordered {


private static final Logger LOG = LoggerFactory.getLogger(SpringCamelContext.class); 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 ApplicationContext applicationContext;
private EventComponent eventComponent; private EventComponent eventComponent;
private boolean shutdownEager = true; private boolean shutdownEager = true;
Expand All @@ -75,7 +73,11 @@ public SpringCamelContext(ApplicationContext applicationContext) {
} }


public static void setNoStart(boolean b) { public static void setNoStart(boolean b) {
NO_START.set(b); if (b) {
NO_START.set(true);
} else {
NO_START.set(null);
}
} }


/** /**
Expand Down

0 comments on commit 1c17aa3

Please sign in to comment.