spring boot udp或者tcp接收数据
2019-08-16 09:47:48来源:博客园 阅读 ()
spring boot udp或者tcp接收数据
下面用的是 springboot内置integration依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-ip</artifactId> </dependency>
下面是一个类 用来接收udp协议和tcp协议的数据
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.integration.annotation.Filter; import org.springframework.integration.annotation.Router; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.annotation.Transformer; import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter; import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory; import org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer; import org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter; import org.springframework.messaging.Message; @Configuration public class DataReceiveConfigration { @Bean public UnicastReceivingChannelAdapter getUnicastReceivingChannelAdapter() { UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(4567);//实例化一个udp 4567端口 adapter.setOutputChannelName("udp"); return adapter; } @Transformer(inputChannel="udp",outputChannel="udpString") public String transformer(Message<?> message) { return new String((byte[])message.getPayload());//把接收的数据转化为字符串 } @Filter(inputChannel="udpString",outputChannel="udpFilter") public boolean filter(String message) { return message.startsWith("abc");//如果接收数据开头不是abc直接过滤掉 } @Router(inputChannel="udpFilter") public String routing(String message) { if(message.contains("1")) {//当接收数据包含数字1时 return "udpRoute1"; } else { return "udpRoute2"; } } @ServiceActivator(inputChannel="udpRoute1") public void udpMessageHandle(String message) { System.out.println("udp1:" +message); } @ServiceActivator(inputChannel="udpRoute2") public void udpMessageHandle2(String message) { System.out.println("udp2:" +message); } @Bean public TcpNetServerConnectionFactory getServerConnectionFactory() { TcpNetServerConnectionFactory serverConnectionFactory = new TcpNetServerConnectionFactory(1234); serverConnectionFactory.setSerializer(new ByteArrayRawSerializer()); serverConnectionFactory.setDeserializer(new ByteArrayRawSerializer()); serverConnectionFactory.setLookupHost(false); return serverConnectionFactory; } @Bean public TcpReceivingChannelAdapter getReceivingChannelAdapter() { TcpReceivingChannelAdapter receivingChannelAdapter = new TcpReceivingChannelAdapter(); receivingChannelAdapter.setConnectionFactory(getServerConnectionFactory()); receivingChannelAdapter.setOutputChannelName("tcp"); return receivingChannelAdapter; } @ServiceActivator(inputChannel="tcp") public void messageHandle(Message<?> message) { System.out.println(new String((byte[])message.getPayload())); } }
原文链接:https://www.cnblogs.com/seakitl/p/11136978.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Spring系列.ApplicationContext接口 2020-06-11
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- 给你一份超详细 Spring Boot 知识清单 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 掌握SpringBoot-2.3的容器探针:实战篇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash