Search code examples
apache-camelpollingproducer-consumer

Apache Camel: Polling consumer


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


Solution

  • 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