0

I am writing a test for Camel using Spring boot. Below is configuration on the test class

@RunWith(CamelSpringBootRunner.class)
@SpringBootApplication
@ComponentScan(basePackages = ["example.test"])
@UseAdviceWith
@BootstrapWith(SpringBootTestContextBootstrapper)
@DirtiesContext
class RouteTest {

  private static final Logger LOGGER = LoggerFactory.getLogger(RouteTest.class)

  @Autowired ModelCamelContext camelContext

  @Test
  void "flow"() {
     camelContext.getRouteDefinition(route.routeId).adviceWith(camelContext, new AdviceWithRouteBuilder() {
        @Override
        void configure() throws Exception {
        }
     }
     LOGGER.info("IN TEST: ******* Camel Status: "+camelContext.getStatus())
  }

I expect camel should not be started. But when I run the test it is already started.

I noticed that CamelSpringBootRunner does start camel context in CamelSpringBootExecutionListener.

How do I force not to start the camel context.

AMK
  • 3
  • 3

3 Answers3

0

In the latest version of camel there is an option for autoStartup of camel. You an achieve what you want by adding autoStartup option. For example the route below is configured autoStartup=false to prevent Camel starting when Spring starts.

<camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring" autoStartup="false">
    <route>
        <from uri="direct:start"/>
        <to uri="mock:result"/>
    </route>
</camelContext>

You can manually start Camel later by invoking its start method as shown below:

ApplicationContext ac = ...
SpringCamelContext camel = (SpringCamelContext) ac.getBean("myCamel");

// now start Camel manually
camel.start();
NitKrish
  • 96
  • 4
  • 16
  • I have autoStartup set to false but CamelSpringBootExecutionListener does not take into account this autostartup flag. and starts the context. – AMK Nov 09 '17 at 13:27
  • Bug [CAMEL-11955](https://issues.apache.org/jira/browse/CAMEL-11955) raised in Camel-2.20.0 – AMK Nov 10 '17 at 15:20
0

If you are using older version of camel then autoStartup option will not work try using shouldStartContext instead. Sometimes starting camel after setting shouldStartContext doesn't work so I have put the work around in below example. Try this :

setting shouldStartContext manually before starting the context from the code:

((SpringCamelContext)camelContext).setShouldStartContext(true);
camelContext.start();

Example context:

<camel:camelContext id="ids.camel.context" shouldStartContext="false"> 

        <!-- Queue endpoints. -->
        <camel:endpoint id="defaultInQueue" uri="jms:queue:${default.in.queue.name}"/>  
        <camel:endpoint id="defaultOutQueue" uri="jms:queue:${default.out.queue.name}"/>                

        <!-- Route to send messages to IDS  -->
        <camel:route id="out" trace="true">
            <camel:from uri="direct:sender"/>
            <!-- Do not expect answer (fire and forget) -->
            <camel:setExchangePattern pattern="InOnly"/>
            <camel:to ref="defaultOutQueue"/>
        </camel:route>

        <!-- Route to receive -->   
        <camel:route id ="in" trace="true">
            <camel:from ref="defaultInQueue"/>
            <camel:to uri="bean:defaultTextAdapter?method=onMessage"/>
        </camel:route>

    </camel:camelContext>   
NitKrish
  • 96
  • 4
  • 16
0

Using Camel 2.20.1 solved the issue

AMK
  • 3
  • 3