EventBus3.0 源码分析

简述:

    在项目中,我们大多数开发者可能都使用过EventBus,即使没有使用过但我可以确定Android开发者也听说过这个牛X的库,从诞生到目前EventBus已经更新到3.X版本,可见生命力极强呀。那么这篇博文就从EventBus3.0源码的角度分析一下其内部处理流程。

使用流程:

    注册:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. EventBus.getDefault().register(obj)  

《EventBus3.0 源码分析》

EventBus.getDefault().register(obj)

    订阅(消息接收):

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. @Subscribe  
  2. public void receive(Object event){  
  3. }  

《EventBus3.0 源码分析》

@Subscribe
public void receive(Object event){
}

    发布消息:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. EventBus.getDefault().post(event)  

《EventBus3.0 源码分析》

EventBus.getDefault().post(event)

    注销:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. EventBus.getDefault().unregister(obj)  

《EventBus3.0 源码分析》

EventBus.getDefault().unregister(obj)

源码分析:

注册:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. EventBus.getDefault().register(obj)   

《EventBus3.0 源码分析》

EventBus.getDefault().register(obj) 

   这段代码做了两件事情:① EventBus.getDefault() 创建EventBus对象;② register(obj) 方法为obj该类对象注册EventBus。 那这两个方法究竟在EventBus中究竟做了哪些工作呢?我们打开EventBus的源码看一下:

   1、EventBus.getDefault()
    源码如下:
[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. public static EventBus getDefault() {  
  2.     if (defaultInstance == null) {  
  3.         synchronized (EventBus.class) {  
  4.             if (defaultInstance == null) {  
  5.                 defaultInstance = new EventBus();  
  6.             }  
  7.         }  
  8.     }  
  9.     return defaultInstance;  
  10. }  

《EventBus3.0 源码分析》

public static EventBus getDefault() {
    if (defaultInstance == null) {
        synchronized (EventBus.class) {
            if (defaultInstance == null) {
                defaultInstance = new EventBus();
            }
        }
    }
    return defaultInstance;
}

    看到了吧,EventBus采用单例模式创建EventBus对象,接下来它在构造方法中又做了什么事情呢?
[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. public EventBus() {  
  2.     this(DEFAULT_BUILDER);  
  3. }  

《EventBus3.0 源码分析》

public EventBus() {
    this(DEFAULT_BUILDER);
}

    在构造方法中其调用了有参构造方法:EventBus(EventBusBuilder builder ),我们再跟进去看一看:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. EventBus(EventBusBuilder builder) {  
  2.     subscriptionsByEventType = new HashMap<>();  
  3.     typesBySubscriber = new HashMap<>();  
  4.     stickyEvents = new ConcurrentHashMap<>();  
  5.   
  6.     mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);  
  7.     backgroundPoster = new BackgroundPoster(this);  
  8.     asyncPoster = new AsyncPoster(this);  
  9.   
  10.     indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;  
  11.   
  12.     //默认情况下参数为(null,false,false)  
  13.     subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,  
  14.             builder.strictMethodVerification, builder.ignoreGeneratedIndex);  
  15.   
  16.     logSubscriberExceptions = builder.logSubscriberExceptions;  
  17.     logNoSubscriberMessages = builder.logNoSubscriberMessages;  
  18.     sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;  
  19.     sendNoSubscriberEvent = builder.sendNoSubscriberEvent;  
  20.     throwSubscriberException = builder.throwSubscriberException;  
  21.     eventInheritance = builder.eventInheritance;  
  22.     executorService = builder.executorService;  
  23. }  

《EventBus3.0 源码分析》

