...
Code Block |
---|
Endpoint endpoint = context.getEndpoint("activemq:my.queue"); PollingConsumer consumer = endpoint.createPollingConsumer(); Exchange exchange = consumer.receive(); |
Notice in Camel 2.0 we have introduced the ConsumerTemplate
The ConsumerTemplate
(discussed below) is also available.
There are 3 main polling methods on PollingConsumer
Method name | Description |
---|---|
Waits until a message is available and then returns it; potentially blocking forever | |
Attempts to receive a message exchange, waiting up to the given timeout and returning null if no message exchange could be received within the time available | |
Attempts to receive a message exchange immediately without waiting and returning null if a message exchange is not available yet |
ConsumerTemplate
...
The ConsumerTemplate
is a template much like Spring's JmsTemplate or JdbcTemplate supporting the Polling Consumer EIP. With the template you can consume Exchanges from an Endpoint.
...
Option | Default | Description |
---|---|---|
pollStrategy | Camel 2.0: A pluggable | |
sendEmptyMessageWhenIdle | | Camel 2.9: If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead. |
...
ScheduledPollConsumer is scheduled based and its run
method is invoked periodically based on schedule settings. But errors can also occur when a poll is being executed. For instance if Camel should poll a file network, and this network resource is not available then a java.io.IOException
could occur. As this error happens before any Exchange has been created and prepared for routing, then the regular Error handling in Camel does not apply. So what does the consumer do then? Well the exception is propagated back to the run
method where its handled. Camel will by default log the exception at WARN
level and then ignore it. At next schedule the error could have been resolved and thus being able to poll the endpoint successfully.
Controlling the error handling using PollingConsumerPollStrategy
...
org.apache.camel.PollingConsumerPollStrategy
is a pluggable strategy that you can configure on the ScheduledPollConsumer
. The default implementation org.apache.camel.impl.DefaultPollingConsumerPollStrategy
will log the caused exception at WARN
level and then ignore this issue.
...