11 Replies Latest reply on Oct 15, 2010 5:49 PM by Peter Chandler

    org.apache.camel.impl.DefaultComponent.doStop() - callback at shutdown

    Peter Chandler Master

      Reference threadID 2266 

       

      "If you component extend DefaultComponent then it should already implement the Service. The idea is then you can override the doStart / doStop methods."

       

      I try to override the doStop in my camel component as shown:

       

      public class MyCamelComponent extends DefaultComponent {
      
      // Other methods createEndpoint, ... 
      
       /**
         * When the camel context.stop() is called this method should be invoked.
         * 
         * @see http://camel.apache.org/lifecycle.html
         * 
         */
        @Override
        protected void doStop() throws Exception
        {
             System.out.println("doStop()");
             LOG.info( "doStop()");
             // clean-up/release ALL external resources.
             BasicDataSource ds = dataSource();
             if (ds != null)
             {
                  try
                  {
                       ds.close();
                       LOG.info("Closing database connections for DB " + ds.getUrl() );
                  }
                  catch (Exception e)
                  {
                       LOG.error("Error shutting down database " + ds.getUrl(), e);
                       throw e;
                  }
             }
             
             // call the super's stop.
             try
             {
                  super.doStop();
             }
             catch (Exception e)
             {
                  LOG.error("Error in super.doStop();",e);
             }
        }
      
      }
      

       

      Created a route that calls my component

        <!-- MyCamelComponent   -->
        <bean id="myComponent" class="com.mycompany.MyCamelComponent">
          <property name="dataSource" ref="dataSource"></property>
        </bean>
      
        <camelContext id="test" xmlns="http://camel.apache.org/schema/spring">
          <route>
            <from uri="jms:topic:incoming"></from>
            <to uri="myComponent://."></to>
          </route>
         
        </camelContext>
      
      

       

      In my junit (note: SpringCamelContext context) I added code:

        /** Stop the camel context. */
        public void tearDown() {
          try {
            LOG.info( "JUnit teardown and stop the camel context " + context.getName());
            context.stop();
          } catch ( Exception e ) {
            LOG.error("Error Stopping Conext", e );
          }
        }
      

       

      Results

       

      • The route starts

      • Route runs successfully

      • In the log: JUnit teardown and stop the camel context camel-2

      • doStop() does not get called?

       

      Question:

      Help: Why doesn't doStop() get called? What am I forgetting?

       

      Again thanks in advance,

       

      Peter.