EventBus(EventBusBuilder builder) {
    subscriptionsByEventType = new HashMap<>();
    typesBySubscriber = new HashMap<>();
    stickyEvents = new ConcurrentHashMap<>();

    mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);
    backgroundPoster = new BackgroundPoster(this);
    asyncPoster = new AsyncPoster(this);

    indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;

    //默认情况下参数为(null,false,false)
    subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
            builder.strictMethodVerification, builder.ignoreGeneratedIndex);

    logSubscriberExceptions = builder.logSubscriberExceptions;
    logNoSubscriberMessages = builder.logNoSubscriberMessages;
    sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;
    sendNoSubscriberEvent = builder.sendNoSubscriberEvent;
    throwSubscriberException = builder.throwSubscriberException;
    eventInheritance = builder.eventInheritance;
    executorService = builder.executorService;
}

    这段代码对一些变量进行了初始化,现在就挑重要的变量解释一下。首先,初始化了3个Map,这3个Map有什么用呢?我们再来一一详细说一下:
    ① Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType:key:事件类型(如:String类型或者自定义的事件类型),value:该事件的订阅者的list集合。当发送event消息的时候,都是去这里找对应的订阅者。
    ② Map<Object, List<Class<?>>> typesBySubscriber:key:事件的订阅者(如XXXActivity),value:事件类型的运行时类的list集合。register()unregister()的时候都是操作这个Map
    ③ Map<Class<?>, Object> stickyEvents:维护的是粘性事件的集合,粘性事件也就是当event发送出去之后再注册粘性事件的话,该粘性事件也能接收到之前发送出去的event消息。
    其次,初始化3个消息发送器如下:
    mainThreadPoster :该类继承自Handler,而且在EventBus中mainThreadPoster属于主线程Handler,这是因为mainThreadPoster就是为处理“消息接收方法在主线程而消息发送在子线程 ”这个问题而设计的,所以子线程向主线程发送消息必须使用主线程的Handler。mainThreadPoster继承Handler也是为了效率考虑的。
    backgroundPoster:该类继承自Runnable,重写了run()方法。在run()方法中将子线程中的消息通过EventBus发送到主线程。所以这个消息发送器作用就是处理“消息接收方法在子线程接而消息的发布在主线程”这样的问题。
    asyncPoster:该类继承自Runnable,也重写了run()方法,不过就像名字一样“异步”,也就是说不管订阅者是不是在主线程,消息接收方法都会另外开启一个线程处理消息。
    然后,一个重要的初始化对象为subscriberMethodFinder,这个对象利用反射的方法查找每一个接收消息者的方法(也即是添加了“@Subscribe ”注解的方法)。
    最后就是一些对EventBusBuilder的一些配置信息。其中eventInheritanceexecutorService在接下来分析源码时会经常碰到:① eventInheritance 表示我们自定义的待发布消息事件是否允许继承,,默认情况下eventInheritance ==true。它的作用就是处理业务时后来新增加业务后不必再修改代码,只需要继承就OK啦(这也符合程序的“开闭原则”)如下:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. public class MessageEvent {  
  2.     public final String message;  
  3.   
  4.     public MessageEvent(String message) {  
  5.         this.message = message;  
  6.     }  
  7. }  
  8.   
  9. public class SubMessageEvent extends MessageEvent {  
  10.     public SubMessageEvent(String message) {  
  11.         super(message);  
  12.     }  
  13. }  

《EventBus3.0 源码分析》

public class MessageEvent {
    public final String message;

    public MessageEvent(String message) {
        this.message = message;
    }
}

public class SubMessageEvent extends MessageEvent {
    public SubMessageEvent(String message) {
        super(message);
    }
}

    ②
executorService :这个线程池是给backgroundPoster和
asyncPoster用来处理消息发送的。这样做也能够提高消息发送的效率。


2、注册register(Object subscriber )
    EventBus的初始化工作已经完毕,我们继续看一下EventBus是怎么进行注册的,在注册过程中又搞了哪些事情?
[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. public void register(Object subscriber) {  
  2.     Class<?> subscriberClass = subscriber.getClass();  
  3.     List<SubscriberMethod> subscriberMethods =  subscriberMethodFinder  
  4.             .findSubscriberMethods(subscriberClass);  
  5.     synchronized (this) {  
  6.         for (SubscriberMethod subscriberMethod : subscriberMethods) {  
  7.             subscribe(subscriber, subscriberMethod);  
  8.         }  
  9.     }  
  10. }  

《EventBus3.0 源码分析》

public void register(Object subscriber) {
    Class<?> subscriberClass = subscriber.getClass();
    List<SubscriberMethod> subscriberMethods = subscriberMethodFinder
            .findSubscriberMethods(subscriberClass);
    synchronized (this) {
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }
    }
}

     在该方法中首先取得注册者的运行时类对象,拿到运行时类对象后通过注册者注册方法查找器SubscriberMethodFinder利用反射的方法找到注册者类中所有的接收消息的方法,也即是所有添加了注解“Subscribe ”的方法。最后进行通过方法subscribe(subscriber, subscriberMethod)为每一个接收消息的方法进行注册。流程大致就是这样的,首先 我们先看一下findSubscriberMethods这个方法:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. /**  
  2.     * 方法描述:获取该运行时类的所有@Subscribe注解的所有方法  
  3.     *  
  4.     * @param subscriberClass @Subscribe注解所属类的运行时类对象  
  5.     * @return 注册EventBus类中@Subscribe注解的所有方法的List集合  
  6.     */  
  7.    List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {  
  8.        //先从缓存中查找是否存在消息接收的方法  
  9.        List<SubscriberMethod> subscriberMethods =  METHOD_CACHE.get(subscriberClass);  
  10.        if (subscriberMethods != null) {  
  11.            return subscriberMethods;  
  12.        }  
  13.   
  14.        if (ignoreGeneratedIndex) {  
  15.            //使用反射方法拿到订阅者中的订阅方法  
  16.            subscriberMethods = findUsingReflection(subscriberClass);  
  17.        } else {  
  18.            //使用apt处理器拿到订阅者中的订阅方法  
  19.            subscriberMethods = findUsingInfo(subscriberClass);  
  20.        }  
  21.            //如果该消息事件没有被订阅则抛出异常  
  22.       if (subscriberMethods.isEmpty()) {  
  23.            throw new EventBusException(“Subscriber ” + subscriberClass  
  24.                    + ” and its super classes have no public methods with the @Subscribe annotation”);  
  25.        } else {  
  26.            //将查到的方法放到缓存中,以便下次使用  
  27.            METHOD_CACHE.put(subscriberClass, subscriberMethods);  
  28.            return subscriberMethods;  
  29.        }  
  30.    }  

《EventBus3.0 源码分析》

 /**
     * 方法描述:获取该运行时类的所有@Subscribe注解的所有方法
     *
     * @param subscriberClass @Subscribe注解所属类的运行时类对象
     * @return 注册EventBus类中@Subscribe注解的所有方法的List集合
     */
    List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        //先从缓存中查找是否存在消息接收的方法
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }

        if (ignoreGeneratedIndex) {
            //使用反射方法拿到订阅者中的订阅方法
            subscriberMethods = findUsingReflection(subscriberClass);
        } else {
            //使用apt处理器拿到订阅者中的订阅方法
            subscriberMethods = findUsingInfo(subscriberClass);
        }
            //如果该消息事件没有被订阅则抛出异常
       if (subscriberMethods.isEmpty()) {
            throw new EventBusException("Subscriber " + subscriberClass
                    + " and its super classes have no public methods with the @Subscribe annotation");
        } else {
            //将查到的方法放到缓存中,以便下次使用
            METHOD_CACHE.put(subscriberClass, subscriberMethods);
            return subscriberMethods;
        }
    }

    这个方法很重要,在初次使用EventBus3.0的时候也容易出错的一个点(3.0新增注解):就是在订阅事件的方法上没有添加
@Subscribe 注解 ,所以会碰到下面这个异常:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class XXX and its super classes   
  2. have no public methods with the @Subscribe annotation  

《EventBus3.0 源码分析》

Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class XXX and its super classes 
have no public methods with the @Subscribe annotation

    说到这我们还是没有最终看到EventBus是怎么进行注册的,
