EventBus-3.1.1源碼閱讀

1. 注冊及查找事件

1.1 EventBus初始化

  • getDefault: 通過單例模式獲取實例愈污,同時里面采用Builder模式構造部分初始化參數(shù)张遭,方便定制部分配置信息
    //Builder模式,在EventBusBuilder里面完成一些默認的初始化操作
    private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
    
    public static EventBus getDefault() {
       //通過DCL單例模式創(chuàng)建EventBus實例
    }
    
    public EventBus() {
        this(DEFAULT_BUILDER);
    }
    
    EventBus(EventBusBuilder builder) {
        //通過EventBusBuilder完成的初始化構造
    }
    

1.2 EventBus注冊

  • register:根據(jù)訂閱者類信息畔裕,查詢對應的訂閱方法信息法精,遍歷注冊訂閱方法

    public void register(Object subscriber) {
        //根據(jù)訂閱者的類名信息查詢訂閱方法
        Class<?> subscriberClass = subscriber.getClass();
        List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
        synchronized (this) {
            //遍歷并注冊這些訂閱方法
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
                subscribe(subscriber, subscriberMethod);
            }
        }
    }
    
  • SubscriberMethod:封裝了訂閱方法的基本類型信息粥谬,即@Subscribe中所注解的內容

    public SubscriberMethod(Method method, Class<?> eventType, ThreadMode threadMode, int priority, boolean sticky) {
            this.method = method;
            this.threadMode = threadMode;
            this.eventType = eventType;
            this.priority = priority;
            this.sticky = sticky;
        }
    

1.3 查詢訂閱者所有的訂閱方法

  • findSubscriberMethods: 查詢訂閱者所有的訂閱方法,三種查詢方式:緩存进胯,通過注解器索引,反射

    //METHOD_CACHE緩存了訂閱及其所有訂閱方法
    private static final Map<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new ConcurrentHashMap<>();
    
    List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        //首先查找緩存
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }
        //ignoreGeneratedIndex默認為false,表示是否忽略注解生成器
        if (ignoreGeneratedIndex) {
            subscriberMethods = findUsingReflection(subscriberClass);//通過反射獲取
        } else {
            subscriberMethods = findUsingInfo(subscriberClass);//通過注解的方式
        }
        if (subscriberMethods.isEmpty()) {//如果當前訂閱者沒有聲明訂閱方法粉渠,則拋出異常
        } else {
            //將訂閱者及訂閱方法存入緩存中
            METHOD_CACHE.put(subscriberClass, subscriberMethods);
            ...
        }
    }
    

1.3.1 通過注解索引的方式獲取訂閱者的訂閱方法

  • findUsingInfo:獲取訂閱者信息分冈,并且對其父類進行向上查詢,最后將所有訂閱方法信息封裝到subscriberMethods列表中返回

     private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
        FindState findState = prepareFindState();//通過對象池的方式復用FindState
        findState.initForSubscriber(subscriberClass);//初始化參數(shù)并將訂閱者類信息導入
        while (findState.clazz != null) {
             //獲取訂閱者信息
            findState.subscriberInfo = getSubscriberInfo(findState);
            //使用了注解索引
            if (findState.subscriberInfo != null) {
                SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
                for (SubscriberMethod subscriberMethod : array) {
                    if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
                        findState.subscriberMethods.add(subscriberMethod);//將訂閱方法存入
                    }
                }
            } else {
                //未使用注解索引霸株,通過反射查詢訂閱者的訂閱方法
                findUsingReflectionInSingleClass(findState);
            }
             // 將findState.clazz改為subscriberClass的父類Class雕沉,再進行遍歷
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);//回收FindState對象到對象池中,并返回subscriberMethods列表
    }
        
    ...
    //實現(xiàn)對象池
    private static final FindState[] FIND_STATE_POOL = new FindState[POOL_SIZE];// POOL_SIZE = 4;
    private FindState prepareFindState() {
        synchronized (FIND_STATE_POOL) {
            for (int i = 0; i < POOL_SIZE; i++) {
                FindState state = FIND_STATE_POOL[i];
                if (state != null) {
                    FIND_STATE_POOL[i] = null;
                    return state;
                }
            }
        }
        return new FindState();
    }
    
    
  • getSubscriberInfo:遍歷訂閱者索引去件,返回查找的訂閱者的信息

    private SubscriberInfo getSubscriberInfo(FindState findState) {
        //上面已經(jīng)有一次判斷蘑秽,所以這個條件不會進入
        if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {
            SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
            if (findState.clazz == superclassInfo.getSubscriberClass()) {
                return superclassInfo;
            }
        }
        //遍歷訂閱者索引,查找訂閱者的信息
        if (subscriberInfoIndexes != null) {
            for (SubscriberInfoIndex index : subscriberInfoIndexes) {
                SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
                if (info != null) {
                    return info;
                }
            }
        }
        return null;
    }
    
  • FindState 主要用來輔助存儲訂閱者信息

    static class FindState {
            final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
            final Map<Class, Object> anyMethodByEventType = new HashMap<>();
            final Map<String, Class> subscriberClassByMethodKey = new HashMap<>();
            final StringBuilder methodKeyBuilder = new StringBuilder(128);
            ...
            void moveToSuperclass() { ... }// 修改findState.clazz為subscriberClass的父類Class
    }  
    

