nacosconfig的EventDispatcher是什么
本篇内容主要讲解“nacos config的EventDispatcher是什么 ”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“nacos config的EventDispatcher是什么 ”吧!
上林网站建设公司创新互联,上林网站设计制作,有大型网站制作公司丰富经验。已为上林千余家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的上林做网站的公司定做!
序
本文主要研究一下nacos config的EventDispatcher
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会添加listener到Entry;fireEvent会遍历指定event的listener然后回调其onEvent方法;clear会清空整个LISTENER_HUB
Entry
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有个eventType属性以及AbstractEventListener类型的listener集合
AbstractEventListener
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会添加listener到Entry;fireEvent会遍历指定event的listener然后回调其onEvent方法;clear会清空整个LISTENER_HUB
到此,相信大家对“nacos config的EventDispatcher是什么 ”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
文章名称:nacosconfig的EventDispatcher是什么
分享网址:http://ybzwz.com/article/jsojjd.html