OK,回过头来我们继续看注册

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {  
  2.     Class<?> eventType = subscriberMethod.eventType;  
  3.     Log.e(TAG, eventType.getSimpleName());  
  4.     //将订阅类Object对象subscriber封装为EventBus的订阅类Subscription  
  5.     Subscription newSubscription = new Subscription(subscriber, subscriberMethod);  
  6.     CopyOnWriteArrayList<Subscription> subscriptions =  subscriptionsByEventType.get(eventType);  
  7.     if (subscriptions == null) {  
  8.         subscriptions = new CopyOnWriteArrayList< >();  
  9.         subscriptionsByEventType.put(eventType, subscriptions);  
  10.     } else {  
  11.         if (subscriptions.contains(newSubscription)) {  
  12.             throw new EventBusException(“Subscriber ” + subscriber.getClass() + ” already registered to event ”  
  13.                     + eventType);  
  14.         }  
  15.     }  
  16.   
  17.     /**  
  18.     * 方法描述:对CopyOnWriteArrayList中的Subscription根据优先级的高低重新进行排序  
  19.     */  
  20.     int size = subscriptions.size();  
  21.     for (int i = 0; i <= size; i++) {  
  22.         if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {  
  23.             subscriptions.add(i, newSubscription);  
  24.             break;  
  25.         }  
  26.     }  
  27.     /**  
  28.     * 方法描述:将开发者注册EventBus的类的运行时类添加到subscribedEvents中,并且把该运行时类添加到  
  29.     * typesBySubscriber中  
  30.     */  
  31.     List<Class<?>> subscribedEvents =  typesBySubscriber.get(subscriber);  
  32.     Log.e(TAG, “typesBySubscriber的”);  
  33.     if (subscribedEvents == null) {  
  34.         subscribedEvents = new ArrayList< >();  
  35.         typesBySubscriber.put(subscriber, subscribedEvents);  
  36.     }  
  37.     subscribedEvents.add(eventType);  
  38.   
  39.     if (subscriberMethod.sticky) {  
  40.         if (eventInheritance) {  
  41.             // Existing sticky events of all subclasses of eventType have to be considered.  
  42.             // Note: Iterating over all events may be inefficient with lots of sticky events,  
  43.             // thus data structure should be changed to allow a more efficient lookup  
  44.             // (e.g. an additional map storing sub classes of super classes: Class –> List <Class>).  
  45.             Set<Map.Entry<Class <?>, Object>> entries =  stickyEvents.entrySet();  
  46.             for (Map.Entry<Class<?>, Object > entry : entries) {  
  47.                 Class<?> candidateEventType =  entry.getKey();  
  48.                 if (eventType.isAssignableFrom(candidateEventType)) {  
  49.                     Object stickyEvent =  entry.getValue();  
  50.                     checkPostStickyEventToSubscription(newSubscription, stickyEvent);  
  51.                 }  
  52.             }  
  53.         } else {  
  54.             Object stickyEvent = stickyEvents.get(eventType);  
  55.             checkPostStickyEventToSubscription(newSubscription, stickyEvent);  
  56.         }  
  57.     }  
  58. }  

《EventBus3.0 源码分析》

private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
    Class<?> eventType = subscriberMethod.eventType;
    Log.e(TAG, eventType.getSimpleName());
    //将订阅类Object对象subscriber封装为EventBus的订阅类Subscription
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
    CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
    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);
        }
    }

    /**
    * 方法描述:对CopyOnWriteArrayList中的Subscription根据优先级的高低重新进行排序
    */
    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;
        }
    }
    /**
    * 方法描述:将开发者注册EventBus的类的运行时类添加到subscribedEvents中,并且把该运行时类添加到
    * typesBySubscriber中
    */
    List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
    Log.e(TAG, "typesBySubscriber的");
    if (subscribedEvents == null) {
        subscribedEvents = new ArrayList<>();
        typesBySubscriber.put(subscriber, subscribedEvents);
    }
    subscribedEvents.add(eventType);

    if (subscriberMethod.sticky) {
        if (eventInheritance) {
            // Existing sticky events of all subclasses of eventType have to be considered.
            // Note: Iterating over all events may be inefficient with lots of sticky events,
            // thus data structure should be changed to allow a more efficient lookup
            // (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
            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);
        }
    }
}

    上面的这段代码虽然很多,但主要做了几件事情:① 将注册的订阅者封装为新的Subscription类 ②将订阅者存储到Map集合
subscriptionsByEventType当中 ③对消息事件接收者根据优先级进行重排序 ④添加粘性消息事件

发布消息:

    我们已经分析完了EventBus的注册过程,接下来我们再来分析一下EventBus的事件发送过程。

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. EventBus.getDefault().post(event);  

《EventBus3.0 源码分析》

EventBus.getDefault().post(event);

    那么这段代码是如何实现消息的发送呢?继续源码看一下:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. public void post(Object event) {  
  2.     PostingThreadState postingState = currentPostingThreadState.get();  
  3.     List<Object> eventQueue =  postingState.eventQueue;  
  4.     eventQueue.add(event);//将该事件添加到事件队列当中  
  5.     //事件没有分发则开始分发  
  6.     if (!postingState.isPosting) {  
  7.         //判断消息接收者是否在主线程   
  8.         postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();    
  9.         postingState.isPosting = true;  
  10.         if (postingState.canceled) {  
  11.             throw new EventBusException(“Internal error. Abort state was not reset”);  
  12.         }  
  13.         try {  
  14.             //循环发送消息  
  15.             while (!eventQueue.isEmpty()) {  
  16.                 postSingleEvent(eventQueue.remove(0), postingState);  
  17.             }  
  18.         } finally {  
  19.             postingState.isPosting = false;  
  20.             postingState.isMainThread = false;  
  21.         }  
  22.     }  
  23. }  
  24.     从上面的代码中可以得知,待发送的消息首先存储到一个消息list集合当中,然后再不断的循环发送消息。发送消息时利用的方法是postSingleEvent(Object event, PostingThreadState postingState ),OK,我们继续跟进:  
  25. private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {  
  26.     Class<?> eventClass = event.getClass();  
  27.     boolean subscriptionFound = false;  
  28.     //默认情况下Event事件允许继承,即默认情况下eventInheritance==true  
  29.     if (eventInheritance) {  
  30.         //查找event事件及event子类事件  
  31.         List<Class<?>>  eventTypes = lookupAllEventTypes(eventClass);  
  32.         int countTypes = eventTypes.size();  
  33.         for (int h = 0; h <  countTypes; h++) {  
  34.             Class<?> clazz =  eventTypes.get(h);  
  35.             subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);  
  36.         }  
  37.     } else {  
  38.         subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);  
  39.     }  
  40.     if (!subscriptionFound) {  
  41.       …  
  42.     }  
  43. }  

《EventBus3.0 源码分析》

public void post(Object event) {
    PostingThreadState postingState = currentPostingThreadState.get();
    List<Object> eventQueue = postingState.eventQueue;
    eventQueue.add(event);//将该事件添加到事件队列当中
    //事件没有分发则开始分发
    if (!postingState.isPosting) {
        //判断消息接收者是否在主线程 
        postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();  
        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;
        }
    }
}
    从上面的代码中可以得知,待发送的消息首先存储到一个消息list集合当中,然后再不断的循环发送消息。发送消息时利用的方法是postSingleEvent(Object event, PostingThreadState postingState ),OK,我们继续跟进:
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
    Class<?> eventClass = event.getClass();
    boolean subscriptionFound = false;
    //默认情况下Event事件允许继承,即默认情况下eventInheritance==true
    if (eventInheritance) {
        //查找event事件及event子类事件
        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);
    }
    if (!subscriptionFound) {
      ...
    }
}

   在这个方法中并没有真正的看到消息的分发,而是查找了待分发事件消息及其子类或者是待分发消息接口及其子类的所有事件(默认情况下我们定义的消息事件是允许继承的。 我们在项目中起初可能考虑的不是很全面,再到后来不可预料的需求到来时我们可能会继续改事件的一种情况,看到这不得不说EventBus真心考虑周全呀)。然后调用postSingleEventForEventType(event, postingState, eventClass)方法查找该事件及其子类事件的订阅者,如果没有找到就发送空消息并打印日志。好吧,很失望,到现在依然没有看到对消息事件进行分发。那我们继续跟进:postSingleEventForEventType(event, postingState, eventClass);

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. private boolean postSingleEventForEventType(Object event, PostingThreadState postingState,  
  2.                                             Class<? > eventClass) {  
  3.     CopyOnWriteArrayList<Subscription> subscriptions;  
  4.     synchronized (this) {  
  5.         //从Map中取出所有订阅了eventClass事件的所有订阅者  
  6.         subscriptions = subscriptionsByEventType.get(eventClass);  
  7.     }  
  8.     //如果该事件的订阅者存在则向每一个订阅者发布消息事件  
  9.     if (subscriptions != null && !subscriptions.isEmpty()) {  
  10.         for (Subscription subscription : subscriptions) {  
  11.             postingState.event = event;  
  12.             postingState.subscription = subscription;  
  13.             boolean aborted = false;  
  14.             try {  
  15.                 postToSubscription(subscription, event, postingState.isMainThread);  
  16.                 aborted = postingState.canceled;  
  17.             } finally {  
  18.                 postingState.event = null;  
  19.                 postingState.subscription = null;  
  20.                 postingState.canceled = false;  
  21.             }  
  22.             if (aborted) {  
  23.                 break;  
  24.             }  
  25.         }  
  26.         return true;  
  27.     }  
  28.     return false;  
  29. }  

