spring boot udp或者tcp接收數(shù)據(jù)的實例詳解
下面用的是 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>
下面是一個類 用來接收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);//實例化一個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直接過濾掉
}
@Router(inputChannel="udpFilter")
public String routing(String message) {
if(message.contains("1")) {//當(dāng)接收數(shù)據(jù)包含數(shù)字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()));
}
}
到此這篇關(guān)于spring boot udp或者tcp接收數(shù)據(jù)的實例詳解的文章就介紹到這了,更多相關(guān)spring boot接收數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java8中關(guān)于Function.identity()的使用
這篇文章主要介紹了Java8中關(guān)于Function.identity()的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
Runtime.getRuntime().exec 路徑包含空格的解決
這篇文章主要介紹了Runtime.getRuntime().exec 路徑包含空格的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實現(xiàn)
這篇文章主要介紹了SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
基于Java SSM框架開發(fā)圖書借閱系統(tǒng)源代碼
本文給大家介紹了基于Java SSM框架開發(fā)圖書借閱系統(tǒng),開發(fā)環(huán)境基于idea2020+mysql數(shù)據(jù)庫,前端框架使用bootstrap4框架,完美了實現(xiàn)圖書借閱系統(tǒng),喜歡的朋友快來體驗吧2021-05-05
SpringBoot Jpa 自定義查詢實現(xiàn)代碼詳解
這篇文章主要介紹了SpringBoot Jpa 自定義查詢實現(xiàn)代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
Java使用ant.jar執(zhí)行SQL腳本文件的示例代碼
這篇文章主要介紹了Java使用ant.jar執(zhí)行SQL腳本文件,文中通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02

