Java Spring 集成 SFTP 示例与 Spring Boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41151569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:11:57  来源:igfitidea点击:

Spring Integration SFTP Example with Spring Boot

javaspring-integrationsftp

提问by tjholmes66

We are using the latest Spring Boot for a Spring app and using the latest Spring Integration for SFTP. I've been to the Spring Integration SFTP documentation site, and I took the Spring Boot Configuration as is:

我们正在为 Spring 应用程序使用最新的 Spring Boot,并为 SFTP 使用最新的 Spring Integration。我去过 Spring Integration SFTP 文档站点,我按原样使用了 Spring Boot 配置:

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

    };
}

Let me be clear, after cutting and pasting, there are some unit tests that run. However, when loading the application context there was an error message because the Polling wasn't there.

让我说清楚,在剪切和粘贴之后,有一些运行的单元测试。但是,在加载应用程序上下文时会出现错误消息,因为轮询不存在。

When I googled that error, other posts on StackOverflow said I also had to add to remove this error message when loading the application context.

当我用谷歌搜索该错误时,StackOverflow 上的其他帖子说我还必须在加载应用程序上下文时添加以删除此错误消息。

@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata defaultPoller() {

    PollerMetadata pollerMetadata = new PollerMetadata();
    pollerMetadata.setTrigger(new PeriodicTrigger(60));
    return pollerMetadata;
}

When I added this code, THEN at least my build would work and the tests would run because the application context was now being loaded correctly.

当我添加这段代码时,那么至少我的构建会工作并且测试会运行,因为应用程序上下文现在被正确加载。

Now I am looking for a code sample on how to make this work and move files? The Spring Integration SFTP examples on GitHub are ok, but not great ... far from it.

现在我正在寻找有关如何进行此工作和移动文件的代码示例?GitHub 上的 Spring Integration SFTP 示例还可以,但不是很好……远非如此。

The Basic Spring Integration Example shows how to read files from an SFTP Server, if the data is configured with an application-context.xml file. Where is the example where a Spring Boot configuration is used, and then the code to read from that server, and the code for the test?

如果数据是使用 application-context.xml 文件配置的,则基本 Spring 集成示例展示了如何从 SFTP 服务器读取文件。使用 Spring Boot 配置的示例在哪里,然后是从该服务器读取的代码,以及用于测试的代码?

I understand that regardless of whether you use a Java class for Spring Boot configuration or an application-context.xml file ... the working code should work the same for autowired SFTP channels and some inbound channel adapter.

我明白,无论您是使用 Java 类进行 Spring Boot 配置还是使用 application-context.xml 文件……工作代码对于自动连接的 SFTP 通道和一些入站通道适配器都应该相同。

So here is the code, I am trying to make work:

所以这是代码,我正在努力工作:

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

Currently, "sftpChannelAdapter.start();" is the part I am having trouble with. This implementation does not find the "SourcePollingChannelAdapter" class.

当前,“sftpChannelAdapter.start();” 是我遇到问题的部分。此实现找不到“SourcePollingChannelAdapter”类。

If this was defined in the classic XML application context with an "id" then this code autowires just fine. With a Spring Boot configuration, it doesn't look like you can define an "id" for a bean.

如果这是在带有“id”的经典 XML 应用程序上下文中定义的,那么此代码自动装配就好了。使用 Spring Boot 配置,您似乎无法为 bean 定义“id”。

This just stems from my lack of knowledge on how to convert from using a traditional application-context XML file WITH annotations in the code, to using a complete Spring Boot application context configuration file.

这只是因为我缺乏关于如何从在代码中使用带有注释的传统应用程序上下文 XML 文件转换为使用完整的 Spring Boot 应用程序上下文配置文件的知识。

Any help with this is much appreciated. Thanks!

非常感谢您对此的任何帮助。谢谢!

采纳答案by Gary Russell

I don't understand the question; you said

我不明白这个问题;你说

I had to add ... to make it work

我不得不添加......以使其工作

and then

进而

Now I am looking for a code sample on how to make this work?

现在我正在寻找有关如何使其工作的代码示例?

What is not working?

什么不工作?

You can also use

你也可以使用

@InboundChannelAdapter(value = "sftpChannel", poller = @Poller(fixedDelay = "5000"))

instead of adding a default poller definition.

而不是添加默认的轮询器定义。

We will fix the docs for the missing poller config.

我们将修复缺少的轮询配置的文档

EDIT

编辑

I just copied the code into a new boot app (with the poller config) and it works as expected.

我只是将代码复制到一个新的启动应用程序中(使用轮询配置),它按预期工作。

@SpringBootApplication
public class SftpJavaApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SftpJavaApplication.class).web(false).run(args);
    }

    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
        factory.setHost("...");
        factory.setPort(22);
        factory.setUser("...");
        factory.setPassword("...");
        factory.setAllowUnknownKeys(true);
        return new CachingSessionFactory<LsEntry>(factory);
    }

    @Bean
    public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
        SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory("foo");
        fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "5000"))
    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());
            }

        };
    }

}

Result:

结果:

16:57:59.697 [task-scheduler-1] WARN  com.jcraft.jsch - Permanently added '10.0.0.3' (RSA) to the list of known hosts.
ftp-inbound/bar.txt
ftp-inbound/baz.txt