问题
I can't seem to get the camel-ftp component to die when no files are found.
I added a LimitedPollingConsumerPollStrategy
with a limit of 1
:
<bean id="noPoll" class="org.apache.camel.impl.LimitedPollingConsumerPollStrategy">
<property name="limit" value="1"/>
</bean>
and configured the URI to use it:
ftp://user@host.ftp/?password=pass&stepwise=false&binary=true&delete=false&noop=true&pollStrategy=#noPoll
It still just hangs, looking for files, when it doesn't find any.. so I added &sendEmptyMessageWhenIdle=true
to the URI.
I added conditions to my route to output to log when a message comes through with a null body and I saw a flood of those messages so it seems the limit on the polling consumer isn't working. I tried changing it to &consumer.pollStrategy=#noPoll
and it behaved the same.
回答1:
The following PollStrategy
will stop the consumer if no messages are consumed.
public class PollOncePollStrategy extends DefaultPollingConsumerPollStrategy {
@Override
public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
try {
if (polledMessages == 0) {
log.info("No polled messages, stopping consumer");
endpoint.getCamelContext().createProducerTemplate().sendBody(String.format("controlbus:route?async=true&action=stop&routeId=%s", EndpointHelper.getRouteIdFromEndpoint(endpoint)), null);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Register it in the camel registry and use as follows: ftp://127.0.0.1/mydir?pollStrategy=#pollOnce
回答2:
The LimitedPollingConsumerPollStrategy
is for limiting when the consumer has failed for X number of times in a row. This is also what is explained in the documentation of it. Its not for stopping after 1 poll.
You can implement your own poll strategy that stops when the commit method is called with the parameter polledMessages = 0. Then you know there was no files polled.
来源:https://stackoverflow.com/questions/18211918/camel-ftp-only-run-once