1.3.2 通過反射獲取訂閱者的訂閱方法

  • findUsingReflection

    //FindState主要用來輔助存儲訂閱者信息
    private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
        FindState findState = prepareFindState();//通過對象池的方式復用FindState
        findState.initForSubscriber(subscriberClass);//初始化參數(shù)并將訂閱者類信息導入
        while (findState.clazz != null) {
            findUsingReflectionInSingleClass(findState);//通過反射查詢訂閱者的訂閱方法
            // 將findState.clazz改為subscriberClass的父類Class箫攀,再進行遍歷
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);//回收FindState對象到對象池中肠牲,并返回subscriberMethods列表
    }
    
    
  • findUsingReflectionInSingleClass:通過反射查詢訂閱者的訂閱方法

    private void findUsingReflectionInSingleClass(FindState findState) {
        Method[] methods;
        try {
            // This is faster than getMethods, especially when subscribers are fat classes like Activities
            methods = findState.clazz.getDeclaredMethods();//獲取所有聲明方法
        } catch (Throwable th) {
            // Workaround for java.lang.NoClassDefFoundError
            methods = findState.clazz.getMethods();
            findState.skipSuperClasses = true;
        }
        for (Method method : methods) {
            int modifiers = method.getModifiers();
            
            //校驗方法修飾屬性,是否PUBLIC及MODIFIERS_IGNORE
            // MODIFIERS_IGNORE = Modifier.ABSTRACT | Modifier.STATIC | BRIDGE | SYNTHETIC;
            
            if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
                Class<?>[] parameterTypes = method.getParameterTypes();//校驗方法參數(shù)
                if (parameterTypes.length == 1) {
                    //拿到注解信息
                    Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
                    if (subscribeAnnotation != null) {
                        Class<?> eventType = parameterTypes[0];
                        if (findState.checkAdd(method, eventType)) {
                            ThreadMode threadMode = subscribeAnnotation.threadMode();
                            //將訂閱方法及方法的注解信息封裝到列表中
                            findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                                    subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                        }
                    }
                } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
                     //拋出異常
                }
            } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
                //拋出異常
            }
        }
    }
    

1.4 注冊訂閱方法:

  • subscribe:將訂閱者和訂閱方法封裝成對象靴跛,并將相同事件類型的封裝對象根據(jù)事件優(yōu)先級封裝到CopyOnWriteArrayList集合中缀雳,再將事件類型和CopyOnWriteArrayList封裝到map集合中

    Map<Object, List<Class<?>>> typesBySubscriber;
    Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
    Map<Class<?>, Object> stickyEvents = new ConcurrentHashMap<>();
    
    private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        Class<?> eventType = subscriberMethod.eventType;
        //將訂閱者和訂閱方法封裝成對象
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
        //查詢出同樣事件類型的所有Subscription對象
        CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
    
        //通過CopyOnWriteArrayList對訂閱事件對象按事件類型,按照優(yōu)先級進行存儲(同優(yōu)先級則插入末尾),并校驗是否多次注冊(已注冊拋出異常)
        if (subscriptions == null) {
            subscriptions = new CopyOnWriteArrayList<>();
            subscriptionsByEventType.put(eventType, subscriptions);
        } else {
            if (subscriptions.contains(newSubscription)) {
                throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
                        + eventType);
            }
        }
        
        int size = subscriptions.size();
        for (int i = 0; i <= size; i++) {
            if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
                subscriptions.add(i, newSubscription);
                break;
            }
        }
        
        //將訂閱者對象和訂閱事件存儲對應存儲到map集合中
        List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
        if (subscribedEvents == null) {
            subscribedEvents = new ArrayList<>();
            typesBySubscriber.put(subscriber, subscribedEvents);
        }
        subscribedEvents.add(eventType);
    
        if (subscriberMethod.sticky) {//處理粘性事件
            if (eventInheritance) {
                // 迭代所有事件可能會導致很多粘性事件的效率低下,因此應該更改數(shù)據(jù)結構以允許更有效的查找
                Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
                for (Map.Entry<Class<?>, Object> entry : entries) {
                    Class<?> candidateEventType = entry.getKey();
                    if (eventType.isAssignableFrom(candidateEventType)) {
                        Object stickyEvent = entry.getValue();
                        //對粘性事件進行通知
                        checkPostStickyEventToSubscription(newSubscription, stickyEvent);
                    }
                }
            } else {
                Object stickyEvent = stickyEvents.get(eventType);
                checkPostStickyEventToSubscription(newSubscription, stickyEvent);
            }
        }
    }
    

