Author: davsclaus Date: Fri Jan 13 15:46:16 2012 New Revision: 1231137 URL: http://svn.apache.org/viewvc?rev=1231137&view=rev Log: CAMEL-4892: Fixed auto startup on CamelContext should start routes if CamelContext started programmatically later.
Modified: camel/branches/camel-2.9.x/ (props changed) camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java Propchange: camel/branches/camel-2.9.x/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Fri Jan 13 15:46:16 2012 @@ -1 +1 @@ -/camel/trunk:1227209,1227212,1227540,1228015,1228027,1228223,1228879,1229565 +/camel/trunk:1227209,1227212,1227540,1228015,1228027,1228223,1228879,1229565,1231135 Propchange: camel/branches/camel-2.9.x/ ------------------------------------------------------------------------------ --- svnmerge-integrated (original) +++ svnmerge-integrated Fri Jan 13 15:46:16 2012 @@ -1 +1 @@ -/camel/trunk:1-1227196,1227209,1227212,1227540,1228015,1228027,1228223,1228879,1229565 +/camel/trunk:1-1227196,1227209,1227212,1227540,1228015,1228027,1228223,1228879,1229565,1231135 Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1231137&r1=1231136&r2=1231137&view=diff ============================================================================== --- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original) +++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Fri Jan 13 15:46:16 2012 @@ -1369,10 +1369,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()) { @@ -1606,16 +1613,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/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java?rev=1231137&r1=1231136&r2=1231137&view=diff ============================================================================== --- camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java (original) +++ camel/branches/camel-2.9.x/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelContextAutoStartupTest.java Fri Jan 13 15:46:16 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(); }