spring boot udp或者tcp接收數(shù)據(jù)的實(shí)例詳解
下面用的是 springboot內(nèi)置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>
下面是一個(gè)類 用來(lái)接收udp協(xié)議和tcp協(xié)議的數(shù)據(jù)
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);//實(shí)例化一個(gè)udp 4567端口
adapter.setOutputChannelName("udp");
return adapter;
}
@Transformer(inputChannel="udp",outputChannel="udpString")
public String transformer(Message<?> message) {
return new String((byte[])message.getPayload());//把接收的數(shù)據(jù)轉(zhuǎn)化為字符串
}
@Filter(inputChannel="udpString",outputChannel="udpFilter")
public boolean filter(String message) {
return message.startsWith("abc");//如果接收數(shù)據(jù)開頭不是abc直接過(guò)濾掉
}
@Router(inputChannel="udpFilter")
public String routing(String message) {
if(message.contains("1")) {//當(dāng)接收數(shù)據(jù)包含數(shù)字1時(shí)
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()));
}
}
到此這篇關(guān)于spring boot udp或者tcp接收數(shù)據(jù)的實(shí)例詳解的文章就介紹到這了,更多相關(guān)spring boot接收數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2.x漏洞將logback1.2.x 升級(jí)至1.3.x
安全部門在代碼漏洞掃描中發(fā)現(xiàn)logback 1.2.x版本存在CVE漏洞,建議升級(jí)至1.3.x版本,本文就來(lái)介紹了logback1.2.x 升級(jí)至1.3.x,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
阿里云部署SpringBoot項(xiàng)目啟動(dòng)后被殺進(jìn)程的問(wèn)題解析
這篇文章主要介紹了阿里云部署SpringBoot項(xiàng)目啟動(dòng)后被殺進(jìn)程的問(wèn)題,本文給大家分享問(wèn)題原因所在及解決步驟,需要的朋友可以參考下2023-09-09
Spring Validation方法實(shí)現(xiàn)原理分析
這篇文章主要介紹了Spring Validation實(shí)現(xiàn)原理分析,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Spring ApplicationListener監(jiān)聽器用法詳解
這篇文章主要介紹了Spring ApplicationListener監(jiān)聽器用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Intellij?IDEA?中調(diào)試?maven?插件的步驟
這篇文章主要介紹了Intellij?IDEA?中調(diào)試?maven?插件,本文分步驟給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Java基于Rest?Assured自動(dòng)化測(cè)試接口詳解
Rest Assured 是一個(gè)基于 Java 的流行的用于測(cè)試 RESTful API 的庫(kù)。這篇文章主要介紹了Java如何基于Rest?Assured實(shí)現(xiàn)自動(dòng)化測(cè)試接口,需要的可以參考一下2023-03-03
SpringBoot中自定義注解實(shí)現(xiàn)控制器訪問(wèn)次數(shù)限制實(shí)例
本篇文章主要介紹了SpringBoot中自定義注解實(shí)現(xiàn)控制器訪問(wèn)次數(shù)限制實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
使用concurrentHashMap如何實(shí)現(xiàn)緩存
文章介紹了使用ConcurrentHashMap實(shí)現(xiàn)緩存的線程安全性和初始化方法,以及如何處理高并發(fā)場(chǎng)景下的緩存清理和數(shù)據(jù)一致性問(wèn)題,包括分桶、使用ConcurrentLinkedQueue以及使用CountDownLatch來(lái)確保緩存數(shù)據(jù)的不丟失2025-02-02

