Camel-FTP only run once?

混江龙づ霸主 提交于 2019-12-12 00:42:54

问题


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

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