2016-01-06 37 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不由方式工作

相關問題