Spring Integration FTP出站网关控制台输出

时间:2018-03-15 19:50:12

标签: spring spring-integration

在使用Java配置(16.8.1)的ftp出站网关的Spring Integration文档示例中,如何将回复通道的有效负载记录到控制台?

1 个答案:

答案 0 :(得分:1)

添加WireTap @Bean并将其MessageChannel连接到LoggingHandler

将有线分接头添加为ChannelInterceptor到网关输出通道。

或者,在使用Java DSL时使用.wiretap()

Documentation here

修改

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]