3 Replies Latest reply on Dec 15, 2014 11:38 PM by niteshjain132

    Invoking a Router via Processor

    niteshjain132

      Already waste 2hours and still not able to figure , how to invoke a Router in Camel context via a Java code.

       

      Tried this -

       

      in Java Class :

      ApplicationContext context = new ClassPathXmlApplicationContext("camel_context.xml");

      CamelContext camel = SpringCamelContext.springCamelContext(context);

      camel.startRoute("firstRoute");

       

      in camel_context.xml :


      <bean id="testRoute" class="org.apache.camel.processor.RouterTest"/>

      <camelContext trace="false" id="TestRouterCamel"

              xmlns="http://camel.apache.org/schema/spring">

             <route id="firstRoute">

                     <from uri="direct:start" />

                     <to uri="testRoute" />

           </route>

      </camelContext>

       

      Trying to invoke firstRoute in camel_context.xml via java class. any help would be appreciated.

       

      Thanks in advance.

        • 1. Re: Invoking a Router via Processor
          bharadwaj

          Main main = new Main();

            main.setApplicationContext(new ClassPathXmlApplicationContext("camel_context.xml"));

            main.enableHangupSupport();

          main.run();

          • 2. Re: Invoking a Router via Processor
            niteshjain132

            org.apache.camel.spring.Main will load only camel_context.xml, but how can i invoke "firstRoute" which is defined in camel_context.xml ?

             

            I tried your code like this :

             

                Main main=new Main();
               main.setApplicationContext(new ClassPathXmlApplicationContext("camel_context.xml"));
               main.enableHangupSupport();
               main.run();
               CamelContext camel=SpringCamelContext.springCamelContext(main.getApplicationContext());
               camel.startRoute("firstRoute");
              
            Logs :

            2014-12-12 10:30:09 INFO  ClassPathXmlApplicationContext:510 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@35497537: startup date [Fri Dec 12 10:30:09 IST 2014]; root of context hierarchy

            2014-12-12 10:30:09 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [camel_context.xml]

            2014-12-12 10:30:11 INFO  DefaultListableBeanFactory:598 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@496edf81: defining beans [template,consumerTemplate,TestRouterCamel:beanPostProcessor,TestRouterCamel,testRoute]; root of factory hierarchy

            still not able to invoke a camel-route, which in turn will invoke a Processor.

            • 3. Re: Invoking a Router via Processor
              niteshjain132

              Using Producer Template worked for me

               

              ProducerTemplate template = context.createProducerTemplate();

                      template.send("direct:start", new MyProcessor());