You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Camel配置maximumReconnectAttempts=0仍反复重连SFTP问题咨询

问题根因

你配置的maximumReconnectAttempts参数作用范围仅为单轮轮询任务内部:设为0代表单次轮询时第一次连接SFTP失败就直接放弃本次轮询,不会在本次轮询中反复重试,和你日志中输出的Will skip this poll告警逻辑是匹配的。
而你看到的多次重连现象是Camel SFTP消费者的默认轮询机制导致的:默认情况下即使本轮轮询失败,组件仍会按照固定间隔触发下一轮轮询任务,和maximumReconnectAttempts配置无关。
另外你给出的路由URI中端口后多写了一个问号,可能会导致部分参数解析异常,建议先修正为单个问号。

解决方案

根据你的预期场景,可选择对应配置:

场景1:连接失败直接抛出异常,不静默跳过轮询

在SFTP URI中新增throwExceptionOnConnectFailed=true参数,开启后连接SFTP失败时会直接抛出连接异常,而非静默等待下一轮轮询。修改后的URI示例:

from("sftp://tester@192.168.1.23:22?noop=true&password=password&maximumReconnectAttempts=0&throwExceptionOnConnectFailed=true")
    .to("file:C:/Users/aminos/Desktop/serverData");

场景2:应用启动时SFTP连接失败就直接终止路由/停止应用

搭配异常处理器捕获连接异常,主动停止路由或关闭Spring上下文即可,示例配置:

// 给路由设置固定id方便后续操作
from("sftp://tester@192.168.1.23:22?noop=true&password=password&maximumReconnectAttempts=0&throwExceptionOnConnectFailed=true")
    .routeId("sftpRoute")
    .errorHandler(deadLetterChannel("direct:connectErrorHandler"))
    .to("file:C:/Users/aminos/Desktop/serverData");

// 异常处理逻辑
from("direct:connectErrorHandler")
    .process(exchange -> {
        Exception caught = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
        // 匹配SFTP连接异常
        if (caught instanceof org.apache.camel.component.file.remote.RemoteFileConnectException) {
            // 停止对应SFTP路由
            exchange.getContext().stopRoute("sftpRoute");
            // 如果需要直接停止整个Spring Boot应用,可注入ApplicationContext调用close()方法
        }
    });

场景3:仅需启动时执行一次拉取,不需要轮询

在SFTP URI中新增delay=-1参数,即可关闭轮询调度,仅在应用启动时执行一次连接拉取任务。


内容的提问来源于stack exchange,提问作者Midovsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 15:06:06