Search code examples
springapache-camelesb

Apache Camel: set an object dependency before operation with Spring


In this example, I am trying to set the object dependency before calling businessLogic. I am receiving a nullpointer because that 'consumer' object is not set.

Here is the basis of the example and mostly trying to use the Spring DSL.

http://camel.apache.org/polling-consumer

Section: Timer based polling consumer

Here is my camel/spring config:

  <bean id="simpleOutboxMessageConsumer" class="org.berlin.camel.esb.logs.mq.SimplePrintMessageConsumer"/>


  <!-- Continue with spring dsl for ESB -->
  <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring">

    <!-- Define a MQ consumer template -->
    <consumerTemplate id="consumer" />

....
   </camelContext>

      <route id="fromOutboxAndConsume">           
          <from uri="timer://foo?period=30000" />
          <to uri="bean:simpleOutboxMessageConsumer?method=businessLogic" />                                        
      </route>

Java code

@Component
public class SimplePrintMessageConsumer {   
    private static final Logger logger = Logger.getLogger(SimplePrintMessageConsumer.class);        
    private int count;  
    @Autowired 
    private ConsumerTemplate consumer;          
    public void setConsumer(final ConsumerTemplate consumer) {
        this.consumer = consumer;
    }
    public void businessLogic() {
        logger.info("Launching business logic to consume outbox, blocking until we get a message >>>");
        while (true) {
            // Consume the message
            final String msg = consumer.receiveBody("activemq:queue.outbox", 3000, String.class);
            logger.info("Printing message found from queue: " + msg);
            if (msg == null) {
                // no more messages in queue
                break;
            }        
        }
    }   
}

There is a nullpointer at the usage of the consume object. I am thinking that spring is not just autowiring that bean properly. Even if I didn't use spring, how would I pass the consumer template object to this bean?


Solution

  • This should work

    <bean id="simpleOutboxMessageConsumer" class="....SimplePrintMessageConsumer">
    <property name="consumer" ref="consumer"/>
    </bean>
    

    Remove the @AutoWire , I am checking on why the @Autowire is not working by the way