EventBus源码解析
EventBus是一个开源的事件总线项目,项目地址:EventBus
EventBus通过注册监听器和发布消息的方式来完成事件的传递,如下所示:

其中Publisher为事件的发布者,Subscriber为事件的订阅者。下图为EventBus的DEMO使用方法:

使用EventBus主要分为以下四步:
1、注册成为事件接受者:EventBus.getDefault().register(this)将当前类注册为事件接受者
2、注销:通过EventBus.getDefault().unregister(this)进行注销;
3、订阅事件:通过@Subscribe注解对方法进行注解,注解的方法必须有且仅有一个参数,这里我们定义了MyEvent作为订阅的事件。
4、发布事件:EventBus.getDefault().post(MyEvent)可以发布一个事件,EventBus会根据发布的消息的类型,找到该类型消息的订阅者,完成消息的传递。
@Subscribe注解

@Subscribe注解的定义如上图所示,通过@Rentention将注解定义为TUNTIME,表示该注解将会保留到JVM运行时,也就是可以通过反射进行调用;通过@Target将注解定义为METHOD,表示该注解只能应用于method;在使用@Subscribe注解时,可以设置threadMode、sticky、priority属性。
threadMode:指定订阅方法运行的线程,根据其值在对应的线程调用订阅Method。
sticky:指定是否是粘性事件,如果为true,在调用register的时候,会接受到注册监听之前EventBus postSticky发送过的同一类型的消息。
priority:优先级,EventBus根据订阅者的优先级来顺序发布事件。
EventBus的主要成员
EventBus.getDefault()以单例的方式获取EventBus实例,这里重点介绍下EventBus的成员变量:

1、subscriptionsByEventType

是一个Map对象,Key为@Subcribe注解的方法的参数类型,对应到本文一开始的DEMO的MyEvent的Class对象,Value为Subscription列表。

Subscription的subscriber对象指的是EventBus.getDefault().register(this)的this对象,subscriberMethod指的是@Subcribe注解的方法的Method对象。
一句话:subscriptionsByEventType保存了订阅MyEvent事件的订阅者列表。
2、typesBySubscriber

是一个Map对象,Key为EventBus.getDefault().register(this)的this对象,Value为this对象中所有的@Subcribe注解方法的参数类型,也就是订阅的事件类型。
3、stickyEvents

是一个Map对象,postSticky发送的粘性事件会保存到该Map中。

Key为发送的粘性事件的Class对象,Value为粘性事件。
4、mainThreadSupport&mainThreadPoster(threadMode=MAIN 或者MAIN_ORDERED)
类型为mainThreadSupport类型为MainThreadSupport,mainThreadPoster类型为HandlerPoster。

通过Looper对象判断当前是否是主线程,创建HandlerPoster赋值给mainThreadPoster,HandlerPoster继承自Handler,handleMessage完成事件的分发,保证订阅方法运行在主线程。
5、backgroundPoster(threadMode=BACKGROUND)
和mainThreadPoster类似,实现类是BackgroundPoster,该类继承实现了Runnable接口,在异步线程调用订阅者的方法。

6、asyncPoster(threadMode=ASYNC)

实现类是AsyncPoster,也是通过后台线程调用订阅者方法,完成事件发布。但与BackgroundPoster有如下区别:
BackgroundPoster:如果当前线程是主线程,就调用backgroundPoster发布事件;如果当前发布时已经是异步线程,那么就直接反射运行订阅者方法,不需要开启线程了。
AsyncPoster:无论在哪个线程post事件都开启新的线程执行。
7、subscriberMethodFinder
实现类是SubscriberMethodFinder,在EventBus.getDefault().register(this)的时候查询this对象及其父类对象的所有@Subcribe注解的方法。
到这里,介绍完EventBus主要的成员后,下面分析下EventBus.getDefault().register的流程。
注册事件订阅者
EventBus.getDefault().register(this);将当前类注册为事件订阅者。

register主要分为以下三步:
1、subscriber.getClass获取订阅者的Class对象
2、subscriberMethodFinder.findSubscriberMethods(subscriberClass)获取订阅者及其父类的所有@Subscribe注解的Method。

通过findUsingInfo来获取订阅者及其父类的所有订阅方法:

findUsingInfo首先初始化了FindState,FindState是个什么东东?

上图描述了FindState的作用和信息,FindState用来查找和保存订阅者及其父类的订阅信息,订阅的方法保存在subscriberMethods中;subscriberInfo默认为空,所以在初始化完FindState后会循环调用findUsingReflectionInSingleClass方法,该方法通过反射来获取FindState.clazz的@Subscribe注解方法,findUsingInfo会一直循环,直到clazz已经没有符合要求的父类了。

在循环结束后,findState.subscriberMethods保存了订阅者及其父类的所有SubscriberMethod。
3、对每个subscriberMethod调用subscribe(subscriber, subscriberMethod)方法。
第2步获取到所有订阅方法后,就循环调用subscribe(subscriber, subscriberMethod)。


至此,register方法运行结束了,subscriptionsByEventType中保存了某个类型消息对应的Subscription列表,Subscription中包含了订阅者和订阅方法;typesBySubscriber保存了某个订阅者的所有订阅事件类型;最后也完成了sticky事件的发布。
发布事件
通过EventBus.getDefault().post(postEvent);可以发布一个事件。

发布事件有以下过程:
1、获取当前的发布状态PostingThreadState

PostingThreadState定义了当前正在发布的事件状态。
2、将事件保存到等待队列
3、循环调用postSingleEvent发布事件

postSingleEvent调用了lookupAllEventTypes()获取要发布的事件及其父类的Class对象,也就是说如果发布MyEvent事件,订阅了MyEvent事件或者其父类事件的订阅者都能收到MyEvent事件(可以这样理解,订阅了MyEvent的父类事件,MyEvent是其父类的一种类型,也应该发布)。postSingleEventForEventType完成单个事件的发布:

首先在subscriptionsByEventType获取eventClass对应的订阅列表,然后调用postToSubscription进行发布。

postToSubscription会根据SubscriberMethod的ThreadMode确定调用哪个Poster就进行事件发布。无论在主线程还是异步线程,最终都是调用了invokeSubscriber:

invokeSubscriber通过反射调用了subscriber的method,并传入了Event参数。
4、重置PostingThreadState状态
注销
通过EventBus.getDefault().unregister(this);可以完成注销,即订阅者不在关注任何事件了。


至此,EventBus的注册、订阅消息、发布消息、注销的流程都已经分析完毕。