栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Spring Boot中创建Tcp Connection以接受连接?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Spring Boot中创建Tcp Connection以接受连接?

有关使用XML配置的一些指针,请参见tcp-client-server示例。

用于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;@SpringBootApplicationpublic 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("foorn".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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/635554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号