6 Replies Latest reply on Sep 17, 2012 1:37 PM by Marc Blomquist

    Want to stop then start Spring ApplicationContext

    Marc Blomquist Newbie

      Problem: Once I stop my FileSystemXmlApplicationContext with stop() I can't seem to figure out how to start it again. start() function seems to do nothing.

       

      Using: spring-context-3.0.5.RELEASE.jar

       

      Comments: The spring application context is just a FileSystemXmlApplicationContext that has a camel context with "direct:start" and "mock:result" endpoints. Here are the unit tests I'm running below,

       

      @Test

      public void testCreateRoute() throws Exception {

          resultEndpoint.expectsMessageCount(1);

          template.sendBody("test");

          resultEndpoint.assertIsSatisfied();

      }

       

      First test above runs fine with no problems.

       

      @Test

      public void testStartAndStop() throws Exception {

          resultEndpoint.expectsMessageCount(1);

          ((AbstractApplicationContext)ctx).stop();

          ((AbstractApplicationContext)ctx).start();   

          template.sendBody("test");

          resultEndpoint.assertIsSatisfied();

      }

       

      Second test case start() function does not bring the context back up. Logs show nothing after I call start(), template.sendBody crashes, isRunning() says false and isActive() says true. Is start the wrong function to use? I want to ultimately be able to start and stop my ApplicationContext as many times as a I please.

        • 1. Re: Want to stop then start Spring ApplicationContext
          Claus Ibsen Master

          If you restart the spring app ctx in an unit test you would need to lookup the mock endpoint again after the start, to set its expectations, and as well to create a new producer template etc.

           

          As the built-in from the CamelTestSupport class is from the first CamelContext that was loaded, before you restart the spring app ctx manually.

          • 2. Re: Want to stop then start Spring ApplicationContext
            Marc Blomquist Newbie

            davsclaus I tried to do what you said but still have issues. I'm extending org.apache.camel.test.junit4.CamelTestSupport for the test case class. So I dug through CamelTestSupport class to figure out how everything is initialized and I found the setUp() function.

             

            public void setUp() throws Exception {

            applicationContext = createApplicationContext();

            assertNotNull("Should have created a valid spring context", applicationContext);

            supper.setUp();

            }

             

            I decided not to use this function because I don't want to create the application context again, which would defeat the purpose of what I'm trying to do. Instead I decided to use CamelTestSupport's setUp function. So I copy pasted CamelTestSupport's function into my class, renamed it to reSetUp() and tried to run the test again. The error I get this time round is the following exception, "Should have at least one route."

             

            This is coming directly from the following two lines in CamelTestSupport.setUp()

             

            context = createCamelContext();

            assertValidContext(context);

             

            The createCamelContext() function is invoking the following function from CamelSpringTestSupport

             

            protected CamelContext createCamelContext() throws Exception {

            return SpringCamelContext.springCamelContext(applicationContext);

            }

             

            Am I to assume that once I stop a Spring ApplicationContext that all the camel routes are destroyed?

             

            I've attached my code because this is starting to get a little too complicated to just provide snippets.

            • 3. Re: Want to stop then start Spring ApplicationContext
              Claus Ibsen Master

              Yes if you use a Spring XML file to embed Camel, eg in the XML file you have  then the lifecycle of Spring will also start|stop Camel and all its routes/components etc.

              • 4. Re: Want to stop then start Spring ApplicationContext
                Marc Blomquist Newbie

                I'm confused by your choice of words. You say "start/stop" but the error message I'm getting makes it sound like the route is "destroyed". If the CamelContext and all its routes have only been stopped as you say then why can't I start them again? Why is org.apache.camel.test.junit4.CamelTestSupport setup function failing with the error message "Should have at least one route." ?

                • 5. Re: Want to stop then start Spring ApplicationContext
                  Claus Ibsen Master

                  The CamelSpringTestSupport class is based on loading a spring xml file, which contains a single CamelContext.

                   

                  If you want to test multiple camel contexts and re-load the files etc. Then it may be easier with a plain TestCase and do it yourself.

                   

                  That last error message is because the CamelSpringTestSupport expects to be at least 1 route. There is a method you can override and return 0. This check has been removed in newer Camel releases where you dont need this anymore.

                  • 6. Re: Want to stop then start Spring ApplicationContext
                    Marc Blomquist Newbie

                    That is unfortunate. I'll create my own test case.