如何使用 Java Config 配置 SFTP 出站网关?

Jam*_*mes 3 spring-integration spring-integration-sftp

我想get通过 SFTP 使用 SFTP 出站网关访问文件,但我只找到使用 XML 配置的示例。如何使用 Java 配置来完成此操作?

更新(感谢 Artem Bilan 帮助)

我的配置类:

@Configuration
public class MyConfiguration {

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

    @Bean
    @ServiceActivator(inputChannel = "sftpChannel")
    public MessageHandler handler() {
        SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), "get", "#getPayload() == '/home/samadmin/test.endf'");
        sftpOutboundGateway.setLocalDirectory(new File("C:/test/gateway/"));
        return sftpOutboundGateway;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的应用类:

@SpringBootApplication
@EnableIntegration
public class TestIntegrationApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestIntegrationApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在配置成功,但没有发生SFTP。需要弄清楚如何请求 SFTP。

Art*_*lan 5

引用参考手册

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
    return new SftpOutboundGateway(ftpSessionFactory(), "ls");
}
Run Code Online (Sandbox Code Playgroud)

另请注意下一节中的 Java DSL 示例。

编辑

@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler() {
    SftpOutboundGateway sftpOutboundGateway = new  SftpOutboundGateway(sftpSessionFactory(), "get", "payload");
    sftpOutboundGateway.setLocalDirectory(new File("C:/test/gateway/"));
    return sftpOutboundGateway;
}
Run Code Online (Sandbox Code Playgroud)

对于GETSFTP 命令, ctor arg 可能与上面类似 - 只是对所有传入消息的expression引用。Message.getPayload()

在这种情况下,您应该发送给以下sftpChannel人员Message

new GenericMessage<>("/home/samadmin/test.endf");
Run Code Online (Sandbox Code Playgroud)

所以,这/home/samadmin/test.endf就是payload其中的一个Message。当它到达 时SftpOutboundGateway,该表达式将根据该消息进行计算并由getPayload()SpEL 调用。因此,GET将使用远程文件所需的路径来执行该命令。

其他消息可能具有与其他文件完全不同的路径。