0
votes

In the Spring Integration documentation example for ftp outbound gateway with Java configuration (16.8.1), how do I log the payload of the reply channel to the console?

1

1 Answers

1
votes

Add a WireTap @Bean and wire its MessageChannel to a LoggingHandler.

Add the wire tap as a ChannelInterceptor to the gateway output channel.

Or, use the .wiretap() when using the Java DSL.

Documentation here.

EDIT

Java Config:

@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]