3

我想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;
    }

}

我的应用程序类:

@SpringBootApplication
@EnableIntegration
public class TestIntegrationApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestIntegrationApplication.class, args);
    }
}

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

4

1 回答 1

4

引用参考手册

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

还要注意下一节中的 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;
}

GETSFTP 命令的情况下, expressionctor arg 可能与上面一样 - 只是Message.getPayload()对所有传入消息的引用。

在这种情况下,您应该发送给sftpChannel喜欢的人Message

new GenericMessage<>("/home/samadmin/test.endf");

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

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

于 2016-09-02T13:41:47.377 回答