在注册好后,就该发送事件了。
post函数:
/** Posts the given event to the event bus. */
public void post(Object event) {
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
if (!postingState.isPosting) {//新的boolean变量默认为false
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();//设置当前线程是否为主线程
postingState.isPosting = true;//表示正在发送
if (postingState.canceled) {//默认为false
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;
}
}
}
该函数上来就是PostingThreadState
内部类定义如下:
final static class PostingThreadState {
final List<Object> eventQueue = new ArrayList<Object>();//事件列表
boolean isPosting;//是否正在发送
boolean isMainThread;//是否是主线程
Subscription subscription;//指向一条注册
Object event;//事件
boolean canceled;//是否取消了
}
而currentPostingThreadState的为ThreadLoacal变量,即当前线程独自拥有的变量。
再开具体的发送函数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);
}
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));
}
}
}
看看涉及到lookupAllEventTypes,就是查找到发生事件的所有相关类(父类)
/** Looks up all Class objects including super classes and interfaces. Should also work for interfaces. */
private static List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
synchronized (eventTypesCache) {
List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
if (eventTypes == null) {
eventTypes = new ArrayList<>();
Class<?> clazz = eventClass;
while (clazz != null) {
eventTypes.add(clazz);
addInterfaces(eventTypes, clazz.getInterfaces());//添加上所有的接口
clazz = clazz.getSuperclass();//父类
}
eventTypesCache.put(eventClass, eventTypes);
}
return eventTypes;
}
}
此处又有采用缓存的设计了:
private static final Map<Class<?>, List<Class<?>>> eventTypesCache = new HashMap<>();
从代码中可以看出eventTypesCache中元素的内容为<事件类型-->[事件类型,所有父类,所有实现接口,直至不存在父类为止]>
那么
postSingleEventForEventType
作为进一步深入的发送事件函数:
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(eventClass);//查找是否存在处理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么,这个函数通过subscription决定了是否继续分发下去,注意这里分发的时候是按照优先级的(为什么呢,因为我们创建这个数组的时候就是按照优先级来的,虽然我们使用的是默认的统一的优先级)
好了,以上的作为一个小阶段,下边我们看看这个具体的发送是如何做到了,虽然上一节也带过了一下该函数,但没有细说。
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.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);
}
}
当我们在注册事件函数的时候采用的是默认参数,那么处理模式就是Posting。我们就以Posting为例,来看:
哇,直接就invokeSubscriber了;而对于Main模式来说,如果不在主线程的话,会发给主线程处理;对于background模式,如果当前是主线程,则交给后台线程处理;如果是Async模式,则直接交给异步线程处理。
那么这里涉及到了:
invokeSubscriber(subscription, event);
mainThreadPoster.enqueue(subscription, event);
backgroundPoster.enqueue(subscription, event);
asyncPoster.enqueue(subscription, event);
共四种处理方式。
第一种:invokeSubscriber
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);
}
}
采用的是直接出发注册的响应函数的方式。
注意这里不会去管subscription中isCanceled的属性,依然为false。
第二种:mainThreadPoster.enqueue
先看看mainThreadPoster的定义:
private final HandlerPoster mainThreadPoster;
起初始化:
mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);即主线程(ui线程)的handler
具体处理参考:EventBus源码阅读(15)-HandlerPoster
注意在处理过程中依然没有处理subscription中isCanceled的属性,依然为false
第三种:backgroundPoster.enqueue
backgroundPoster初始化:
backgroundPoster = new BackgroundPoster(this);
参考EventBus源码阅读(14)-BackgroundPoster
交给线程池去处理了
同样没有处理isCanceled属性
第四种:asyncPoster.enqueue
初始化:
asyncPoster = new AsyncPoster(this);
参考EventBus源码阅读(13)-AsyncPoster
后两种处理方式都是使用EventBus中的getExecuterService来处理任务的。
ExecutorService getExecutorService() {
return executorService;
}
executerService的初始化:
executorService = builder.executorService;
在eventBusBuilder中的初始化:
private final static ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();
ExecutorService executorService = DEFAULT_EXECUTOR_SERVICE;
同时执行具体的执行操作,最终都调用了
eventBus.invokeSubscriber(pendingPost);
我们返回来继续看
invokeSubscriber
/** * Invokes the subscriber if the subscriptions is still active. Skipping subscriptions prevents race conditions * between {@link #unregister(Object)} and event delivery. Otherwise the event might be delivered after the * subscriber unregistered. This is particularly important for main thread delivery and registrations bound to the * live cycle of an Activity or Fragment. */ void invokeSubscriber(PendingPost pendingPost) { Object event = pendingPost.event; Subscription subscription = pendingPost.subscription; PendingPost.releasePendingPost(pendingPost); if (subscription.active) { invokeSubscriber(subscription, event); } }
到这里就到头了。
先辨看看我们已经阅读了多少了,用绿色的点标记的是我们这节读的: