我们正在使用最新的Spring Boot for Spring应用程序并使用最新的Spring Integration for SFTP . 我去过Spring Integration SFTP文档站点,我按原样使用Spring Boot Configuration:
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost("localhost");
factory.setPort(port);
factory.setUser("foo");
factory.setPassword("foo");
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory("/");
fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xml"));
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "sftpChannel")
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source =
new SftpInboundFileSynchronizingMessageSource(sftpInboundFileSynchronizer());
source.setLocalDirectory(new File("ftp-inbound"));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
return source;
}
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
return new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
System.out.println(message.getPayload());
}
};
}
让我明白一下,在切割和粘贴之后,会有一些单元测试运行 . 但是,加载应用程序上下文时出现错误消息,因为轮询不存在 .
当我用Google搜索该错误时,StackOverflow上的其他帖子说我还必须添加以在加载应用程序上下文时删除此错误消息 .
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata defaultPoller() {
PollerMetadata pollerMetadata = new PollerMetadata();
pollerMetadata.setTrigger(new PeriodicTrigger(60));
return pollerMetadata;
}
当我添加此代码时,那么至少我的构建将起作用并且测试将运行,因为现在正在正确加载应用程序上下文 .
现在我正在寻找有关如何使这项工作和移动文件的代码示例? GitHub上的Spring Integration SFTP示例没问题,但不是很好......远非如此 .
Basic Spring集成示例显示了如果使用application-context.xml文件配置数据,如何从SFTP服务器读取文件 . 使用Spring Boot配置的示例在哪里,然后是从该服务器读取的代码,以及测试的代码?
我知道无论您是使用Java类进行Spring Boot配置还是使用application-context.xml文件......对于自动装配的SFTP通道和某些入站通道适配器,工作代码应该是相同的 .
所以这是代码,我正在努力工作:
@Component
@Profile("sftpInputFetch")
public class SFTPInputFetcher implements InputFetcher
{
// The PollableChannel seems fine
@Autowired
PollableChannel sftpChannel;
@Autowired
SourcePollingChannelAdapter sftpChannelAdapter;
@Override
public Stream<String> fetchLatest() throws FileNotFoundException
{
Stream<String> stream = null;
sftpChannelAdapter.start();
Message<?> received = sftpChannel.receive();
File file = (File)received.getPayload();
// get Stream<String> from file
return stream;
}
目前,“sftpChannelAdapter.start();”是我遇到麻烦的部分 . 此实现未找到“SourcePollingChannelAdapter”类 .
如果这是在具有“id”的经典XML应用程序上下文中定义的,则此代码自动完成 . 使用Spring Boot配置,看起来您不能为bean定义“id” .
这仅仅源于我对如何使用传统的应用程序上下文XML文件(在代码中使用注释)转换为使用完整的Spring Boot应用程序上下文配置文件缺乏知识 .
对此有任何帮助非常感谢 . 谢谢!
1 回答
我不明白这个问题;你说
然后
什么不起作用?
你也可以使用
而不是添加默认的轮询器定义 .
我们将fix the docs for the missing poller config .
EDIT
我只是将代码复制到一个新的启动应用程序(使用poller配置),它按预期工作 .
结果: