您好,登錄后才能下訂單哦!
本篇內容主要講解“nacos config的EventDispatcher是什么 ”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“nacos config的EventDispatcher是什么 ”吧!
本文主要研究一下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 CopyOnWriteArrayList<Entry> LISTENER_HUB = new CopyOnWriteArrayList<Entry>(); public interface Event { } //...... }
EventDispatcher定義了addEventListener、fireEvent、clear方法;addEventListener會添加listener到Entry;fireEvent會遍歷指定event的listener然后回調其onEvent方法;clear會清空整個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 CopyOnWriteArrayList<AbstractEventListener> listeners; Entry(Class<? extends Event> type) { eventType = type; listeners = new CopyOnWriteArrayList<AbstractEventListener>(); } @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有個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<Class<? extends Event>> interest(); /** * 處理事件 * * @param event event */ abstract public void onEvent(Event event); }
AbstractEventListener定義了interest、onEvent方法
EventDispatcher定義了addEventListener、fireEvent、clear方法;addEventListener會添加listener到Entry;fireEvent會遍歷指定event的listener然后回調其onEvent方法;clear會清空整個LISTENER_HUB
到此,相信大家對“nacos config的EventDispatcher是什么 ”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。