av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:72日期:2022-08-16 08:17:38
前言

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,不過隨著 Java 版本的飛速升級(jí),其使用方式一直沒有變化,許多程序庫(kù)提供了更加簡(jiǎn)單的實(shí)現(xiàn),例如 Guava EventBus、RxJava、EventBus 等

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

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

相比 Observer 編程簡(jiǎn)單方便 通過自定義參數(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 ->1EventListener#listenInteger ->2EventListener#listenString ->3

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

為什么說這么做是同步的呢?

Guava Event 實(shí)際上是使用線程池來(lái)處理訂閱消息的,通過源碼可以看出,當(dāng)我們使用默認(rèn)的構(gòu)造方法創(chuàng)建 EventBus 的時(shí)候,其中 executor 為 MoreExecutors.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. 異步使用

通過上面的源碼,可以看出只要將構(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)聽和處理,通過實(shí)現(xiàn) SubscriberExceptionHandler 來(lái)處理異常的情況,無(wú)論時(shí)同步還是異步都能游刃有余

參考

https://github.com/google/guavahttps://github.com/greenrobot/EventBushttps://github.com/ReactiveX/RxJava

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

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 欧美精品福利 | wwwxxx欧美 | 国产成人在线免费观看 | 亚洲精品乱码久久久久久 | 欧美一区二区三区的 | 国产丝袜视频 | 亚欧av在线 | 国产伦精品一区二区三区视频网站 | 国产福利网站 | 久久久精品一区二区三区 | 亚洲免费精品 | 日韩精品免费视频 | 国产呦小j女精品视频 | 久久久久久国产精品 | 91av在线看 | 亚洲小说欧美激情另类 | 国产盗摄一区二区 | av片在线免费观看 | 精品国产乱码久久久久 | 日韩精品三区 | 亚洲精品少妇 | 中文字幕在线观看日韩 | www.黄色av| 放几个免费的毛片出来看 | 国产欧美精品一区 | www.久久.com| 国产日本在线 | 亚洲精品一二三四 | h片在线观看免费 | 国产一级生活片 | av免费不卡 | 国产精品高潮呻吟 | 一区二区不卡 | 高清视频一区二区 | 韩国免费理论片 | 亚洲欧美日韩一区二区三区四区 | av手机天堂| 波多野结衣一区二区三区在线观看 | 午夜影院在线观看视频 | 在线a| 亚洲天堂网在线观看 |