spring boot udp或者tcp接收数据的实例详解

网友投稿 343 2022-11-18

spring boot udp或者tcp接收数据的实例详解

下面用的是 springboot内置integration依赖

org.springframework.boot

spring-boot-starter-integration

org.springframework.boot

spring-boot-starter-logging

org.springframework.integration

spring-integration-ip

下面是一个类 用来接收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());//把emqfAlmGt接收的数据转化为字符串

}

@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="udpRoutemqfAlmGte1")

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()));

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:串口通信属性及事件解析
下一篇:PyTorch基础(part4)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~