【发布时间】:2018-03-15 19:50:12
【问题描述】:
在带有 Java 配置 (16.8.1) 的 ftp 出站网关的 Spring Integration 文档示例中,如何将回复通道的有效负载记录到控制台?
【问题讨论】:
在带有 Java 配置 (16.8.1) 的 ftp 出站网关的 Spring Integration 文档示例中,如何将回复通道的有效负载记录到控制台?
【问题讨论】:
添加WireTap @Bean 并将其MessageChannel 连接到LoggingHandler。
将分接头作为ChannelInterceptor 添加到网关输出通道。
或者,在使用 Java DSL 时使用 .wiretap()。
编辑
Java 配置:
@SpringBootApplication
public class So49308064Application {
public static void main(String[] args) {
SpringApplication.run(So49308064Application.class, args);
}
@Bean
public ApplicationRunner runner (Gate gate) {
return args -> {
List<String> list = gate.list("foo");
System.out.println("Result:" + list);
};
}
@ServiceActivator(inputChannel = "ftpLS")
@Bean
public FtpOutboundGateway getGW() {
FtpOutboundGateway gateway = new FtpOutboundGateway(sf(), "ls", "payload");
gateway.setOption(Option.NAME_ONLY);
gateway.setOutputChannelName("results");
return gateway;
}
@Bean
public MessageChannel results() {
DirectChannel channel = new DirectChannel();
channel.addInterceptor(tap());
return channel;
}
@Bean
public WireTap tap() {
return new WireTap("logging");
}
@ServiceActivator(inputChannel = "logging")
@Bean
public LoggingHandler logger() {
LoggingHandler logger = new LoggingHandler(Level.INFO);
logger.setLogExpressionString("'Files:' + payload");
return logger;
}
@Bean
public DefaultFtpSessionFactory sf() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("...");
sf.setUsername("...");
sf.setPassword("...");
return sf;
}
@MessagingGateway(defaultRequestChannel = "ftpLS", defaultReplyChannel = "results")
public interface Gate {
List<String> list(String directory);
}
}
.
2018-03-29 09:04:20.383 INFO 15158 --- [ main] o.s.integration.handler.LoggingHandler
: Files:bar.tx,bar.txt,baz.txt
Result:[bar.tx, bar.txt, baz.txt]
Java DSL:
@SpringBootApplication
public class So49308064Application {
public static void main(String[] args) {
SpringApplication.run(So49308064Application.class, args);
}
@Bean
public ApplicationRunner runner (Gate gate) {
return args -> {
List<String> list = gate.list("foo");
System.out.println("Result:" + list);
};
}
@Bean
public IntegrationFlow flow() {
return f -> f
.handle((Ftp.outboundGateway(sf(), "ls", "payload").options(Option.NAME_ONLY)))
.log(Level.INFO, "lsResult", "payload")
.bridge(); // needed, otherwise log ends the flow.
}
@Bean
public DefaultFtpSessionFactory sf() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("...");
sf.setUsername("...");
sf.setPassword("...");
return sf;
}
@MessagingGateway(defaultRequestChannel = "flow.input")
public interface Gate {
List<String> list(String directory);
}
}
.
2018-03-29 09:12:28.991 INFO 16638 --- [ main] lsResult
: [bar.tx, bar.txt, baz.txt]
Result:[bar.tx, bar.txt, baz.txt]
【讨论】:
ls 中看到了什么?这就是这段代码所做的一切。也许尝试在服务器上打开调试日志记录。
ftpserver.user.anonymous.homedirectory=./res/home 在res\home 中有一个 README.txt 文件,它是唯一的文件。所以列表(ls)是正确的。我正在检查启动服务器的 Apache FTP 服务器安装文件夹。在这个文件夹中,有一个 README.txt 和其他文件,我认为它们没有被列出。