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ù)開(kāi)頭不是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)文章
Java8中關(guān)于Function.identity()的使用
這篇文章主要介紹了Java8中關(guān)于Function.identity()的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌)
JWT全稱為JSON Web Token,是一種用于身份驗(yàn)證的開(kāi)放標(biāo)準(zhǔn),本文主要介紹了SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Runtime.getRuntime().exec 路徑包含空格的解決
這篇文章主要介紹了Runtime.getRuntime().exec 路徑包含空格的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實(shí)現(xiàn)
這篇文章主要介紹了SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
基于Java SSM框架開(kāi)發(fā)圖書(shū)借閱系統(tǒng)源代碼
本文給大家介紹了基于Java SSM框架開(kāi)發(fā)圖書(shū)借閱系統(tǒng),開(kāi)發(fā)環(huán)境基于idea2020+mysql數(shù)據(jù)庫(kù),前端框架使用bootstrap4框架,完美了實(shí)現(xiàn)圖書(shū)借閱系統(tǒng),喜歡的朋友快來(lái)體驗(yàn)吧2021-05-05
SpringBoot Jpa 自定義查詢實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了SpringBoot Jpa 自定義查詢實(shí)現(xiàn)代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java使用ant.jar執(zhí)行SQL腳本文件的示例代碼
這篇文章主要介紹了Java使用ant.jar執(zhí)行SQL腳本文件,文中通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02

