首页 > 解决方案 > Spring Integration Sftp:有时需要超过 15 分钟才能完成操作

问题描述

我正在使用 Spring 集成 sftp 将文件放在远程服务器上,下面是配置。

<spring-integration.version>5.2.5.RELEASE</spring-integration.version>

我已经配置了@MessagingGateway。

@MessagingGateway
public interface SftpMessagingGateway {
 
     @Gateway(requestChannel = "sftpOutputChannel")
     void sendToFTP(Message<?> message);
     
}

我已将 MessageHandler 配置如下,

@Bean
@ServiceActivator(inputChannel = "sftpOutputChannel" )
public MessageHandler genericOutboundhandler() {
    SftpMessageHandler handler = new SftpMessageHandler(outboundTemplate(), FileExistsMode.APPEND);
    handler.setRemoteDirectoryExpressionString("headers['remote_directory']");
    handler.setFileNameGenerator((Message<?> message) -> ((String) message.getHeaders().get(Constant.FILE_NAME_KEY)));
    handler.setUseTemporaryFileName(false);
    return handler;
}

我已经配置了 SftpRemoteFileTemplate 如下

private SftpRemoteFileTemplate outboundTemplate;

public SftpRemoteFileTemplate outboundTemplate(){
    if (outboundTemplate == null) {
        outboundTemplate = new SftpRemoteFileTemplate(sftpSessionFactory());
    }
    return outboundTemplate;
}

这是 SessionFactory 的配置

public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
    factory.setHost(host);
    factory.setPort(port);
    factory.setUser(username);
    factory.setPassword(password);
    factory.setAllowUnknownKeys(true);
    factory.setKnownHosts(host);
    factory.setSessionConfig(configPropertiesOutbound());
    CachingSessionFactory<LsEntry> csf = new CachingSessionFactory<LsEntry>(factory);
    csf.setSessionWaitTimeout(1000);
    csf.setPoolSize(10);
    csf.setTestSession(true);
    return csf;
}

我已经在其中一项服务中配置了所有这些。

现在的问题是,

有时整个操作需要超过 15 分钟~ 特别是如果服务是理想的几个小时,我不确定是什么导致了这个问题。看起来它正在花费时间从 CachedSessionFactory 获取活动会话,下面的操作非常快是我设法捕获时间的工具之一的快照。

在此处输入图像描述

传输文件通常需要几毫秒。

我最近做了以下更改,但在此之前我也遇到了同样的问题,

我想我配置错误,这就是为什么我最终打桩连接?还是有别的东西?

观察:

完成操作所花费的时间在问题发生时都有些相似,即 938000 毫秒 +

如果我每天重新启动应用程序,它工作得很好。

标签: spring-bootspring-integrationjschspring-integration-sftp

解决方案


推荐阅读