springbootudp或者tcp接收数据的实例详解
短信预约 -IT技能 免费直播动态提醒
下面用的是 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()));
}
}
到此这篇关于spring boot udp或者tcp接收数据的实例详解的文章就介绍到这了,更多相关spring boot接收数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341