2016-01-06 59 views
1

在此示例中,我尝试在调用businessLogic之前设置对象依赖项。我收到一个空指针,因为'消费者'对象没有设置。Apache Camel:在使用Spring之前设置对象依赖项

以下是该示例的基础,主要尝试使用Spring DSL。

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

部分:基于定时器的轮询消费者

这里是我的骆驼/ Spring配置:

<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代码

@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; 
      }   
     } 
    } 
} 

有一个空指针的用法的消费对象。我在想,春天不只是自动装配这个豆。即使我没有使用spring,我如何将消费者模板对象传递给这个bean?

+0

到目前为止,这似乎是最好的办法,直到我看到一个更好一。 \t @EndpointInject(uri =“activemq:consumer”) \t私人ConsumerTemplate消费者; –

+0

如果这是解决你的问题,你可以添加这个答案并接受它。 –

回答

0

这应该工作

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

取出@AutoWire,我检查上为什么@Autowire不由方式工作