How to implement simple echo socket service in Spring Integration DSL

后端 未结 2 1666
忘掉有多难
忘掉有多难 2020-12-22 11:19

Please,
could you help with implementation of a simple, echo style, Heartbeat TCP socket service in Spring Integration DSL? More precisely how to plug Adapter/Handler/Ga

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 11:50

    It's much simpler with the DSL...

    @SpringBootApplication
    @EnableScheduling
    public class So55154418Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So55154418Application.class, args);
        }
    
        @Bean
        public IntegrationFlow server() {
            return IntegrationFlows.from(Tcp.inboundGateway(Tcp.netServer(1234)))
                    .transform(Transformers.objectToString())
                    .log()
                    .handle((p, h) -> "OK")
                    .get();
        }
    
        @Bean
        public IntegrationFlow client() {
            return IntegrationFlows.from(Gate.class)
                    .handle(Tcp.outboundGateway(Tcp.netClient("localhost", 1234)))
                    .transform(Transformers.objectToString())
                    .handle((p, h) -> {
                        System.out.println("Received:" + p);
                        return null;
                    })
                    .get();
        }
    
        @Bean
        @DependsOn("client")
        public Runner runner(Gate gateway) {
            return new Runner(gateway);
        }
    
        public static class Runner {
    
            private final Gate gateway;
    
            public Runner(Gate gateway) {
                this.gateway = gateway;
            }
    
            @Scheduled(fixedDelay = 5000)
            public void run() {
                this.gateway.send("foo");
            }
    
        }
    
        public interface Gate {
    
            void send(String out);
    
        }
    
    }
    

    Or, get the reply from the Gate method...

        @Bean
        public IntegrationFlow client() {
            return IntegrationFlows.from(Gate.class)
                    .handle(Tcp.outboundGateway(Tcp.netClient("localhost", 1234)))
                    .transform(Transformers.objectToString())
                    .get();
        }
    
        @Bean
        @DependsOn("client")
        public Runner runner(Gate gateway) {
            return new Runner(gateway);
        }
    
        public static class Runner {
    
            private final Gate gateway;
    
            public Runner(Gate gateway) {
                this.gateway = gateway;
            }
    
            @Scheduled(fixedDelay = 5000)
            public void run() {
                String reply = this.gateway.sendAndReceive("foo"); // null for timeout
                System.out.println("Received:" + reply);
            }
    
        }
    
        public interface Gate {
    
            @Gateway(replyTimeout = 5000)
            String sendAndReceive(String out);
    
        }
    

    Bonus:

    Consuming endpoints are actually comprised of 2 beans; a consumer and a message handler. The channel goes on the consumer. See here.

    EDIT

    An alternative, for a single bean for the client...

    @Bean
    public IntegrationFlow client() {
        return IntegrationFlows.from(() -> "foo", 
                        e -> e.poller(Pollers.fixedDelay(Duration.ofSeconds(5))))
                .handle(Tcp.outboundGateway(Tcp.netClient("localhost", 1234)))
                .transform(Transformers.objectToString())
                .handle((p, h) -> {
                    System.out.println("Received:" + p);
                    return null;
                })
                .get();
    }
    

提交回复
热议问题