欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot udp或者tcp接收數(shù)據(jù)的實(shí)例詳解

 更新時(shí)間:2021年12月01日 09:49:04   作者:JakeWin  
這篇文章主要介紹了spring boot udp或者tcp接收數(shù)據(jù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

下面用的是 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è)類 用來接收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直接過濾掉
    }

   @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)文章

  • Java?try?catch語句異常處理詳解

    Java?try?catch語句異常處理詳解

    這篇文章主要給大家介紹了關(guān)于Java?try?catch語句異常處理的相關(guān)資料,Java中的try-catch用于捕獲和處理異常,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • Java8中關(guān)于Function.identity()的使用

    Java8中關(guān)于Function.identity()的使用

    這篇文章主要介紹了Java8中關(guān)于Function.identity()的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • java8中:: 用法示例(JDK8雙冒號(hào)用法)

    java8中:: 用法示例(JDK8雙冒號(hào)用法)

    這篇文章主要給大家介紹了關(guān)于java8 中的:: 用法(JDK8雙冒號(hào)用法)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌)

    SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌)

    JWT全稱為JSON Web Token,是一種用于身份驗(yàn)證的開放標(biāo)準(zhǔn),本文主要介紹了SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • Runtime.getRuntime().exec 路徑包含空格的解決

    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)

    這篇文章主要介紹了SpringSecurity構(gòu)建基于JWT的登錄認(rèn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 基于Java SSM框架開發(fā)圖書借閱系統(tǒng)源代碼

    基于Java SSM框架開發(fā)圖書借閱系統(tǒng)源代碼

    本文給大家介紹了基于Java SSM框架開發(fā)圖書借閱系統(tǒng),開發(fā)環(huán)境基于idea2020+mysql數(shù)據(jù)庫,前端框架使用bootstrap4框架,完美了實(shí)現(xiàn)圖書借閱系統(tǒng),喜歡的朋友快來體驗(yàn)吧
    2021-05-05
  • Spring中的@Cacheable緩存注解詳解

    Spring中的@Cacheable緩存注解詳解

    這篇文章主要介紹了Spring中的@Cacheable緩存注解詳解,數(shù)據(jù)庫查找的流程是先要從磁盤拿到數(shù)據(jù),再刷新到內(nèi)存,再返回?cái)?shù)據(jù)。磁盤相比于內(nèi)存來說,速度是很慢的,為了提升性能,就出現(xiàn)了基于內(nèi)存的緩存,需要的朋友可以參考下
    2023-05-05
  • SpringBoot Jpa 自定義查詢實(shí)現(xiàn)代碼詳解

    SpringBoot Jpa 自定義查詢實(shí)現(xiàn)代碼詳解

    這篇文章主要介紹了SpringBoot Jpa 自定義查詢實(shí)現(xiàn)代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java使用ant.jar執(zhí)行SQL腳本文件的示例代碼

    Java使用ant.jar執(zhí)行SQL腳本文件的示例代碼

    這篇文章主要介紹了Java使用ant.jar執(zhí)行SQL腳本文件,文中通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-02-02

最新評(píng)論