使用SFTP流入站通道适配器时,会话似乎未关闭。当适配器耗尽sftp服务器上可用的会话时,spring会抛出com.jcraft.jsch.JSchException。
当RemoteFileTemplate
在服务器上执行ls时,我看到它打开并关闭了会话,但是,当AbstractRemoteFileStreamingMessageSource
执行doReceive
时,我看不到对关闭会话。这可能是我的错误配置。
以下是我的配置:
@Bean
public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(properties.getHost());
factory.setPort(properties.getPort());
factory.setUser(properties.getUsername());
factory.setPassword(properties.getPassword());
CachingSessionFactory cachingSessionFactory = new CachingSessionFactory<>(factory);
return cachingSessionFactory;
}
@Bean
public SftpRemoteFileTemplate template() {
return new SftpRemoteFileTemplate(sftpSessionFactory());
}
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(cron = "0/5 * * * * *", maxMessagesPerPoll = "10"))
public MessageSource<InputStream> sftpMessageSource(ConcurrentMetadataStore concurrentMetadataStore) {
ChainFileListFilter<ChannelSftp.LsEntry> filterChain = new ChainFileListFilter<>();
if (properties.getFilterPattern() != null && !"".equals(properties.getFilterPattern()))
filterChain.addFilter(new SftpRegexPatternFileListFilter(properties.getFilterPattern()));
filterChain.addFilter(new SftpPersistentAcceptOnceFileListFilter(concurrentMetadataStore, "sftpSource"));
SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template());
messageSource.setRemoteDirectory(properties.getRemoteDirectory());
messageSource.setFilter(filterChain);
return messageSource;
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler(SftpDocumentInput sftpDocumentInput) {
return sftpDocumentInput;
}
感谢您的帮助!