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

Camel SFTP如何复用同一连接全局配置文件名与目录?

解决方案

Camel 3.7.5版本不需要引入额外组件,有两种方式可以实现SFTP配置一次、复用连接的需求:

方案1:配置SFTP组件全局默认参数

你可以直接给SFTP组件设置全局公共属性,所有该组件创建的endpoint都会自动继承这些参数,无需重复配置:

  • XML配置示例:
<bean id="sftp" class="org.apache.camel.component.file.remote.SftpComponent">
    <property name="host" value="localhost"/>
    <property name="port" value="22"/>
    <property name="username" value="user"/>
    <property name="password" value="password"/>
</bean>
  • Java配置示例:
SftpComponent sftp = camelContext.getComponent("sftp", SftpComponent.class);
sftp.setHost("localhost");
sftp.setPort(22);
sftp.setUsername("user");
sftp.setPassword("password");

配置完成后,你原来的endpoint可以简化为如下形式,无需重复填写主机、账号、密码信息:

<endpoint id="sftpPollCSVFile" uri="sftp:/input/test">
    <property key="include" value=".csv" />
    <property key="streamDownload" value="true" />
</endpoint>
<endpoint id="sftpWriteFileToTest1" uri="sftp:/output/test1">
    <property key="fileName" value="test1.csv" />
</endpoint>
<endpoint id="sftpWriteFileToTest2" uri="sftp:/output/test2">
    <property key="fileName" value="test2.csv" />
</endpoint>

方案2:使用属性占位符提取公共配置

你也可以将SFTP公共参数抽取到配置文件中,通过占位符复用:

  1. 配置文件中添加公共配置项:
sftp.host=localhost
sftp.port=22
sftp.username=user
sftp.password=password
  1. endpoint中引用占位符即可:
<endpoint id="sftpPollCSVFile"
    uri="sftp://${sftp.username}@${sftp.host}:${sftp.port}/input/test?password=${sftp.password}">
    <property key="include" value=".csv" />
    <property key="streamDownload" value="true" />
</endpoint>

连接复用说明

Camel SFTP组件底层默认会复用同一主机+端口+用户名对应的连接,你哪怕定义多个endpoint,只要核心连接参数一致,底层就只会创建一个连接实例,不会额外占用资源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 11:27:00