Apache Camel: Polling consumer

♀尐吖头ヾ 提交于 2020-01-23 17:52:05

问题


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


回答1:


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());

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

see the docs for more details: http://camel.apache.org/polling-consumer




回答2:


Camel supports implementing the Polling Consumer from the EIP patterns using the PollingConsumer interface which can be created via the Endpoint.createPollingConsumer() method.

This is also known as a synchronous receiver, because the receiver thread blocks until a message is received. We call it a Polling Consumer because the receiver polls for a message, processes it, then polls for another. As a convenience, messaging API’s usually provide a receive() method that blocks until a message is delivered, in addition to methods like receiveNoWait() and receive(0) that return immediately if no message is available.

Eg

ActiveMq Consumer

<from uri="activemq:someQueue"/>
<to uri="direct:somepath"/>

Periodic Consumer

<from uri="timer://foo?period=5000"/>
 <to uri="direct:somepath"/>

For more information regarding Polling Consumer



来源:https://stackoverflow.com/questions/22046197/apache-camel-polling-consumer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!