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

JAVA | Guava EventBus 使用 發(fā)布/訂閱模式的步驟

 更新時(shí)間:2021年03月02日 10:25:08   作者:雙鬼帶單  
這篇文章主要介紹了JAVA | Guava EventBus 使用 發(fā)布/訂閱模式的步驟,幫助大家更好的理解和學(xué)習(xí)使用Guava EventBus,感興趣的朋友可以了解下

前言

EventBus 是 Guava 的事件處理機(jī)制,是觀察者模式(生產(chǎn)/消費(fèi)模型)的一種實(shí)現(xiàn)。

觀察者模式在我們?nèi)粘i_發(fā)中使用非常廣泛,例如在訂單系統(tǒng)中,訂單狀態(tài)或者物流信息的變更會(huì)向用戶發(fā)送APP推送、短信、通知賣家、買家等等;審批系統(tǒng)中,審批單的流程流轉(zhuǎn)會(huì)通知發(fā)起審批用戶、審批的領(lǐng)導(dǎo)等等。

Observer模式也是 JDK 中自帶就支持的,其在 1.0 版本就已經(jīng)存在 Observer,不過(guò)隨著 Java 版本的飛速升級(jí),其使用方式一直沒(méi)有變化,許多程序庫(kù)提供了更加簡(jiǎn)單的實(shí)現(xiàn),例如 Guava EventBus、RxJava、EventBus 等

一、為什么要用 Observer模式以及 EventBus 優(yōu)點(diǎn) ?

EventBus 優(yōu)點(diǎn)

  • 相比 Observer 編程簡(jiǎn)單方便
  • 通過(guò)自定義參數(shù)可實(shí)現(xiàn)同步、異步操作以及異常處理
  • 單進(jìn)程使用,無(wú)網(wǎng)絡(luò)影響

缺點(diǎn)

  • 只能單進(jìn)程使用
  • 項(xiàng)目異常重啟或者退出不保證消息持久化

如果需要分布式使用還是需要使用 MQ

二、EventBus 使用步驟

1. 引入庫(kù)

Gradle

compile group: 'com.google.guava', name: 'guava', version: '29.0-jre'

Maven

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>29.0-jre</version>
</dependency>

引入依賴后,這里我們主要使用 com.google.common.eventbus.EventBus 類進(jìn)行操作,其提供了 register、unregister、post 來(lái)進(jìn)行注冊(cè)訂閱、取消訂閱和發(fā)布消息

public void register(Object object);

public void unregister(Object object);

public void post(Object event);

2. 同步使用

1. 首先創(chuàng)建一個(gè) EventBus

EventBus eventBus = new EventBus();

2. 創(chuàng)建一個(gè)訂閱者

在 Guava EventBus 中,是根據(jù)參數(shù)類型進(jìn)行訂閱,每個(gè)訂閱的方法只能由一個(gè)參數(shù),同時(shí)需要使用 @Subscribe 標(biāo)識(shí)

class EventListener {

 /**
  * 監(jiān)聽 Integer 類型的消息
  */
 @Subscribe
 public void listenInteger(Integer param) {
  System.out.println("EventListener#listenInteger ->" + param);
 }

 /**
  * 監(jiān)聽 String 類型的消息
  */
 @Subscribe
 public void listenString(String param) {
  System.out.println("EventListener#listenString ->" + param);
 }
}

3. 注冊(cè)到 EventBus 上并發(fā)布消息

EventBus eventBus = new EventBus();

eventBus.register(new EventListener());

eventBus.post(1);
eventBus.post(2);
eventBus.post("3");

運(yùn)行結(jié)果為

EventListener#listenInteger ->1
EventListener#listenInteger ->2
EventListener#listenString ->3

根據(jù)需要我們可以創(chuàng)建多個(gè)訂閱者完成訂閱信息,同時(shí)如果一個(gè)類型存在多個(gè)訂閱者,則所有訂閱方法都會(huì)執(zhí)行

為什么說(shuō)這么做是同步的呢?

