Mule 4中SFTP连接器On New or Updated File触发异常问题
MuleSoft SFTP触发器读取文件时连接通道关闭异常排查与解决
问题场景
需求为SFTP文件上传后自动拾取并处理:
- 第一步通过SFTP连接器的
On New or Updated File触发器获取文件名 - 第二步将文件名传入SFTP Read连接器获取文件数据
手动传入文件名时SFTP Read可正常获取数据,但触发器触发时出现连接异常,且两种场景使用相似的连接器配置。
原配置代码
<sftp:config name="SFTP_Conf" doc:name="SFTP Config" doc:id="62dfd75b-941c-ec70c3bdf3" > <sftp:connection host="${sftp.host}" port="${sftp.port}" username="${sftp.username}" password="${sftp.password}" identityFile="${sftp.identityFile}" passphrase="${sftp.passphrase}" workingDir="365_CRM"> <pooling-profile maxIdle="1" minEvictionMillis="60000" evictionCheckIntervalMillis="30000" /> <sftp:listener doc:name="On New or Updated File" doc:id="7b435458-a0c2-b1b5bda7b19" config-ref="SFTP_Conf" watermarkEnabled="true" outputMimeType="application/xlsx" outputEncoding="ISO-8859-1"> <scheduling-strategy > <fixed-frequency /> </scheduling-strategy> </sftp:listener> </sftp:connection> <expiration-policy /> </sftp:config>
错误日志
Found exception trying to poll directory '/365_CRM'. Will try again on the next poll. org.mule.runtime.api.exception.MuleRuntimeException: Found exception trying to obtain path /365_CRM/abc.xlsx Caused by: org.mule.runtime.api.exception.MuleRuntimeException: Could not obtain attributes for path /365_CRM/abc.xlsx Caused by: org.mule.extension.sftp.internal.exception.SftpConnectionException: Error occurred while trying to connect to host Caused by: org.mule.runtime.extension.api.exception.ModuleException: org.mule.runtime.api.connection.ConnectionException: Channel is being closed
排查与解决方案
1. 修正监听器配置结构
原配置中sftp:listener嵌套在sftp:connection内部属于语法错误,会导致连接复用逻辑异常。正确做法是将监听器独立出来,引用SFTP配置。
2. 调整连接池参数
当前maxIdle=1的连接池配置过于严苛,触发器轮询时容易出现连接被回收或不足的情况。建议调整:
- 增大
maxIdle至3-5,保证有足够的空闲连接可用 - 延长
minEvictionMillis和evictionCheckIntervalMillis,减少连接频繁回收的概率
3. 检查路径拼接是否重复
SFTP配置中已指定workingDir="365_CRM",触发器获取的文件名无需再添加/365_CRM/前缀,重复拼接会导致路径错误,触发连接异常。
4. 添加连接超时配置
在sftp:connection中增加connectionTimeout属性,避免连接未完全建立就被尝试使用。
修正后的配置示例
<sftp:config name="SFTP_Conf" doc:name="SFTP Config" doc:id="62dfd75b-941c-ec70c3bdf3" > <sftp:connection host="${sftp.host}" port="${sftp.port}" username="${sftp.username}" password="${sftp.password}" identityFile="${sftp.identityFile}" passphrase="${sftp.passphrase}" workingDir="365_CRM" connectionTimeout="30000"> <pooling-profile maxIdle="3" minEvictionMillis="120000" evictionCheckIntervalMillis="60000" /> </sftp:connection> <expiration-policy /> </sftp:config> <sftp:listener doc:name="On New or Updated File" doc:id="7b435458-a0c2-b1b5bda7b19" config-ref="SFTP_Conf" watermarkEnabled="true" outputMimeType="application/xlsx" outputEncoding="ISO-8859-1"> <scheduling-strategy > <fixed-frequency /> </scheduling-strategy> </sftp:listener>
内容的提问来源于stack exchange,提问作者Thiru
相关产品推荐
相关产品推荐

