【问题标题】:Apache Camel FTP goes into endless loop when deleting files which are less than the specified amount删除小于指定数量的文件时,Apache Camel FTP 进入无限循环
【发布时间】:2021-12-20 15:35:18
【问题描述】:

我已经设置了 Apache Camel Route,它从 FTP 服务器读取 XML 文件,将内容转换为 Java 对象,将创建的 Java 对象聚合到列表中,然后将列表转储到直接对象以供 ConsumerTemplate 读取.待处理文件数为10个。

这是我的路线:

JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{Dokumente.class});
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat();
jaxbDataFormat.setContext(jaxbContext);

from("ftp://tstusr@localhost/uploads/Replies?delete=true&password=ftpServPW&passiveMode=true&binary=true&disconnect=true")
        .unmarshal(jaxbDataFormat)
        .aggregate(AggregationStrategies.flexible(Dokumente.class).accumulateInCollection(LinkedList.class))
        .constant(true)
        .completionSize(10)
        .to("direct:files");

Route 正在做它应该做的事情,除了“delete=true”部分。当我的目录中的文件少于 10 个时,程序进入无限循环。在我的本地 FTP 测试服务器(FileZilla 服务器)中,我可以看到它登录、更改目录、执行 LIST 命令然后退出。它在无限循环中重复执行此操作。

如何修复 Route 以阻止它这样做?我不知道目录中有多少文件。这意味着总有可能小于批量大小。我使用的是 3.11.4 版的 Apache Camel。

【问题讨论】:

  • 你确定那个无限循环吗? FTP 是一个轮询消费者,因此默认情况下它每隔500ms 左右检查一次新文件。

标签: java ftp apache-camel filezilla


【解决方案1】:

问题在于,如果可用文件少于指定的文件数量,路由会一直等待丢失的文件出现。

因此,如果告诉它下载 10 个文件,但只有 2 个存在,它将等待 8 个丢失的文件。

我必须先给它一个 ID 来手动终止路由。然后我必须设置路由选项 sendEmptyMessageWhenIdle=true 并附加一个处理器,该处理器在收到空消息时停止路由。由于空消息没有做任何事我事后将其过滤掉然后强制 com 调整后的路由看起来是这样的

        from("ftp://dm@localhost/uploads/Replies?delete=true&password=ftpPass&passiveMode=true&binary=true&disconnect=true&sendEmptyMessageWhenIdle=true") 
            .routeId("ftpRoute")
            .process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    if (exchange.getMessage().getBody() == null) {
                        exchange.getContext().getRouteController().stopRoute("ftpRoute");
                    }
                }
            })
            .filter(simple("${body} != null"))
            .unmarshal(jaxbDataFormat)
            .aggregate(AggregationStrategies.flexible(Dokumente.class).accumulateInCollection(LinkedList.class)) 
            .constant(true) 
            .completionSize(10) 
            .forceCompletionOnStop()
            .to("direct:files"); 

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多