Author: davsclaus Date: Fri Jan 13 15:42:25 2012 New Revision: 1231135 URL: http://svn.apache.org/viewvc?rev=1231135&view=rev Log: CAMEL-4892: Fixed auto startup on CamelContext should start routes if CamelContext started programmatically later.
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1231135&r1=1231134&r2=1231135&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Fri Jan 13 15:42:25 2012 @@ -1368,10 +1368,17 @@ public class DefaultCamelContext extends log.info("Apache Camel " + getVersion() + " (CamelContext: " + getName() + ") is starting"); doNotStartRoutesOnFirstStart = !firstStartDone && !isAutoStartup(); - firstStartDone = true; + + // if the context was configured with auto startup = false, and we are already started, + // then we may need to start the routes on the 2nd start call + if (firstStartDone && !isAutoStartup() && isStarted()) { + // invoke this logic to warmup the routes and if possible also start the routes + doStartOrResumeRoutes(routeServices, true, true, false, true); + } // super will invoke doStart which will prepare internal services and start routes etc. try { + firstStartDone = true; super.start(); } catch (VetoCamelContextStartException e) { if (e.isRethrowException()) { @@ -1605,16 +1612,18 @@ public class DefaultCamelContext extends // filter out already started routes Map<String, RouteService> filtered = new LinkedHashMap<String, RouteService>(); for (Map.Entry<String, RouteService> entry : routeServices.entrySet()) { - boolean startable; + 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(); - } else if (consumer instanceof StatefulService) { + } + + if (!startable && consumer instanceof StatefulService) { // consumer could be stopped, which is not reflected in the RouteService status startable = ((StatefulService) consumer).getStatus().isStartable(); - } else { + } else if (!startable) { // no consumer so use state from route service startable = entry.getValue().getStatus().isStartable(); } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java?rev=1231135&r1=1231134&r2=1231135&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java Fri Jan 13 15:42:25 2012 @@ -25,7 +25,42 @@ import org.apache.camel.component.mock.M */ public class DefaultCamelContextAutoStartupTest extends TestSupport { - public void testAutoStartupFalse() throws Exception { + // TODO: We should have a JMX test of this as well + + public void testAutoStartupFalseContextStart() throws Exception { + DefaultCamelContext camel = new DefaultCamelContext(new SimpleRegistry()); + camel.disableJMX(); + camel.setAutoStartup(false); + + camel.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").routeId("foo").to("mock:result"); + } + }); + camel.start(); + + assertEquals(true, camel.isStarted()); + assertEquals(1, camel.getRoutes().size()); + assertEquals(true, camel.getRouteStatus("foo").isStopped()); + + // now start camel again, to get it to start the routes + camel.start(); + + assertEquals(true, camel.getRouteStatus("foo").isStarted()); + + // and now its started we can test that it works by sending in a message to the route + MockEndpoint mock = camel.getEndpoint("mock:result", MockEndpoint.class); + mock.expectedMessageCount(1); + + camel.createProducerTemplate().sendBody("direct:start", "Hello World"); + + mock.assertIsSatisfied(); + + camel.stop(); + } + + public void testAutoStartupFalseRouteStart() throws Exception { DefaultCamelContext camel = new DefaultCamelContext(new SimpleRegistry()); camel.disableJMX(); camel.setAutoStartup(false); @@ -54,7 +89,7 @@ public class DefaultCamelContextAutoStar camel.createProducerTemplate().sendBody("direct:start", "Hello World"); mock.assertIsSatisfied(); - + camel.stop(); }