EventBus-3.1.1源码阅读

1. 注册及查找事件

1.1 EventBus初始化

  • getDefault: 通过单例模式获取实例,同时里面采用Builder模式构造部分初始化参数,方便定制部分配置信息
    //Builder模式,在EventBusBuilder里面完成一些默认的初始化操作
    private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
    
    public static EventBus getDefault() {
       //通过DCL单例模式创建EventBus实例
    }
    
    public EventBus() {
        this(DEFAULT_BUILDER);
    }
    
    EventBus(EventBusBuilder builder) {
        //通过EventBusBuilder完成的初始化构造
    }
    

1.2 EventBus注册

  • register:根据订阅者类信息,查询对应的订阅方法信息,遍历注册订阅方法

    public void register(Object subscriber) {
        //根据订阅者的类名信息查询订阅方法
        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);//初始化参数并将订阅者类信息导入
        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列表
    }
        
    ...
    //实现对象池
    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) {
        //上面已经有一次判断,所以这个条件不会进入
        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);//初始化参数并将订阅者类信息导入
        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();//校验方法参数
                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:将订阅者和订阅方法封装成对象,并将相同事件类型的封装对象根据事件优先级封装到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对订阅事件对象按事件类型,按照优先级进行存储(同优先级则插入末尾),并校验是否多次注册(已注册抛出异常)
        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) {
                // 迭代所有事件可能会导致很多粘性事件的效率低下,因此应该更改数据结构以允许更有效的查找
                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. 发布通知消息

  • post:通过在ThreadLocal中封装一个事件队列,去添加和执行相应的事件

    public void post(Object event) {
        //在ThreadLocal中封装一个线程状态信息,里面包含一个事件队列
        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()) {
                    //遍历发送当前事件队列
                    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);
                //发送该事件
                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:通知订阅者,对于不同线程发布消息会调用相应的线程事件队列去执行

    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 {
                    //如果不是主线程,则加入到主线程执行队列中执行,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) {
                    //如果不是后台线程,则加入到后台线程行队列中执行
                    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. 发布粘性通知

  • postSticky: 粘性事件会通过单独的stickyEvents集合进行存储,同时会在事件订阅的时候进行check post,继而实现粘性这一效果

     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);//发布通知,当前已经订阅的订阅者可以收到
        }
        
    ...
    
      private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        ...
    
        if (subscriberMethod.sticky) {//处理粘性事件
            if (eventInheritance) {
                // 迭代所有事件可能会导致很多粘性事件的效率低下,因此应该更改数据结构以允许更有效的查找
                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);
            }
        }
    }
    
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • 项目到了一定阶段会出现一种甜蜜的负担:业务的不断发展与人员的流动性越来越大,代码维护与测试回归流程越来越繁琐。这个...
    fdacc6a1e764阅读 3,147评论 0 6
  • EventBus基本使用 EventBus基于观察者模式的Android事件分发总线。 从这个图可以看出,Even...
    顾氏名清明阅读 612评论 0 1
  • EventBus源码理解 EventBus是我们在开发中经常使用的开源库,使用起来比较简单,而且源码看起来不是很吃...
    崔老板阅读 243评论 0 2
  • 先吐槽一下博客园的MarkDown编辑器,推出的时候还很高兴博客园支持MarkDown了,试用了下发现支持不完善就...
    Ten_Minutes阅读 555评论 0 2
  • BroadcastReceiver ​ 本质上是一个监听器,分为接收者和发送者。用于监听(接收)应用发出的广播...
    dw_0920阅读 239评论 0 0