如何使用 Java 配置配置 SFTP 出站网关?
How to configure SFTP Outbound Gateway using Java Config?
我想使用 SFTP 出站网关通过 SFTP get
文件,但我只找到使用 XML 配置的示例。如何使用 Java 配置完成此操作?
更新(感谢 Artem Bilan 的帮助)
我的配置class:
@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;
}
}
我的申请class:
@SpringBootApplication
@EnableIntegration
public class TestIntegrationApplication {
public static void main(String[] args) {
SpringApplication.run(TestIntegrationApplication.class, args);
}
}
现在配置成功,但没有发生SFTP。需要弄清楚如何请求 SFTP。
引用 Reference Manual:
@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;
}
在 GET
SFTP 命令的情况下,expression
ctor arg 可能与上面类似 - 只是对所有传入消息的 Message.getPayload()
的引用。
在这种情况下,您应该向 sftpChannel
发送一个 Message
,例如:
new GenericMessage<>("/home/samadmin/test.endf");
因此,/home/samadmin/test.endf
是 Message
的 payload
。当它到达 SftpOutboundGateway
时,将根据该消息评估该表达式,并由 SpEL 调用 getPayload()
。因此,GET
命令将使用所需的远程文件路径执行。
另一封邮件可能具有与其他文件完全不同的路径。
我想使用 SFTP 出站网关通过 SFTP get
文件,但我只找到使用 XML 配置的示例。如何使用 Java 配置完成此操作?
更新(感谢 Artem Bilan 的帮助)
我的配置class:
@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;
}
}
我的申请class:
@SpringBootApplication
@EnableIntegration
public class TestIntegrationApplication {
public static void main(String[] args) {
SpringApplication.run(TestIntegrationApplication.class, args);
}
}
现在配置成功,但没有发生SFTP。需要弄清楚如何请求 SFTP。
引用 Reference Manual:
@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;
}
在 GET
SFTP 命令的情况下,expression
ctor arg 可能与上面类似 - 只是对所有传入消息的 Message.getPayload()
的引用。
在这种情况下,您应该向 sftpChannel
发送一个 Message
,例如:
new GenericMessage<>("/home/samadmin/test.endf");
因此,/home/samadmin/test.endf
是 Message
的 payload
。当它到达 SftpOutboundGateway
时,将根据该消息评估该表达式,并由 SpEL 调用 getPayload()
。因此,GET
命令将使用所需的远程文件路径执行。
另一封邮件可能具有与其他文件完全不同的路径。