最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringBoot實(shí)現(xiàn)事件同步與異步監(jiān)聽(tīng)

 更新時(shí)間:2022年06月22日 09:25:35   作者:IT利刃出鞘  
這篇文章主要通過(guò)示例為大家詳細(xì)介紹了SpringBoot中的事件的用法和原理以及如何實(shí)現(xiàn)事件同步與異步監(jiān)聽(tīng),快跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧

簡(jiǎn)介

說(shuō)明

本文用示例介紹SpringBoot中的事件的用法及原理。

事件監(jiān)聽(tīng)簡(jiǎn)述

事件的發(fā)布與監(jiān)聽(tīng)從屬于觀察者模式;和MQ相比,事件的發(fā)布與監(jiān)聽(tīng)偏向于處理服務(wù)內(nèi)的某些邏輯。 

多個(gè)監(jiān)聽(tīng)器可以監(jiān)聽(tīng)同一個(gè)事件。例如:發(fā)布了事件A,監(jiān)聽(tīng)器A和監(jiān)聽(tīng)器B都監(jiān)聽(tīng)了事件A,則監(jiān)聽(tīng)器A和B都會(huì)進(jìn)行處理。

同步與異步監(jiān)聽(tīng)

監(jiān)聽(tīng)方式特點(diǎn)使用時(shí)機(jī)
同步監(jiān)聽(tīng)發(fā)布器線程與監(jiān)聽(tīng)器線程處于同一線程1.監(jiān)聽(tīng)邏輯處理較快
2.需要緊接著根據(jù)監(jiān)聽(tīng)器追蹤業(yè)務(wù)線程
異步監(jiān)聽(tīng)發(fā)布器線程與監(jiān)聽(tīng)器線程處于不同線程1.監(jiān)聽(tīng)邏輯處理比較耗時(shí)
2.追求響應(yīng)性能

事件的順序

可使用實(shí)現(xiàn)Ordered接口的方式,調(diào)整監(jiān)聽(tīng)器順序。

注意:必須是同時(shí)實(shí)現(xiàn) ApplicationListener<MyEvent>,Ordered這樣的方法才能控制順序。

下邊幾種都是無(wú)法控制順序的:

  • @Component+@EventListerner+實(shí)現(xiàn)Ordered
  • 實(shí)現(xiàn) ApplicationListener<MyEvent>+@Order

實(shí)例

同步監(jiān)聽(tīng)(無(wú)序)

事件

package com.example.event;
 
import org.springframework.context.ApplicationEvent;
 
public class MyEvent extends ApplicationEvent {
 
    public MyEvent(Object source) {
        super(source);
    }
 
}

監(jiān)聽(tīng)器

監(jiān)聽(tīng)器1

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener {
    @EventListener
    public void abc(MyEvent event) {
        System.out.println("監(jiān)聽(tīng)器:        " + "MyListener");
        System.out.println("監(jiān)聽(tīng)器所在線程:" + Thread.currentThread().getName());
        System.out.println("事件:          " + event);
        System.out.println("事件的數(shù)據(jù):    " + event.getSource());
    }
}

監(jiān)聽(tīng)器2

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener2 {
 
    @EventListener
    public void onApplicationEvent(MyEvent event) {
        System.out.println("監(jiān)聽(tīng)器:      " + "MyListener2");
        System.out.println("  所在線程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的數(shù)據(jù):" + event.getSource());
    }
}

發(fā)布器

package com.example.event;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;
 
    public void myPublish(String message) {
        System.out.println("發(fā)布器所在線程:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new MyEvent(message));
    }
}

測(cè)試

寫(xiě)一個(gè)Controller

package com.example.controller;
 
import com.example.event.MyPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @Autowired
    MyPublisher myPublisher;
 
    @GetMapping("/test1")
    public String test1() {
        myPublisher.myPublish("Hello");
        return "test1 success";
    }
}

啟動(dòng)后,訪問(wèn):http://localhost:8080/test1

后端輸出:

發(fā)布器所在線程:http-nio-8080-exec-1
監(jiān)聽(tīng)器:      MyListener
  所在線程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello
監(jiān)聽(tīng)器:      MyListener2
  所在線程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello

可以發(fā)現(xiàn),所有監(jiān)聽(tīng)器和發(fā)布器都在同一個(gè)線程。 

