Spring integration 如何使用JSch和Spring集成在会话超时后进行SFTP

Spring integration 如何使用JSch和Spring集成在会话超时后进行SFTP,spring-integration,jsch,spring-integration-sftp,Spring Integration,Jsch,Spring Integration Sftp,我有一个应用程序,它可以通过SftpInboundFileSynchronizer来SFTPs文件: @Bean public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() { SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory()); fileSynchronizer

我有一个应用程序,它可以通过
SftpInboundFileSynchronizer
来SFTPs文件:

@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
    SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
    fileSynchronizer.setDeleteRemoteFiles(false);
    fileSynchronizer.setRemoteDirectory(applicationProperties.getSftpDirectory());
    CompositeFileListFilter<ChannelSftp.LsEntry> compositeFileListFilter = new CompositeFileListFilter<ChannelSftp.LsEntry>();
    compositeFileListFilter.addFilter(new SftpPersistentAcceptOnceFileListFilter(store, "sftp"));
    compositeFileListFilter.addFilter(new SftpSimplePatternFileListFilter(applicationProperties.getLoadFileNamePattern()));
    fileSynchronizer.setFilter(compositeFileListFilter);
    fileSynchronizer.setPreserveTimestamp(true);
    return fileSynchronizer;
}
轮询器设置有复合触发器。它有一个cron触发器,用于尝试每天对预期文件进行一次SFTP。如果找不到所需的文件,我会有一个周期性触发器来覆盖cron触发器。它每15分钟尝试一次SFTP文件(最多5次)。获取文件或尝试5次获取文件后,cron触发器将覆盖定期触发器。
RetryCompoundTriggerAdvice
包含此逻辑

@Bean
public PollerMetadata pollerMetadata(RetryCompoundTriggerAdvice retryCompoundTriggerAdvice) {
    PollerMetadata pollerMetadata = new PollerMetadata();
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(retryCompoundTriggerAdvice);
    pollerMetadata.setAdviceChain(adviceChain);
    pollerMetadata.setTrigger(compoundTrigger());
    pollerMetadata.setMaxMessagesPerPoll(1);
    return pollerMetadata;
}

@Bean
public CompoundTrigger compoundTrigger() {
    CompoundTrigger compoundTrigger = new CompoundTrigger(primaryTrigger());
    return compoundTrigger;
}

@Bean
public CronTrigger primaryTrigger() {
    return new CronTrigger(applicationProperties.getSchedule());
}

@Bean
public PeriodicTrigger secondaryTrigger() {
    return new PeriodicTrigger(applicationProperties.getRetryInterval());
}
问题在于,当应用程序在15分钟后重新尝试SFTP时,服务器会话已超时。因此,JSch报告:

2017-01-27 18:30:01.447  INFO 14248 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Caught an exception, leaving main loop due to End of IO Stream Read
2017-01-27 18:30:01.448  INFO 14248 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Disconnecting from my.sftp.com port 22
因此,不会再收到任何消息,应用程序在下一个cron时间(即第二天)之前不会尝试SFTP文件。等待15分钟后,如何重新尝试使用SFTP文件

我尝试在
DefaultSftpSessionFactory
上调用
setServerAliveInterval
,但没有成功。注意:在检索文件或尝试5次之后,我希望在下一次cron运行之前停止与SFTP服务器的通信

更新
我终于能够执行一个测试。根据请求添加调试日志。注意:调试日志设置在prop文件中如下所示:

logging.level.org.springframework: DEBUG
logging.level.com.jcraft.jsch: DEBUG
这是日志:

2017-02-15 18:15:56.206 DEBUG 26748 --- [task-scheduler-9] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:16:56.211 DEBUG 26748 --- [task-scheduler-9] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:17:56.213 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:18:56.214 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:19:56.215 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:20:56.215 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:21:56.217 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:22:56.218 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:23:56.219 DEBUG 26748 --- [task-scheduler-3] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:24:56.221 DEBUG 26748 --- [task-scheduler-2] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:25:56.222 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:26:56.223 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:27:56.224 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:28:56.225 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:29:56.226 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:30:03.884  INFO 26748 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Caught an exception, leaving main
loop due to End of IO Stream Read
2017-02-15 18:30:03.884  INFO 26748 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Disconnecting from my.sftp.com port 22
2017-02-15 18:30:56.227 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:31:56.228 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:32:56.228 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:33:56.230 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'

即使设置了调试级别,JSCH也不会记录调试语句。从日志来看,SFTP连接似乎已终止。注意:由于另一个轮询器用于文件系统同步,task-scheduler-6线程正在每分钟轮询一次。

缓存应检测到会话不再有效,并发出一个新会话。我需要查看调试日志(针对spring和jsch)以了解发生了什么


但是,如果您一天只运行一次,实际上不需要缓存会话,只需直接使用会话工厂,每次轮询都会得到一个新会话。

谢谢您的回复。我正在建立一个测试环境来重现这个问题。完成后,我将发布调试代码。我还将切换到直接使用会话工厂,看看问题是否得到解决。我终于能够进行测试运行了。不幸的是,调试日志可能没有提供任何有用的信息。我应该试试trace吗?此外,从缓存会话转到使用普通的旧会话工厂并没有解决问题。如果没有缓存,则不太可能出现客户端问题。你看过服务器日志了吗?很遗憾,我没有访问SFTP服务器日志的权限。服务器似乎在15分钟空闲时间后终止会话。我的重试间隔约为16分钟。我已将间隔时间设置为<15分钟。这似乎已经摆脱了JSCH信息消息。正在测试这是否解决了我的问题。尽管如此,如果旧会话已终止,是否应该在重试期间创建新会话?您所说的毫无意义;如果删除了缓存会话工厂。在这种情况下,当操作完成并为下一个请求建立新会话时,会话将关闭。因此,不可能有“15分钟的空闲时间”。
2017-01-27 18:30:01.447  INFO 14248 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Caught an exception, leaving main loop due to End of IO Stream Read
2017-01-27 18:30:01.448  INFO 14248 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Disconnecting from my.sftp.com port 22
logging.level.org.springframework: DEBUG
logging.level.com.jcraft.jsch: DEBUG
2017-02-15 18:15:56.206 DEBUG 26748 --- [task-scheduler-9] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:16:56.211 DEBUG 26748 --- [task-scheduler-9] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:17:56.213 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:18:56.214 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:19:56.215 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:20:56.215 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:21:56.217 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:22:56.218 DEBUG 26748 --- [task-scheduler-8] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:23:56.219 DEBUG 26748 --- [task-scheduler-3] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:24:56.221 DEBUG 26748 --- [task-scheduler-2] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:25:56.222 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:26:56.223 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:27:56.224 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:28:56.225 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:29:56.226 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:30:03.884  INFO 26748 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Caught an exception, leaving main
loop due to End of IO Stream Read
2017-02-15 18:30:03.884  INFO 26748 --- [Connect thread my.sftp.com session] com.jcraft.jsch                          : Disconnecting from my.sftp.com port 22
2017-02-15 18:30:56.227 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:31:56.228 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:32:56.228 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'
2017-02-15 18:33:56.230 DEBUG 26748 --- [task-scheduler-6] o.s.i.e.SourcePollingChannelAdapter      : Received no Message during the poll, returning 'false'