本篇內(nèi)容主要講解“nacos config的EventDispatcher是什么 ”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“nacos config的EventDispatcher是什么 ”吧!
上林網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),上林網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為上林千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的上林做網(wǎng)站的公司定做!
本文主要研究一下nacos config的EventDispatcher
nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/utils/event/EventDispatcher.java
public class EventDispatcher { /** * add event listener */ static public void addEventListener(AbstractEventListener listener) { for (Class extends Event> type : listener.interest()) { getEntry(type).listeners.addIfAbsent(listener); } } /** * fire event, notify listeners. */ static public void fireEvent(Event event) { if (null == event) { throw new IllegalArgumentException(); } for (AbstractEventListener listener : getEntry(event.getClass()).listeners) { try { listener.onEvent(event); } catch (Exception e) { log.error(e.toString(), e); } } } /** * For only test purpose */ static public void clear() { LISTENER_HUB.clear(); } /** * get event listener for eventType. Add Entry if not exist. */ static Entry getEntry(Class extends Event> eventType) { for (; ; ) { for (Entry entry : LISTENER_HUB) { if (entry.eventType == eventType) { return entry; } } Entry tmp = new Entry(eventType); /** * false means already exists */ if (LISTENER_HUB.addIfAbsent(tmp)) { return tmp; } } } //...... static private final Logger log = LoggerFactory.getLogger(EventDispatcher.class); static final CopyOnWriteArrayListLISTENER_HUB = new CopyOnWriteArrayList (); public interface Event { } //...... }
EventDispatcher定義了addEventListener、fireEvent、clear方法;addEventListener會(huì)添加listener到Entry;fireEvent會(huì)遍歷指定event的listener然后回調(diào)其onEvent方法;clear會(huì)清空整個(gè)LISTENER_HUB
nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/utils/event/EventDispatcher.java
static private class Entry { final Class extends Event> eventType; final CopyOnWriteArrayListlisteners; Entry(Class extends Event> type) { eventType = type; listeners = new CopyOnWriteArrayList (); } @Override public boolean equals(Object obj) { if (null == obj || obj.getClass() != getClass()) { return false; } if (this == obj) { return true; } return eventType == ((Entry)obj).eventType; } @Override public int hashCode() { return super.hashCode(); } }
Entry有個(gè)eventType屬性以及AbstractEventListener類型的listener集合
nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/utils/event/EventDispatcher.java
static public abstract class AbstractEventListener { public AbstractEventListener() { /** * automatic register */ EventDispatcher.addEventListener(this); } /** * 感興趣的事件列表 * * @return event list */ abstract public List> interest(); /** * 處理事件 * * @param event event */ abstract public void onEvent(Event event); }
AbstractEventListener定義了interest、onEvent方法
EventDispatcher定義了addEventListener、fireEvent、clear方法;addEventListener會(huì)添加listener到Entry;fireEvent會(huì)遍歷指定event的listener然后回調(diào)其onEvent方法;clear會(huì)清空整個(gè)LISTENER_HUB
到此,相信大家對(duì)“nacos config的EventDispatcher是什么 ”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!