2. 發(fā)布通知消息

  • post:通過在ThreadLocal中封裝一個事件隊列梢睛,去添加和執(zhí)行相應的事件

    public void post(Object event) {
        //在ThreadLocal中封裝一個線程狀態(tài)信息肥印,里面包含一個事件隊列
        PostingThreadState postingState = currentPostingThreadState.get();
        List<Object> eventQueue = postingState.eventQueue;
        eventQueue.add(event);//將任務添加到事件隊列
    
        if (!postingState.isPosting) {
            postingState.isMainThread = isMainThread();
            postingState.isPosting = true;
            if (postingState.canceled) {
                throw new EventBusException("Internal error. Abort state was not reset");
            }
            try {
                while (!eventQueue.isEmpty()) {
                    //遍歷發(fā)送當前事件隊列
                    postSingleEvent(eventQueue.remove(0), postingState);
                }
            } finally {
                postingState.isPosting = false;
                postingState.isMainThread = false;
            }
        }
    }
    private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
            @Override
            protected PostingThreadState initialValue() {
                return new PostingThreadState();
            }
        };
    
  • postSingleEvent

    private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<?> eventClass = event.getClass();
        boolean subscriptionFound = false;
        if (eventInheritance) {
            //遍歷查找它的父類添加到列表
            List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
            int countTypes = eventTypes.size();
            for (int h = 0; h < countTypes; h++) {
                Class<?> clazz = eventTypes.get(h);
                //發(fā)送該事件
                subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
            }
        } else {
            subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
        }
        ... //找不到事件時異常處理 NoSubscriberEvent
    }
    
    private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
            CopyOnWriteArrayList<Subscription> subscriptions;
            synchronized (this) {
                //獲取對應的訂閱者
                subscriptions = subscriptionsByEventType.get(eventClass);
            }
            ...
            for (Subscription subscription : subscriptions) {
                postingState.event = event;
                postingState.subscription = subscription;
                boolean aborted = false;
                try {//通知訂閱者
                    postToSubscription(subscription, event, postingState.isMainThread);
                    aborted = postingState.canceled;
                } finally {
                    ...//重置postingState屬性
                }
                if (aborted)break;
            }
            ...
        }
    
  • postToSubscription:通知訂閱者识椰,對于不同線程發(fā)布消息會調用相應的線程事件隊列去執(zhí)行

    private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
            case POSTING:
                //通反射調用過訂閱者的方法
                invokeSubscriber(subscription, event);
                break;
            case MAIN:
                if (isMainThread) {
                    invokeSubscriber(subscription, event);
                } else {
                    //如果不是主線程,則加入到主線程執(zhí)行隊列中執(zhí)行深碱,mainThreadPoster繼承自Handler
                    mainThreadPoster.enqueue(subscription, event);
                }
                break;
            case MAIN_ORDERED:
                if (mainThreadPoster != null) {
                    mainThreadPoster.enqueue(subscription, event);
                } else {
                    invokeSubscriber(subscription, event);
                }
                break;
            case BACKGROUND:
                if (isMainThread) {
                    //如果不是后臺線程腹鹉,則加入到后臺線程行隊列中執(zhí)行
                    backgroundPoster.enqueue(subscription, event);
                } else {
                    invokeSubscriber(subscription, event);
                }
                break;
            case ASYNC:
                asyncPoster.enqueue(subscription, event);
                break;
            ...
        }
    }
    ...
    void invokeSubscriber(Subscription subscription, Object event) {
        ...
        subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
    }
    
    