《EventBus3.0 源码分析》

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState,
                                            Class<?> eventClass) {
    CopyOnWriteArrayList<Subscription> subscriptions;
    synchronized (this) {
        //从Map中取出所有订阅了eventClass事件的所有订阅者
        subscriptions = subscriptionsByEventType.get(eventClass);
    }
    //如果该事件的订阅者存在则向每一个订阅者发布消息事件
    if (subscriptions != null && !subscriptions.isEmpty()) {
        for (Subscription subscription : subscriptions) {
            postingState.event = event;
            postingState.subscription = subscription;
            boolean aborted = false;
            try {
                postToSubscription(subscription, event, postingState.isMainThread);
                aborted = postingState.canceled;
            } finally {
                postingState.event = null;
                postingState.subscription = null;
                postingState.canceled = false;
            }
            if (aborted) {
                break;
            }
        }
        return true;
    }
    return false;
}

    好吧,小眼一瞄仍然没有对消息进行分发,而是查找事件的所有订阅者然后对所有订阅者进行了一层封装,封装成PostingThreadState。那我们还是继续吧,我们跟进postToSubscription(subscription, event, postingState.isMainThread)这个方法:

[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {  
  2.     switch (subscription.subscriberMethod.threadMode) {  
  3.         case POSTING:  
  4.             invokeSubscriber(subscription, event);  
  5.             break;  
  6.         case MAIN:  
  7.             if (isMainThread) {  
  8.                 invokeSubscriber(subscription, event);  
  9.             } else {  
  10.                 mainThreadPoster.enqueue(subscription, event);  
  11.             }  
  12.             break;  
  13.         case BACKGROUND:  
  14.             if (isMainThread) {  
  15.                 backgroundPoster.enqueue(subscription, event);  
  16.             } else {  
  17.                 invokeSubscriber(subscription, event);  
  18.             }  
  19.             break;  
  20.         case ASYNC:  
  21.             asyncPoster.enqueue(subscription, event);  
  22.             break;  
  23.         default:  
  24.             throw new IllegalStateException(“Unknown thread mode: ” + subscription.subscriberMethod.threadMode);  
  25.     }  
  26. }  

《EventBus3.0 源码分析》

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.enqueue(subscription, event);
            }
            break;
        case BACKGROUND:
            if (isMainThread) {
                backgroundPoster.enqueue(subscription, event);
            } else {
                invokeSubscriber(subscription, event);
            }
            break;
        case ASYNC:
            asyncPoster.enqueue(subscription, event);
            break;
        default:
            throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
    }
}

    看到这是不是有一种“复行数十步,豁然开朗”的感觉。是的,在这个方法中我们终于看到了对消息事件进行4中不同情况下的分发了。根据消息接收的threadMode分别进行了不同的处理:
    POSTING:EventBus默认情况下的threadMode类型,这里意思就是如果消息发布和消息接收在同一线程情况下就直接调用invokeSubscriber(subscription, event)对消息进行发送。这种情况下事件传递是同步完成的,事件传递完成时,所有的订阅者将已经被调用一次了。这个ThreadMode意味着最小的开销,因为它完全避免了线程的切换。
    MAIN:消息接收在主线程中进行(此种情况适合进行UI操作),如果消息发布也在主线程就直接调用invokeSubscriber(subscription, event)对消息进行发送(这种情况是POSTING的情况),如果消息发布不在主线程中进行,那么调用mainThreadPoster.enqueue(subscription, event)进行处理。他是怎么处理的呢?我们跟进去瞧瞧:
