【问题标题】:Spring Integration FTP Outbound Gateway console outputSpring Integration FTP Outbound Gateway 控制台输出
【发布时间】:2018-03-15 19:50:12
【问题描述】:

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

【问题讨论】:

    标签: spring spring-integration


    【解决方案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]
    

    【讨论】:

    • 加里:不知道怎么写这些。如果您提供代码 sn-ps 将不胜感激。
    • 太好了,这段代码对我有用。在您的输出中,我看到列出了多个文件。但是,我只得到一个文件名,尽管 ftp 服务器上有多个文件。我在独立模式下使用 Apache FtpServer,我的电脑是 Windows 8。
    • 你所描述的没有意义;你在命令行 FTP 客户端和ls 中看到了什么?这就是这段代码所做的一切。也许尝试在服务器上打开调试日志记录。
    • 对不起,我的错(运气和注意力)。 Apache FTP 服务器 users.properties 有ftpserver.user.anonymous.homedirectory=./res/homeres\home 中有一个 README.txt 文件,它是唯一的文件。所以列表(ls)是正确的。我正在检查启动服务器的 Apache FTP 服务器安装文件夹。在这个文件夹中,有一个 README.txt 和其他文件,我认为它们没有被列出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2015-08-10
    相关资源
    最近更新 更多