当前位置: 首页 > 知识库问答 >
问题:

如何使用JSch和Spring集成实现会话超时后的SFTP

元胡媚
2023-03-14

我有通过sftpinboundfilesynchronizerSFTPs文件应用程序:

@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;
}

会话工厂是:

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory();
    sftpSessionFactory.setHost(applicationProperties.getSftpHost());
    sftpSessionFactory.setPort(applicationProperties.getSftpPort());
    sftpSessionFactory.setUser(applicationProperties.getSftpUser());
    sftpSessionFactory.setPassword(applicationProperties.getSftpPassword());
    sftpSessionFactory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(sftpSessionFactory);
}

SFTPinBoundFileSynchronizingMessageSource设置为使用复合触发器轮询。

@Bean
@InboundChannelAdapter(autoStartup="true", channel = "sftpChannel", poller = @Poller("pollerMetadata"))
public SftpInboundFileSynchronizingMessageSource sftpMessageSource() {
    SftpInboundFileSynchronizingMessageSource source =
            new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
    source.setLocalDirectory(applicationProperties.getScheduledLoadDirectory());
    source.setAutoCreateLocalDirectory(true);
    CompositeFileListFilter<File> compositeFileFilter = new CompositeFileListFilter<File>();
    compositeFileFilter.addFilter(new LastModifiedFileListFilter());
    compositeFileFilter.addFilter(new FileSystemPersistentAcceptOnceFileListFilter(store, "dailyfilesystem"));
    source.setLocalFilter(compositeFileFilter);
    source.setCountsEnabled(true);
    return source;
}

轮询器是用复合触发器设置的。它有一个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());
}

RetryCompoundTriggerAdvice

@Component
public class RetryCompoundTriggerAdvice extends AbstractMessageSourceAdvice {

    private final static Logger logger = LoggerFactory.getLogger(RetryCompoundTriggerAdvice.class);

    private final CompoundTrigger compoundTrigger;

    private final Trigger override;

    private final ApplicationProperties applicationProperties;

    private final Mail mail;

    private int attempts = 0;

    public RetryCompoundTriggerAdvice(CompoundTrigger compoundTrigger, 
            @Qualifier("secondaryTrigger") Trigger override, 
            ApplicationProperties applicationProperties,
            Mail mail) {
        this.compoundTrigger = compoundTrigger;
        this.override = override;
        this.applicationProperties = applicationProperties;
        this.mail = mail;
    }

    @Override
    public boolean beforeReceive(MessageSource<?> source) {
        return true;
    }

    @Override
    public Message<?> afterReceive(Message<?> result, MessageSource<?> source) {
        final int  maxOverrideAttempts = applicationProperties.getMaxFileRetry();
        attempts++;
        if (result == null && attempts < maxOverrideAttempts) {
            logger.info("Unable to find load file after " + attempts + " attempt(s). Will reattempt");
            this.compoundTrigger.setOverride(this.override);
        } else if (result == null && attempts >= maxOverrideAttempts) {
            mail.sendAdminsEmail("Missing File");
            attempts = 0;
            this.compoundTrigger.setOverride(null);
        }
        else {
            attempts = 0;
            this.compoundTrigger.setOverride(null);
            logger.info("Found load file");
        }
        return result;
    }

    public void setOverrideTrigger() {
        this.compoundTrigger.setOverride(this.override);
    }

    public CompoundTrigger getCompoundTrigger() {
        return compoundTrigger;
    }
}
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

我尝试在DefaultsFtpSessionFactory上调用SetServerAliveInterval但没有成功。注意:在文件被检索或尝试了5次之后,我想停止与SFTP服务器的通信,直到下一次cron运行。

更新我终于能够得到一个测试执行。按请求添加调试日志。注意:在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线程在每分钟轮询一次,这是另一个轮询器用于文件系统同步的结果。

共有1个答案

郭知
2023-03-14

缓存应该检测到会话不再有效,并发出一个新的会话。我需要查看调试日志(对于spring和jsch)来弄清楚发生了什么。

但是,如果您每天只运行一次,就真的不需要缓存会话,只需要直接使用会话工厂,您就会在每次轮询时得到一个新的会话。

 类似资料:
  • 我有一个用Spring Boot1.5.13构建的java应用程序,我没有设法将会话超时设置为60分钟。我发现Spring将默认的会话超时设置为30分钟。在这个项目上,我们使用: > HttpSecurity的自定义配置 protected void configure(HttpSecurity http){ http.SuccessHandler((httpServletRequest,http

  • 我试图使用Spring Integration创建一个iso8385 TCP服务器。典型的情况如下: 客户端连接到服务器并保存ISO8385消息 服务器处理消息 服务器制定响应并关闭连接 我希望跟踪每个新的TCP连接,并为其创建一个标识符,以便将每个处理与连接的客户端相关联。但我不知道怎么做。这个想法是: 将ISO8385转换为java类的tcp入站适配器 将处理消息的服务激活器 将java类转换

  • 我已经使用Spring Security两周了,除了匿名用户和会话超时之外,它工作得很好。 用例#1 null 如果他们的会话超时,将显示invalidSessionUrl页面,并指示用户登录。 我已经阅读了大量的SO和博客文章,但我似乎找不到用例1的解决方案。我使用的是Spring4.1.6.RELEASE、Spring Security 4.0.2 RELEASE和Tomcat8。 下面是我的

  • 问题内容: 我的应用程序当前将Spring Session与Redis一起用作后端。 我搜索了Spring Session 的官方文档,但使用该模块时找不到默认的会话超时。 另外,我不确定如果需要如何更改默认超时。 有人可以请教吗? 问题答案: 使用Redis存储库时配置会话超时的最简单方法是 当存储库中不再有会话时,该会话将 过期 。可以同时在和上配置超时。默认值为 30分钟 。 如果您使用的是

  • 我使用Spring Boot和Spring会话来控制一个使用ReactJS作为前端的应用程序。我的问题很简单,我尝试了几种方法来处理,但都没有成功。 React部分使用AJAX在登录后调用Spring REST服务(我也使用Spring Security),这至少需要30分钟。之后,会话停止,所有调用都会收到一个302,并以登录页面作为响应。这是意料之中的。 但我的问题是:有什么更好的方法来延长后

  • 在我的Vaadin应用程序中,当Vaadin在“session Timeout”消息之后没有使会话无效时,我遇到了一个问题。收到此消息后,用户有时可以单击链接或刷新页面,并继续工作,就像他们仍在登录一样。我使用以下参数: Last参数(session-timeout)也在context.xml(session-timeout=900)和web.xml(session-config/session-