流程分析
EventBus 是一个发布 / 订阅的事件总线,总线可以有一个也可以有多个。
总共包含4个成分:发布者,订阅者,事件,总线。
订阅者通过register方法订阅某类型事件到总线中,总线按照事件类型存储订阅方法,发送者发布事件到总线中,总线查找事件对应的订阅方法并执行。
并以事件类型为key存入eventbus总线的集合mEventTypeMap中,同时也会也存入class为key的集合mSubscriberTypeMap中,方便解注册。
源码梳理
public void register(Object subscriber) {
Class> subscriberClass = subscriber.getClass();
List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
1.查找所有方法
register方法的第一步就是就是找到所有我们在订阅类中@Subscribe注解的方法,findSubscriberMethods的实现也很简单,通过反射、注解器,结合缓存找到类中所有订阅方法。ignoreGeneratedIndex默认false,也就是说默认从调用findUsingReflection,该方法主要就是寻找订阅者类中的公有方法,且其参数唯一的以及含有注解声明@Subscribe 的方法。就是我们在写代码时所定义的订阅者方法了。找到订阅者方法后,会将其 method(方法的反射类 Method) 、event、thread mode 以及优先级等封装成一个 SubscribeMethod 然后添加到 FindState 中。
这个 FindState 是 SubscriberMethodFinder 的一个内部类。其用了大小只有 4 的 FIND_STATE_POOL 来进行管理,这样避免了 FindState 对象的重复创建。
最后在 find 中会将所找到订阅者方法添加到 “METHOD_CACHE” 中,这是一个 ConcurrentHashMap 的结构也就是之前提到的缓存。findUsingInfo方法只是查找方式稍有不同,在此不再赘述。
List findSubscriberMethods(Class> subscriberClass) {
List subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
if (ignoreGeneratedIndex) {
subscriberMethods = findUsingReflection(subscriberClass);
} else {
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;
}
}
private List findUsingReflection(Class> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
findUsingReflectionInSingleClass(findState);
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
2.处理方法
完成了方法的查找后,接下来就是把这些方法存在总线中,方便接收事件,处理事件,解绑等操作。这里分别用了 3 个 HashMap 来描述。subscriptionsByEventType 记录了一个事件应该调用哪个订阅者的订阅方法来处理。而 typesBySubscriber 记录了订阅者订阅了哪些事件。stickyEvents是粘性的消息。至此,订阅者的注册完成之后,也就相当于是 EventBus 的初始化完成
private final Map, CopyOnWriteArrayList> subscriptionsByEventType;
private final Map
3.发送消息
这个代码比较简单,主要是拿到eventQueue,将事件入队,eventqueue存在PostingThreadState类中,满足当前队列中没有执行事件的条件下,执行postSingleEvent(eventQueue.remove(0), postingState)方法,
这里用了 ThreadLocal 来保存 PostingThreadState。ThreadLocal 的主要作用是使得所定义的资源是线程私有的。对于每一个发送事件的线程其都有一个唯一的 PostingThreadState 来记录事件发送的队列以及状态。
/** Posts the given event to the event bus. */
public void post(Object event) {
PostingThreadState postingState = currentPostingThreadState.get();
List
4.接下来是事件分发处理流程 postSingleEvent-----》postSingleEventForEventType----》postToSubscription
postSingleEvent
所做的事情主要就是根据要发送的事情收集事件,然后逐个发送。收集的什么事情呢,很简单,就是收集它的父类事件。也就是说,当我们发送一个事件时,它的所有父类事件也同时会被发送,
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
Class> eventClass = event.getClass();
boolean subscriptionFound = false;
if (eventInheritance) {
List> 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) {
if (logNoSubscriberMessages) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
postSingleEventForEventType
就是从 subscriptionsByEventType 找出事件所订阅的订阅者以及订阅者方法
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class> eventClass) {
CopyOnWriteArrayList subscriptions;
synchronized (this) {
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;
}
postToSubscription
这个方法也是比较简单主要是根据你方法注解时的参数,来进行线程的切换,比如:MAIN,意思就是在主线程中执行,当前做了判断主线程直接执行,非主线程就扔到主线程消息队列中执行。
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);
}
}
设计模式分析
观察者模式
1.订阅者: 所有包含 @Subscribe(threadMode = ThreadMode.xxx)注解头的方法的类,通过EventBus.getDefault().register()方法
2.发布者:EventBus.getDefault().post(事件类型);
3.取消订阅EventBus.getDefault().unregister();
单例模式
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
享元设计模式
考虑到方法的注册会根据生命周期的onstart调用,调用时机比较频繁,注册方法也会很多,为避免对面的频繁创建,回收,这里用了大小只有 4 的 FIND_STATE_POOL 来进行管理。
private static final int POOL_SIZE = 4;
private static final FindState[] FIND_STATE_POOL = new FindState[POOL_SIZE];
//取出数据对象
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();
}
private List getMethodsAndRelease(FindState findState) {
List subscriberMethods = new ArrayList<>(findState.subscriberMethods);
findState.recycle();
//准备数据对象
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
if (FIND_STATE_POOL[i] == null) {
FIND_STATE_POOL[i] = findState;
break;
}
}
}
return subscriberMethods;
}
FindState findState = prepareFindState();