[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. final class HandlerPoster extends Handler {  
  2.   
  3.     private final PendingPostQueue queue;  
  4.     private final int maxMillisInsideHandleMessage;  
  5.     private final EventBus eventBus;  
  6.     private boolean handlerActive;  
  7.   
  8.     //默认情况下EventBus创建HandlerPoster的Looper为MainLooper,最大maxMillisInsideHandleMessage==10ms  
  9.     HandlerPoster(EventBus eventBus, Looper looper, int maxMillisInsideHandleMessage) {  
  10.         super(looper);  
  11.         this.eventBus = eventBus;  
  12.         this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;  
  13.         queue = new PendingPostQueue();  
  14.     }  
  15.   
  16.     /**  
  17.     * 方法描述:将订阅者与消息实体之间的映射存到队列PendingPostQueue当中  
  18.     *  
  19.     * @param subscription 订阅者  
  20.     * @param event        消息事件  
  21.     */  
  22.     void enqueue(Subscription subscription, Object event) {  
  23.         PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);  
  24.         synchronized (this) {  
  25.             queue.enqueue(pendingPost);  
  26.             if (!handlerActive) {  
  27.                 handlerActive = true;  
  28.                 if (!sendMessage(obtainMessage())) {  
  29.                     throw new EventBusException(“Could not send handler message”);  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     @Override  
  36.     public void handleMessage(Message msg) {  
  37.         boolean rescheduled = false;  
  38.         try {  
  39.             long started = SystemClock.uptimeMillis();  
  40.             while (true) {  
  41.                 //同步取出消息队列  
  42.                 PendingPost pendingPost = queue.poll();  
  43.                 if (pendingPost == null) {  
  44.                     synchronized (this) {  
  45.                         // Check again, this time in synchronized  
  46.                         pendingPost =  queue.poll();  
  47.                         if (pendingPost == null) {  
  48.                             handlerActive =  false;  
  49.                             return;  
  50.                         }  
  51.                     }  
  52.                 }  
  53.                 eventBus.invokeSubscriber(pendingPost);  
  54.                 long timeInMethod = SystemClock.uptimeMillis() – started;  
  55.                 //消息发送超时  
  56.                 if (timeInMethod >= maxMillisInsideHandleMessage) {  
  57.                     if (!sendMessage(obtainMessage())) {  
  58.                         throw new EventBusException(“Could not send handler message”);  
  59.                     }  
  60.                     rescheduled =  true;  
  61.                     return;  
  62.                 }  
  63.             }  
  64.         } finally {  
  65.             handlerActive = rescheduled;  
  66.         }  
  67.     }  
  68. }  

《EventBus3.0 源码分析》

final class HandlerPoster extends Handler {

    private final PendingPostQueue queue;
    private final int maxMillisInsideHandleMessage;
    private final EventBus eventBus;
    private boolean handlerActive;

    //默认情况下EventBus创建HandlerPoster的Looper为MainLooper,最大maxMillisInsideHandleMessage==10ms
    HandlerPoster(EventBus eventBus, Looper looper, int maxMillisInsideHandleMessage) {
        super(looper);
        this.eventBus = eventBus;
        this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;
        queue = new PendingPostQueue();
    }

    /**
    * 方法描述:将订阅者与消息实体之间的映射存到队列PendingPostQueue当中
    *
    * @param subscription 订阅者
    * @param event        消息事件
    */
    void enqueue(Subscription subscription, Object event) {
        PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
        synchronized (this) {
            queue.enqueue(pendingPost);
            if (!handlerActive) {
                handlerActive = true;
                if (!sendMessage(obtainMessage())) {
                    throw new EventBusException("Could not send handler message");
                }
            }
        }
    }

    @Override
    public void handleMessage(Message msg) {
        boolean rescheduled = false;
        try {
            long started = SystemClock.uptimeMillis();
            while (true) {
                //同步取出消息队列
                PendingPost pendingPost = queue.poll();
                if (pendingPost == null) {
                    synchronized (this) {
                        // Check again, this time in synchronized
                        pendingPost = queue.poll();
                        if (pendingPost == null) {
                            handlerActive = false;
                            return;
                        }
                    }
                }
                eventBus.invokeSubscriber(pendingPost);
                long timeInMethod = SystemClock.uptimeMillis() - started;
                //消息发送超时
                if (timeInMethod >= maxMillisInsideHandleMessage) {
                    if (!sendMessage(obtainMessage())) {
                        throw new EventBusException("Could not send handler message");
                    }
                    rescheduled = true;
                    return;
                }
            }
        } finally {
            handlerActive = rescheduled;
        }
    }
}


    为了说明问题,我们整个类贴出来:由于是主线程向子线程发送消息所以Looper采用的是主线程Looper,Handler也就是主线程Handler,其内部维护了一个PendingPost的对象池,这样做也是为了提高内存利用率,这也不是重点,我们直接看重点,在enqueue(Subscription subscription, Object event)方法中利用HandlerPoster 发送空消息,HandlerPoster也重写了handleMessage方法,在handleMessage方法中又调用eventBus.invokeSubscriber(pendingPost)进行消息发送,我们跟进去之后发现最终还是调用了invokeSubscriber(subscription, event)对消息进行发送。
     BACKGROUND:这种情况是消息接收在子线程(此种模式下适合在接收者方法中做IO等耗时操作)。那么如果消息发布也在某个子线程中进行的就直接调用invokeSubscriber(subscription, event)对消息进行发送,如果消息发布在主线程当中应该尽可能快的将消息发送出去以免造成主线程阻塞,所以这时候就交给backgroundPoster去处理。它是怎么处理的呢?我们进去看一看:
[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. final class BackgroundPoster implements Runnable {  
  2.   
  3.     ….  
  4.     private volatile boolean executorRunning;  
  5.   
  6.     BackgroundPoster(EventBus eventBus) {  
  7.         this.eventBus = eventBus;  
  8.         queue = new PendingPostQueue();  
  9.     }  
  10.   
  11.     public void enqueue(Subscription subscription, Object event) {  
  12.         PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);  
  13.         synchronized (this) {  
  14.                 …  
  15.                 eventBus.getExecutorService().execute(this);  
  16.             }  
  17.         }  
  18.     }  
  19.   
  20.     @Override  
  21.     public void run() {  
  22.         try {  
  23.              while (true) {  
  24.                     …  
  25.              eventBus.invokeSubscriber(pendingPost);  
  26.             }  
  27.         } finally {  
  28.             executorRunning = false;  
  29.         }  
  30.     }  
  31. }  

《EventBus3.0 源码分析》

final class BackgroundPoster implements Runnable {

    ....
    private volatile boolean executorRunning;

    BackgroundPoster(EventBus eventBus) {
        this.eventBus = eventBus;
        queue = new PendingPostQueue();
    }

    public void enqueue(Subscription subscription, Object event) {
        PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
        synchronized (this) {
                ...
                eventBus.getExecutorService().execute(this);
            }
        }
    }

    @Override
    public void run() {
        try {
             while (true) {
                    ...
             eventBus.invokeSubscriber(pendingPost);
            }
        } finally {
            executorRunning = false;
        }
    }
}


     backgroundPoster对Runnable进行了重写,而且和HandlerPoster一样也采用了对象池提高效率,当然重点是其开启了线程池处理消息的发送,这也是避免阻塞主线程的举措。当然其最终还是调用了invokeSubscriber()—–》invokeSubscriber(subscription, event)方法。
    ASYNC:这种情况是消息接收在子线程(如果消息发布在子线程中进行,那么该子线程既不同于消息发布的子线程,又不在主线程,而是接收消息是一个独立于主线程又不同于消息发布的子线程)。由于在这种模式下每一个新添加的任务都会在线程池中开辟一个新线程执行,所以并发量更高效。而且最终还是会调用invokeSubscriber(subscription, event)方法对消息进行分发。     既然4种模式下均是调用了invokeSubscriber(subscription, event)方法,那我们最后再看一下这个方法:
[html]
view plain
copy
print
?
《EventBus3.0 源码分析》
《EventBus3.0 源码分析》

  1. void invokeSubscriber(Subscription subscription, Object event) {  
  2.     try {  
  3.         subscription.subscriberMethod.method.invoke(subscription.subscriber, event);  
  4.     } catch (InvocationTargetException e) {  
  5.         handleSubscriberException(subscription, event, e.getCause());  
  6.     } catch (IllegalAccessException e) {  
  7.         throw new IllegalStateException(“Unexpected exception”, e);  
  8.     }  
  9. }  

《EventBus3.0 源码分析》

void invokeSubscriber(Subscription subscription, Object event) {
    try {
        subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
    } catch (InvocationTargetException e) {
        handleSubscriberException(subscription, event, e.getCause());
    } catch (IllegalAccessException e) {
        throw new IllegalStateException("Unexpected exception", e);
    }
}

   看到了吧,这个方法中就是利用反射将消息发送给每一个消息的订阅者。到此我们就完整的看完了EventBus的工作流程及主要代码的分析过程。真心不容易呀,已经被累跪啦!

    原文作者:Android源码分析
    原文地址: https://juejin.im/entry/58edd12d5c497d0062ceb557
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