gpt4 book ai didi

java - 如何确保 SFTP session 始终在 spring-batch 结束时关闭

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:08 25 4
gpt4 key购买 nike

我的应用程序基于 spring-boot 2.1.6,配备 spring-batch( block 方法)和 spring-integration 来处理 SFTP。

高级功能是从数据库获取数据,生成文本文件,然后通过 SFTP 发送它,此任务每 30 分钟运行一次。

此应用程序已在生产环境中运行了一段时间,但如果我看到日志,则存在有关 ssh_msg_disconnect 11 空闲连接 的错误。它会一直保持这种状态,直到我重新启动应用程序。

下面是我的应用程序代码:

SftpConfig.java

@Configuration
public class SftpConfig {

@Autowired
ApplicationProperties applicationProperties;

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(applicationProperties.getSftp().getHost());
factory.setUser(applicationProperties.getSftp().getUser());
factory.setPassword(applicationProperties.getSftp().getPass());
factory.setAllowUnknownKeys(true);

return new CachingSessionFactory<>(factory);
}

@Bean
@ServiceActivator(inputChannel = "toSftpChannel", adviceChain = "retryAdvice")
public MessageHandler handler() {
final SftpMessageHandler handler = new SftpMessageHandler(this.sftpSessionFactory());
handler.setRemoteDirectoryExpression(new LiteralExpression(applicationProperties.getSftp().getPath()));
handler.setFileNameGenerator((final Message<?> message) -> {
if (message.getPayload() instanceof File) {
return ((File) message.getPayload()).getName();
} else {
throw new IllegalArgumentException("File expected as payload.");
}
});

return handler;
}

@Bean
public RequestHandlerRetryAdvice retryAdvice() {
final RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
final RetryTemplate retryTemplate = new RetryTemplate();
final SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(NumberConstants.FIVE);
retryTemplate.setRetryPolicy(retryPolicy);
advice.setRetryTemplate(retryTemplate);

return advice;
}

@MessagingGateway
public interface UploadGateway {

@Gateway(requestChannel = "toSftpChannel")
void upload(File file);
}

}

发送文件到sftp的步骤


@Autowired
UploadGateway uploadGateway;

private boolean uploadToSharedFolderSuccess(final PaymentStatus paymentStatus, final String strLocalTmpPath) {
try {
final File fileLocalTmpFullPath = new File(strLocalTmpPath);
uploadGateway.upload(fileLocalTmpFullPath);
} catch (final Exception e) {
paymentStatus.setStatus(ProcessStatus.ERROR.toString());
paymentStatus.setRemark(StringUtil.appendIfNotEmpty(paymentStatus.getRemark(),
"Error during upload to shared folder - " + e.getMessage()));
}
return !StringUtils.equalsIgnoreCase(ProcessStatus.ERROR.toString(), paymentStatus.getStatus());
}

从错误中,我知道似乎我打开了太多连接。但我不确定如何检查 spring-batch 的每个末端的连接是否都已关闭。

最佳答案

如果您不将 session 工厂包装在 CachingSessionFactory 中, session 将在每次使用后关闭。

@Bean
public DefaultSftpSessionFactory sftpSessionFactory() {
final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(applicationProperties.getSftp().getHost());
factory.setUser(applicationProperties.getSftp().getUser());
factory.setPassword(applicationProperties.getSftp().getPass());
factory.setAllowUnknownKeys(true);

return factory;
}

关于java - 如何确保 SFTP session 始终在 spring-batch 结束时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com