【问题标题】:Spring Integration server with Java DSL带有 Java DSL 的 Spring 集成服务器
【发布时间】:2018-05-18 13:49:07
【问题描述】:

我正在寻找一个使用 Java DSL 而不是 XML 响应消息的 Spring Integration 4.3.14 TCP 服务器的示例。

4.3.14 要求由公司政策设置,该政策也避免使用 XML。

最终要求是从 PLC 接收格式化的文本有效负载并以同样的方式响应。 PLC 代码是遗留的,根本没有很好的定义,并且模拟的有效载荷可以有不同的格式。

处理输入负载的简单方法是将其视为字符串并在 Java 代码中处理。

我有一个基本的接收工作,但不知道如何发送回复,阅读了很多示例等,但现在认为头脑只是糊涂了,所以一个简单的工作示例是理想的。

非常感谢

【问题讨论】:

    标签: spring-boot spring-integration tcp-ip


    【解决方案1】:

    给你...

    @SpringBootApplication
    public class So50412811Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So50412811Application.class, args).close();
        }
    
        @Bean
        public TcpNetServerConnectionFactory cf() {
            return new TcpNetServerConnectionFactory(1234);
        }
    
        @Bean
        public TcpInboundGateway gateway() {
            TcpInboundGateway gw = new TcpInboundGateway();
            gw.setConnectionFactory(cf());
            return gw;
        }
    
        @Bean
        public IntegrationFlow flow() {
            return IntegrationFlows.from(gateway())
                    .transform(Transformers.objectToString())
                    .<String, String>transform(String::toUpperCase)
                    .get();
        }
    
        // client
    
        @Bean
        public ApplicationRunner runner() {
            return args -> {
                Socket socket = SocketFactory.getDefault().createSocket("localhost", 1234);
                socket.getOutputStream().write("foo\r\n".getBytes()); // default CRLF deserializer
                InputStream is = socket.getInputStream();
                int in = 0;
                while (in != 0x0a) {
                    in = is.read();
                    System.out.print((char) in);
                }
                socket.close();
            };
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2015-02-03
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      相关资源
      最近更新 更多