同步監(jiān)聽(tīng)(有序)

事件

package com.example.event;
 
import org.springframework.context.ApplicationEvent;
 
public class MyEvent extends ApplicationEvent {
 
    public MyEvent(Object source) {
        super(source);
    }
 
}

監(jiān)聽(tīng)器

監(jiān)聽(tīng)器1

package com.example.event;
 
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener implements ApplicationListener<MyEvent>,  Ordered {
    @Override
    public void onApplicationEvent(MyEvent event) {
        System.out.println("監(jiān)聽(tīng)器:      " + "MyListener");
        System.out.println("  所在線程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的數(shù)據(jù):" + event.getSource());
    }
 
    @Override
    public int getOrder() {
        return 2;
    }
}

監(jiān)聽(tīng)器2

package com.example.event;
 
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener2 implements ApplicationListener<MyEvent>, Ordered {
 
    public void onApplicationEvent(MyEvent event) {
        System.out.println("監(jiān)聽(tīng)器:      " + "MyListener2");
        System.out.println("  所在線程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的數(shù)據(jù):" + event.getSource());
    }
 
    @Override
    public int getOrder() {
        return 1;
    }
}

發(fā)布器

package com.example.event;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;
 
    public void myPublish(String message) {
        System.out.println("發(fā)布器所在線程:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new MyEvent(message));
    }
}

測(cè)試

寫(xiě)一個(gè)Controller

package com.example.controller;
 
import com.example.event.MyPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @Autowired
    MyPublisher myPublisher;
 
    @GetMapping("/test1")
    public String test1() {
        myPublisher.myPublish("Hello");
        return "test1 success";
    }
}

啟動(dòng)后,訪問(wèn):http://localhost:8080/test1

后端輸出:

發(fā)布器所在線程:http-nio-8080-exec-1
監(jiān)聽(tīng)器:      MyListener2
  所在線程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello
監(jiān)聽(tīng)器:      MyListener
  所在線程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello

如果將監(jiān)聽(tīng)器的實(shí)現(xiàn)的Ordered順序顛倒,則輸出結(jié)果如下:

發(fā)布器所在線程:http-nio-8080-exec-1
監(jiān)聽(tīng)器:      MyListener
  所在線程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello
監(jiān)聽(tīng)器:      MyListener2
  所在線程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello

異步監(jiān)聽(tīng)(無(wú)序)

方法:

  • 開(kāi)啟異步監(jiān)聽(tīng)
  • 在監(jiān)聽(tīng)器上加@Async(此監(jiān)聽(tīng)器必須是@Component方法注冊(cè)的)

事件

package com.example.event;
 
import org.springframework.context.ApplicationEvent;
 
public class MyEvent extends ApplicationEvent {
 
    public MyEvent(Object source) {
        super(source);
    }
 
}

同步監(jiān)聽(tīng)器

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
@Component
public class MyListener {
    @EventListener
    public void abc(MyEvent event) {
        System.out.println("監(jiān)聽(tīng)器:      " + "MyListener");
        System.out.println("  所在線程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的數(shù)據(jù):" + event.getSource());
    }
}

異步監(jiān)聽(tīng)器

package com.example.event;
 
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
 
@Component
@Async
public class MyListener2 {
 
    @EventListener
    public void onApplicationEvent(MyEvent event) {
        System.out.println("監(jiān)聽(tīng)器:      " + "MyListener2");
        System.out.println("  所在線程:  " + Thread.currentThread().getName());
        System.out.println("  事件:      " + event);
        System.out.println("  事件的數(shù)據(jù):" + event.getSource());
    }
}

發(fā)布器

package com.example.event;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
@Component
public class MyPublisher {
    @Autowired
    ApplicationContext applicationContext;
 
    public void myPublish(String message) {
        System.out.println("發(fā)布器所在線程:" + Thread.currentThread().getName());
        applicationContext.publishEvent(new MyEvent(message));
    }
}

啟動(dòng)類

package com.example;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
 
@SpringBootApplication
@EnableAsync
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

測(cè)試

寫(xiě)一個(gè)Controller

package com.example.controller;
 
import com.example.event.MyPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    @Autowired
    MyPublisher myPublisher;
 
    @GetMapping("/test1")
    public String test1() {
        myPublisher.myPublish("Hello");
        return "test1 success";
    }
}

