2 Replies Latest reply on Feb 11, 2015 11:48 PM by niteshjain132

    Invoke a Route on bundle StartUp

    niteshjain132

      Server - Jboss Fuse 6.0

      Trying to invoke a route when the ApplicationContext is loaded successfully using ApplicationContextAware Interface.

       

      package com.test;

      import org.apache.camel.CamelContext;

      import org.apache.camel.Exchange;

      import org.apache.camel.ProducerTemplate;

      import org.apache.camel.impl.DefaultExchange;

      import org.springframework.beans.BeansException;

      import org.springframework.context.ApplicationContext;

      import org.springframework.context.ApplicationContextAware;

       

       

      public class ContextAware implements ApplicationContextAware {

        public static CamelContext context;

        public static CamelContext getContext() {

             return context;

        }

        public static void setContext(CamelContext context) throws Exception {

           ContextAware.context = context;

        }

       

        public void setApplicationContext(ApplicationContext ac) throws BeansException {

             try {

                  ContextAware.setContext((CamelContext) ac.getBean("hazel_camel"));

                   System.out.println("-----------Invoking route----------------");

                  ProducerTemplate template = ContextAware.context.createProducerTemplate();

               

                  Exchange exchange=new DefaultExchange(ContextAware.context);

                  template.send("direct:getMessageProcessor",exchange);

                  System.out.println("-----------END----------------");

             } catch (Exception e) {

                  e.printStackTrace();

             }

        }

      }

       

      the "direct:getMessageProcessor" is not invoking the processor associated with it in camel_context.xml :


      <camelContext trace="false" id="camel"  xmlns="http://camel.apache.org/schema/spring">

           <route id="getMessageProcessor">

                  <from uri="direct:getMessageProcessor" />

                  <process ref="getMessage" />

            </route>

      </camelContext>

      <bean id="getMessage" class="com.test.GetMessageProcessor" />

       

      i get this in log -

      org.apache.camel.camel-core - 2.12.0.redhat-610379 | Route: getMessageProcessor started and consuming from: Endpoint[direct://getMessageProcessor]

       

      but GetMessageProcessor.java is not invoked,

       

      Thanks,

      Nitesh

        • 1. Re: Invoke a Route on bundle StartUp
          davsclaus

          Likely better to use use Spring init method and dependency injection and inject CamelContext using getter/setter

           

          <bean id="getMessage" class="com.test.GetMessageProcessor" init-method="someName">

          <property name="camelContext" ref="camel-1"/>

          </bean>


          And then have a getter/setter for CamelContext in your bean. Then you do not have to implement any spring interfaces.


          And then create a method you want to call and use the init-method

          http://stackoverflow.com/questions/1088550/spring-how-to-call-a-method-after-bean-initialization-is-complete

          • 2. Re: Invoke a Route on bundle StartUp
            niteshjain132

            Agreed, I tried spring's DI as below,

            Camel_context.xml

            ....

            <camelContext trace="false" id="camel"  xmlns="http://camel.apache.org/schema/spring">

                 <route id="getMessageProcessor">

                        <from uri="direct:getMessageProcessor" />

                        <process ref="getMessage" />

                  </route>

            </camelContext>

            <bean id="loadCamelContext" class="com.test.Camel">

              <property name="context">

              <ref bean="camel" />

              </property>

            </bean>

            <bean id="getMessage" class="com.test.GetMessageProcessor" />

            ....

            Camel.java

             

            package com.test;

            import org.apache.camel.CamelContext;

            public class Camel {

              public static CamelContext context;

              public static CamelContext getContext() {

              return context;

              }

              public void setContext(CamelContext context) {

              /*

              * Here context is created and logged.

              * but still the same, not able to get inside a processor

              */

              System.out.println("Context :: "+context);

              Camel.context = context;

             

                System.out.println("-----------Invoking route----------------");

                ProducerTemplate template = ContextAware.context.createProducerTemplate();    

                Exchange exchange=new DefaultExchange(Camel.context);

                template.send("direct:getMessageProcessor",exchange);

                System.out.println("-----------END----------------");

              }

            }

             

            Camel context is printed on terminal but still the same ("org.apache.camel.camel-core - 2.12.0.redhat-610379 | Route: getMessageProcessor started and consuming from: Endpoint[direct://getMessageProcessor]")

             

            I tried this " template.send("direct:getMessageProcessor",new GetMessageProcessor());  " instead of  " template.send("direct:getMessageProcessor",exchange); "

             

            and the GetMessageProcessor Route gets invoked successfully,

            -----------Invoking route----------------

            -----Inside GetMessageProcessor-----

            -----End GetMessageProcessor-------

            -----------END----------------


            But the problem with this approach is org.apache.camel.Exchange doesn't get transferred to GetMessageProcessor, as i'm not using " template.send("direct:getMessageProcessor",exchange); " to invoke the route