3. 發(fā)布粘性通知

  • postSticky: 粘性事件會通過單獨的stickyEvents集合進行存儲,同時會在事件訂閱的時候進行check post敷硅,繼而實現(xiàn)粘性這一效果

     public void postSticky(Object event) {
            synchronized (stickyEvents) {
                stickyEvents.put(event.getClass(), event);
                //添加到粘性事件列表功咒,在上述注冊訂閱方法時,會通過列表去通知信息
            }
            // Should be posted after it is putted, in case the subscriber wants to remove immediately
            post(event);//發(fā)布通知绞蹦,當前已經(jīng)訂閱的訂閱者可以收到
        }
        
    ...
    
      private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        ...
    
        if (subscriberMethod.sticky) {//處理粘性事件
            if (eventInheritance) {
                // 迭代所有事件可能會導致很多粘性事件的效率低下力奋,因此應該更改數(shù)據(jù)結構以允許更有效的查找
                Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
                for (Map.Entry<Class<?>, Object> entry : entries) {
                    Class<?> candidateEventType = entry.getKey();
                    if (eventType.isAssignableFrom(candidateEventType)) {
                        Object stickyEvent = entry.getValue();
                        //對粘性事件進行通知
                        checkPostStickyEventToSubscription(newSubscription, stickyEvent);
                    }
                }
            } else {
                Object stickyEvent = stickyEvents.get(eventType);
                checkPostStickyEventToSubscription(newSubscription, stickyEvent);
            }
        }
    }
    
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市幽七,隨后出現(xiàn)的幾起案子景殷,更是在濱河造成了極大的恐慌,老刑警劉巖澡屡,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件猿挚,死亡現(xiàn)場離奇詭異,居然都是意外死亡驶鹉,警方通過查閱死者的電腦和手機绩蜻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來梁厉,“玉大人,你說我怎么就攤上這事踏兜〈使耍” “怎么了?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵碱妆,是天一觀的道長肉盹。 經(jīng)常有香客問我,道長疹尾,這世上最難降的妖魔是什么上忍? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮纳本,結果婚禮上窍蓝,老公的妹妹穿的比我還像新娘。我一直安慰自己繁成,他們只是感情好吓笙,可當我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著巾腕,像睡著了一般面睛。 火紅的嫁衣襯著肌膚如雪絮蒿。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天叁鉴,我揣著相機與錄音土涝,去河邊找鬼。 笑死幌墓,一個胖子當著我的面吹牛但壮,可吹牛的內容都是我干的。 我是一名探鬼主播克锣,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼茵肃,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了袭祟?” 一聲冷哼從身側響起验残,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎巾乳,沒想到半個月后您没,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡胆绊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年氨鹏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片压状。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡仆抵,死狀恐怖,靈堂內的尸體忽然破棺而出种冬,到底是詐尸還是另有隱情镣丑,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布娱两,位于F島的核電站莺匠,受9級特大地震影響,放射性物質發(fā)生泄漏十兢。R本人自食惡果不足惜趣竣,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望旱物。 院中可真熱鬧遥缕,春花似錦、人聲如沸宵呛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至封孙,卻和暖如春迹冤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背虎忌。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工泡徙, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人膜蠢。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓堪藐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親挑围。 傳聞我的和親對象是個殘疾皇子礁竞,可洞房花燭夜當晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內容

  • 項目到了一定階段會出現(xiàn)一種甜蜜的負擔:業(yè)務的不斷發(fā)展與人員的流動性越來越大,代碼維護與測試回歸流程越來越繁瑣杉辙。這個...
    fdacc6a1e764閱讀 3,163評論 0 6
  • EventBus基本使用 EventBus基于觀察者模式的Android事件分發(fā)總線模捂。 從這個圖可以看出,Even...
    顧氏名清明閱讀 616評論 0 1
  • EventBus源碼理解 EventBus是我們在開發(fā)中經(jīng)常使用的開源庫蜘矢,使用起來比較簡單狂男,而且源碼看起來不是很吃...
    崔老板閱讀 247評論 0 2
  • 先吐槽一下博客園的MarkDown編輯器,推出的時候還很高興博客園支持MarkDown了品腹,試用了下發(fā)現(xiàn)支持不完善就...
    Ten_Minutes閱讀 557評論 0 2
  • BroadcastReceiver ? 本質上是一個監(jiān)聽器岖食,分為接收者和發(fā)送者。用于監(jiān)聽(接收)應用發(fā)出的廣播...
    dw_0920閱讀 244評論 0 0