啟動(dòng)后,訪問(wèn):http://localhost:8080/test1

后端輸出:

發(fā)布器所在線程:http-nio-8080-exec-1
監(jiān)聽(tīng)器:      MyListener
  所在線程:  http-nio-8080-exec-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello
監(jiān)聽(tīng)器:      MyListener2
  所在線程:  task-1
  事件:      com.example.event.MyEvent[source=Hello]
  事件的數(shù)據(jù):Hello

到此這篇關(guān)于詳解SpringBoot實(shí)現(xiàn)同步與異步事件監(jiān)聽(tīng)的文章就介紹到這了,更多相關(guān)SpringBoot事件監(jiān)聽(tīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • jenkins配置詳細(xì)指南(附j(luò)dk多個(gè)版本配置)

    jenkins配置詳細(xì)指南(附j(luò)dk多個(gè)版本配置)

    Jenkins是一款CICD(持續(xù)集成與持續(xù)交付)工具,Jenkins可以幫你在寫(xiě)完代碼后,一鍵完成開(kāi)發(fā)過(guò)程中的一系列自動(dòng)化部署的工作,這篇文章主要給大家介紹了關(guān)于jenkins配置的相關(guān)資料,文中還附j(luò)dk多個(gè)版本配置指南,需要的朋友可以參考下
    2024-02-02
  • Java?批量生成條碼的示例代碼

    Java?批量生成條碼的示例代碼

    這篇文章主要介紹了Java?批量生成條碼的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能

    java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Spring-AOP 靜態(tài)普通方法名匹配切面操作

    Spring-AOP 靜態(tài)普通方法名匹配切面操作

    這篇文章主要介紹了Spring-AOP 靜態(tài)普通方法名匹配切面操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java根據(jù)本地IP獲取mac地址的方法

    java根據(jù)本地IP獲取mac地址的方法

    這篇文章主要為大家詳細(xì)介紹了java根據(jù)本地IP獲取mac地址的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • AsyncHttpClient?ClientStats源碼流程解讀

    AsyncHttpClient?ClientStats源碼流程解讀

    這篇文章主要為大家介紹了AsyncHttpClient?ClientStats源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java?Web項(xiàng)目中解決中文亂碼方法總結(jié)(三種最新方法)

    Java?Web項(xiàng)目中解決中文亂碼方法總結(jié)(三種最新方法)

    這篇文章主要介紹了Java?Web項(xiàng)目中解決中文亂碼方法總結(jié),本文給大家分享三種最新解決方法,需要的朋友可以參考下
    2022-06-06
  • 談?wù)劄镴AXB和response設(shè)置編碼,解決wechat4j中文亂碼的問(wèn)題

    談?wù)劄镴AXB和response設(shè)置編碼,解決wechat4j中文亂碼的問(wèn)題

    中文亂碼是每個(gè)程序員都會(huì)遇到的問(wèn)題,本篇文章主要介紹了談?wù)劄镴AXB和response設(shè)置編碼,解決wechat4j中文亂碼的問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2016-12-12
  • JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼

    JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼

    本文主要介紹了JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java+WebSocket實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)雙人協(xié)同pk答題系統(tǒng)

    Java+WebSocket實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)雙人協(xié)同pk答題系統(tǒng)

    在實(shí)時(shí)互動(dòng)應(yīng)用中,實(shí)現(xiàn)流暢的多人協(xié)同對(duì)戰(zhàn)功能是一大挑戰(zhàn),WebSocket技術(shù),以其全雙工通信能力,提供了解決方案,本文我們就來(lái)使用WebSocket實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)雙人協(xié)同pk答題系統(tǒng)吧
    2025-06-06

最新評(píng)論

双城市| 株洲市| 鹤岗市| 丰宁| 吕梁市| 无为县| 汉寿县| 百色市| 开平市| 盐亭县| 米林县| 镇安县| 泽普县| 平果县| 固镇县| 沾化县| 信丰县| 南陵县| 安平县| 澎湖县| 潼关县| 天祝| 普宁市| 邹平县| 大城县| 简阳市| 高青县| 饶河县| 河北区| 资中县| 太和县| 磐石市| 墨竹工卡县| 柳江县| 中宁县| 辽源市| 百色市| 林西县| 衡阳市| 古浪县| 泰来县|