Guava Event 實(shí)際上是使用線程池來(lái)處理訂閱消息的,通過(guò)源碼可以看出,當(dāng)我們使用默認(rèn)的構(gòu)造方法創(chuàng)建 EventBus 的時(shí)候,其中 executorMoreExecutors.directExecutor(),其具體實(shí)現(xiàn)中直接調(diào)用的 Runnable#run 方法,使其仍然在同一個(gè)線程中執(zhí)行,所以默認(rèn)操作仍然是同步的,這種處理方法也有適用的地方,這樣既可以解耦也可以讓方法在同一個(gè)線程中執(zhí)行獲取同線程中的便利,比如事務(wù)的處理

EventBus 部分源碼

public class EventBus {
 private static final Logger logger = Logger.getLogger(EventBus.class.getName());
 private final String identifier;
 private final Executor executor;
 private final SubscriberExceptionHandler exceptionHandler;
 private final SubscriberRegistry subscribers;
 private final Dispatcher dispatcher;

 public EventBus() {
  this("default");
 }

 public EventBus(String identifier) {
  this(identifier, MoreExecutors.directExecutor(), Dispatcher.perThreadDispatchQueue(), EventBus.LoggingHandler.INSTANCE);
 }

 public EventBus(SubscriberExceptionHandler exceptionHandler) {
  this("default", MoreExecutors.directExecutor(), Dispatcher.perThreadDispatchQueue(), exceptionHandler);
 }

 EventBus(String identifier, Executor executor, Dispatcher dispatcher, SubscriberExceptionHandler exceptionHandler) {
  this.subscribers = new SubscriberRegistry(this);
  this.identifier = (String)Preconditions.checkNotNull(identifier);
  this.executor = (Executor)Preconditions.checkNotNull(executor);
  this.dispatcher = (Dispatcher)Preconditions.checkNotNull(dispatcher);
  this.exceptionHandler = (SubscriberExceptionHandler)Preconditions.checkNotNull(exceptionHandler);
 }
}

DirectExecutor 部分源碼

enum DirectExecutor implements Executor {
 INSTANCE;

 private DirectExecutor() {
 }

 public void execute(Runnable command) {
  command.run();
 }

 public String toString() {
  return "MoreExecutors.directExecutor()";
 }
}

3. 異步使用

通過(guò)上面的源碼,可以看出只要將構(gòu)造方法中的 executor 換成一個(gè)線程池實(shí)現(xiàn)即可, 同時(shí) Guava EventBus 為了簡(jiǎn)化操作,提供了一個(gè)簡(jiǎn)化的方案即 AsyncEventBus

EventBus eventBus = new AsyncEventBus(Executors.newCachedThreadPool());

這樣即可實(shí)現(xiàn)異步使用

AsyncEventBus 源碼

public class AsyncEventBus extends EventBus {
 public AsyncEventBus(String identifier, Executor executor) {
  super(identifier, executor, Dispatcher.legacyAsync(), LoggingHandler.INSTANCE);
 }

 public AsyncEventBus(Executor executor, SubscriberExceptionHandler subscriberExceptionHandler) {
  super("default", executor, Dispatcher.legacyAsync(), subscriberExceptionHandler);
 }

 public AsyncEventBus(Executor executor) {
  super("default", executor, Dispatcher.legacyAsync(), LoggingHandler.INSTANCE);
 }
}

4. 異常處理

如果處理時(shí)發(fā)生異常應(yīng)該如何處理? 在看源碼中,無(wú)論是 EventBus 還是 AsyncEventBus 都可傳入自定義的 SubscriberExceptionHandler 該 handler 當(dāng)出現(xiàn)異常時(shí)會(huì)被調(diào)用,我可可以從參數(shù) exception 獲取異常信息,從 context 中獲取消息信息進(jìn)行特定的處理

其接口聲明為

public interface SubscriberExceptionHandler {
 /** Handles exceptions thrown by subscribers. */
 void handleException(Throwable exception, SubscriberExceptionContext context);
}

總結(jié)

在上面的基礎(chǔ)上,我們可以定義一些消息類型來(lái)實(shí)現(xiàn)不同消息的監(jiān)聽和處理,通過(guò)實(shí)現(xiàn) SubscriberExceptionHandler 來(lái)處理異常的情況,無(wú)論時(shí)同步還是異步都能游刃有余

參考

https://github.com/google/guava
https://github.com/greenrobot/EventBus
https://github.com/ReactiveX/RxJava

