Apache Camel:轮询消费者 [英] Apache Camel: Polling consumer
问题描述
我是 Apache Camel 的新手,我试图在一个简单的项目中理解和使用轮询消费者 EIP,但我觉得有点迷茫..有人可以帮我解释一下,甚至是一个小的工作示例.
I'm new to Apache Camel and I'm trying to understand and use the Polling Consumer EIP in a simple project but I feel a little bit lost.. Could someone please help me with a little explanation or even with a little working example.
任何帮助将不胜感激提前致谢
Any help would be appreciated Thanks in advance
推荐答案
对于大多数用例,您可以通过在路由中的 from()
子句中定义它们来创建消费者...
for most use cases, you can create a consumer by just defining them in the from()
clause in a route...
from("activemq:inbox").to(new MyProcessor());
但是,您也可以编写自己的 POJO 轮询使用者逻辑以更好地控制使用者逻辑...只需使用计时器定期启动它并调用 receive()
方法,如下所示:
but, you can also write your own POJO polling consumer logic for more control over the consumer logic...simply initiate it periodically with a timer and call the receive()
method as follows:
from("timer://foo?period=5000").bean(MyBean, "processQueue");
public void processQueue() {
while (true) {
// receive the message from the queue, wait at most 3 sec
String msg = consumer.receiveBody("activemq:inbox", 3000, String.class);
if (msg == null) {
// no more messages in queue
break;
}
// do something with body
}
}
查看文档了解更多详情:http://camel.apache.org/polling-consumer一个>
see the docs for more details: http://camel.apache.org/polling-consumer
这篇关于Apache Camel:轮询消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!