【问题标题】:How to create a Tcp Connection in spring boot to accept connections?如何在 Spring Boot 中创建 Tcp 连接以接受连接?
【发布时间】:2016-09-02 11:02:54
【问题描述】:

我已经通过this 了解我需要创建一个TcpReceivingChannelAdapter 来接受连接。 但我不知道该怎么做。

有人可以指导我吗?

【问题讨论】:

    标签: java spring-boot tcp spring-integration


    【解决方案1】:

    有关使用 XML 配置的一些指针,请参阅 tcp-client-server sample

    用于Java配置;这是一个简单的 Spring Boot 应用程序...

    package com.example;
    
    import java.net.Socket;
    
    import javax.net.SocketFactory;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.integration.annotation.ServiceActivator;
    import org.springframework.integration.annotation.Transformer;
    import org.springframework.integration.channel.DirectChannel;
    import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
    import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
    import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
    import org.springframework.integration.transformer.ObjectToStringTransformer;
    import org.springframework.messaging.MessageChannel;
    
    @SpringBootApplication
    public class So39290834Application {
    
        public static void main(String[] args) throws Exception {
            ConfigurableApplicationContext context = SpringApplication.run(So39290834Application.class, args);
            Socket socket = SocketFactory.getDefault().createSocket("localhost", 9999);
            socket.getOutputStream().write("foo\r\n".getBytes());
            socket.close();
            Thread.sleep(1000);
            context.close();
        }
    
        @Bean
        public TcpNetServerConnectionFactory cf() {
            return new TcpNetServerConnectionFactory(9999);
        }
    
        @Bean
        public TcpReceivingChannelAdapter inbound(AbstractServerConnectionFactory cf) {
            TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
            adapter.setConnectionFactory(cf);
            adapter.setOutputChannel(tcpIn());
            return adapter;
        }
    
        @Bean
        public MessageChannel tcpIn() {
            return new DirectChannel();
        }
    
        @Transformer(inputChannel = "tcpIn", outputChannel = "serviceChannel")
        @Bean
        public ObjectToStringTransformer transformer() {
            return new ObjectToStringTransformer();
        }
    
        @ServiceActivator(inputChannel = "serviceChannel")
        public void service(String in) {
            System.out.println(in);
        }
    
    }
    

    【讨论】:

    • 完美!你能告诉我在哪里可以找到关于 Spring boot 中基于 Java 的配置的文档吗?
    • 我不确定你的意思。如果您指的是 Spring Integration 组件在启动时的 java 配置,我们在参考手册中有一些示例,但还没有针对所有模块。我们确实有一个section 来指导您并指出您在哪里可以找到有关大多数元素的底层组件的信息。
    • gradle 依赖:编译 'org.springframework.integration:spring-integration-core:4.3.11.RELEASE' 编译 'org.springframework.integration:spring-integration-ip:4.3.11.RELEASE '
    • 您的问题不清楚。在任何情况下,您都不得在评论中提出新问题;打开一个更详细的新问题,准确解释您遇到的问题。
    • 你在说什么?哪个豆子?定义“将工作”。连接工厂允许客户端连接;其他 bean 是 Spring Integration 基础设施,最终是简单地打印字符串的服务。除非您要添加有意义的内容,否则不要评论旧答案。如果您有不明白的地方,请用适当的标签提出一个新问题,并准确地表明您的意思。
    猜你喜欢
    • 2011-01-09
    • 1970-01-01
    • 2021-06-25
    • 2020-04-05
    • 2020-05-11
    • 2023-03-26
    • 1970-01-01
    • 2016-12-24
    • 2018-09-22
    相关资源
    最近更新 更多