以上就是JAVA | Guava EventBus 使用 發(fā)布/訂閱模式的步驟的詳細(xì)內(nèi)容,更多關(guān)于Guava EventBus 使用 發(fā)布/訂閱模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java多線程實(shí)現(xiàn)文件下載

    java多線程實(shí)現(xiàn)文件下載

    這篇文章主要為大家詳細(xì)介紹了java多線程實(shí)現(xiàn)文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • idea指定啟動(dòng)參數(shù)、環(huán)境變量的過(guò)程

    idea指定啟動(dòng)參數(shù)、環(huán)境變量的過(guò)程

    這篇文章主要介紹了idea指定啟動(dòng)參數(shù)、環(huán)境變量的操作過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Spring?Data?Exists查詢最佳方法編寫示例

    Spring?Data?Exists查詢最佳方法編寫示例

    這篇文章主要為大家介紹了Spring?Data?Exists查詢最佳方法編寫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Java使用正則表達(dá)式提取XML節(jié)點(diǎn)內(nèi)容的方法示例

    Java使用正則表達(dá)式提取XML節(jié)點(diǎn)內(nèi)容的方法示例

    這篇文章主要介紹了Java使用正則表達(dá)式提取XML節(jié)點(diǎn)內(nèi)容的方法,結(jié)合具體實(shí)例形式分析了java針對(duì)xml格式字符串的正則匹配相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Java?設(shè)計(jì)模式中的策略模式詳情

    Java?設(shè)計(jì)模式中的策略模式詳情

    這篇文章主要介紹了Java?設(shè)計(jì)模式中的策略模式詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • SpringBoot項(xiàng)目集成xxljob實(shí)現(xiàn)全紀(jì)錄

    SpringBoot項(xiàng)目集成xxljob實(shí)現(xiàn)全紀(jì)錄

    XXL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),本文主要介紹了SpringBoot項(xiàng)目集成xxljob實(shí)現(xiàn)全紀(jì)錄,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java?枚舉的常用技巧匯總

    Java?枚舉的常用技巧匯總

    在Java中,枚舉類型是一種特殊的數(shù)據(jù)類型,允許定義一組固定的常量,默認(rèn)情況下,toString方法返回枚舉常量的名稱,本文提供了一個(gè)完整的代碼示例,展示了如何在Java中通過(guò)重寫枚舉的toString方法來(lái)展示枚舉實(shí)例的字段信息,感興趣的朋友一起看看吧
    2025-01-01
  • Java String類簡(jiǎn)單用法實(shí)戰(zhàn)示例【字符串輸出、比較】

    Java String類簡(jiǎn)單用法實(shí)戰(zhàn)示例【字符串輸出、比較】

    這篇文章主要介紹了Java String類簡(jiǎn)單用法,結(jié)合具體實(shí)例形式分析了Java使用String類實(shí)現(xiàn)字符串的輸出和比較功能相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • 基于rabbitmq延遲插件實(shí)現(xiàn)分布式延遲任務(wù)

    基于rabbitmq延遲插件實(shí)現(xiàn)分布式延遲任務(wù)

    這篇文章主要介紹了基于rabbitmq延遲插件實(shí)現(xiàn)分布式延遲任務(wù),今天我們講解延遲隊(duì)列的實(shí)現(xiàn)方式,而延遲隊(duì)列有很多種實(shí)現(xiàn)方式,今天就每種實(shí)現(xiàn)方式給大家大概介紹下,感興趣的朋友一起看看吧
    2022-01-01
  • 如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目

    如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目

    這篇文章主要介紹了如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目,幫助大家更好的利用springboot進(jìn)行網(wǎng)絡(luò)編程,感興趣的朋友可以了解下
    2020-10-10

最新評(píng)論

大荔县| 大冶市| 视频| 三明市| 逊克县| 马尔康县| 汨罗市| 天长市| 玛曲县| 江口县| 大港区| 连云港市| 曲麻莱县| 蒙城县| 襄樊市| 广汉市| 平武县| 彭山县| 桂林市| 东方市| 上思县| 景德镇市| 土默特左旗| 凤山市| 宁波市| 石楼县| 深水埗区| 武汉市| 新民市| 金湖县| 改则县| 安岳县| 濮阳县| 嘉义市| 宿州市| 宝兴县| 舞阳县| 镇坪县| 丰城市| 吴川市| 平乡县|