這篇文章主要是根據(jù)我們平時(shí)的使用盾鳞,一步一步的分析EventBus源碼流程爬虱,因此分為三步:
1、注冊(cè)訂閱者
2躺涝、事件發(fā)布
3厨钻、反注冊(cè)訂閱者
1、register 注冊(cè)訂閱者
在使用eventBus的時(shí)候坚嗜,第一個(gè)步驟就是注冊(cè)訂閱者
EventBus.getDefault().register(this);
getDefault方法是一個(gè)單例模式的初始化方法,主要就是獲取一個(gè)實(shí)例诗充,源碼如下:
public static EventBus getDefault() {
EventBus instance = defaultInstance;
if (instance == null) {
synchronized (EventBus.class) {
instance = EventBus.defaultInstance;
if (instance == null) {
instance = EventBus.defaultInstance = new EventBus();
}
}
}
return instance;
}
可以很清楚的看到苍蔬,這里只是一個(gè)DoubleCheck的單例模式,接下來(lái)直接看一下構(gòu)造方法:
/**
* Creates a new EventBus instance; each instance is a separate scope in which events are delivered. To use a
* central bus, consider {@link #getDefault()}.
*/
public EventBus() {
this(DEFAULT_BUILDER);
}
EventBus(EventBusBuilder builder) {
logger = builder.getLogger();
subscriptionsByEventType = new HashMap<>();
typesBySubscriber = new HashMap<>();
stickyEvents = new ConcurrentHashMap<>();
mainThreadSupport = builder.getMainThreadSupport();
mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
backgroundPoster = new BackgroundPoster(this);
asyncPoster = new AsyncPoster(this);
indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
//
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;
}
構(gòu)造方法里面是通過(guò)一個(gè)Builder模式來(lái)對(duì)EventBus各項(xiàng)配置進(jìn)行初始化
在getDefault獲取到實(shí)例之后蝴蜓,就會(huì)調(diào)用register方法進(jìn)行注冊(cè)碟绑,這時(shí)候進(jìn)入register方法看一下注冊(cè)過(guò)程:
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
//獲取到訂閱者中所有的訂閱方法,并儲(chǔ)存到list集合中
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
從源碼里面看茎匠,注冊(cè)過(guò)程只有兩步:
a格仲、根據(jù)注冊(cè)時(shí)傳入的訂閱者對(duì)象,找到所有的訂閱方法诵冒;
b凯肋、訂閱所有的訂閱方法
a1)、
首先看一下如何查找到訂閱者中所有的訂閱方法汽馋,即findSubscriberMethods()方法侮东,subscriberMethodFinder是在EventBus構(gòu)造方法中進(jìn)行的初始化,進(jìn)入findSubscriberMethods方法中進(jìn)行查看:
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
//METHOD_CACHE 一個(gè)map集合豹芯,以訂閱者為key悄雅,以訂閱方法的list集合為value進(jìn)行緩存
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
//如果當(dāng)前的對(duì)象已經(jīng)被緩存,則直接獲取返回
if (subscriberMethods != null) {
return subscriberMethods;
}
if (ignoreGeneratedIndex) {
// 如果忽略索引铁蹈,就根據(jù)反射來(lái)獲取
subscriberMethods = findUsingReflection(subscriberClass);
} else {
//否則使用索引
subscriberMethods = findUsingInfo(subscriberClass);
}
//如果該訂閱者中沒(méi)有訂閱方法宽闲,此處會(huì)拋出一個(gè)異常,提醒你該訂閱者和他的父類里面都沒(méi)有訂閱方法(public 修飾并且@Subscribe 進(jìn)行注解)
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
//如果有訂閱方法,則把該訂閱者和訂閱方法進(jìn)行緩存容诬,并返回訂閱方法的list集合
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
a1.1)围辙、
findSubscriberMethods 里面的邏輯已經(jīng)很清晰了,現(xiàn)在看一下 findUsingInfo()和findUsingReflection()放案,一般使用的時(shí)候姚建,并沒(méi)有自定義進(jìn)行配置,所以一般都是使用索引進(jìn)行查找
a1.1.1)
這里就先分析一下findUsingInfo()方法吱殉,反射的稍后再看:
//忽略索引時(shí)掸冤,查找方法
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
// 創(chuàng)建并初始化FindState對(duì)象 FindState是訂閱者的一個(gè)輔助類,用于獲取到訂閱方法
FindState findState = prepareFindState();
//關(guān)聯(lián)訂閱者
findState.initForSubscriber(subscriberClass);
//此處通過(guò)循環(huán)友雳,獲取到訂閱者父類中的訂閱方法
while (findState.clazz != null) {
//獲取到訂閱者的信息
findState.subscriberInfo = getSubscriberInfo(findState);
if (findState.subscriberInfo != null) {
//如果可以找到訂閱者信息稿湿,將訂閱方法緩存到 subscriberMethods 中
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
//如果找不到訂閱者的信息,則通過(guò)反射方法進(jìn)行獲取
findUsingReflectionInSingleClass(findState);
}
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
FindState類是一個(gè)輔助類押赊,用來(lái)輔助訂閱者獲取訂閱方法饺藤,代碼比較簡(jiǎn)單,篇幅原因此處就不在進(jìn)行源碼解析流礁,只要清楚這個(gè)類主要用途即可
prepareFindState() 方法則是用來(lái)創(chuàng)建并初始化FindState的方法涕俗,同時(shí)坐著也通過(guò)一個(gè)緩存池進(jìn)行了優(yōu)化
從上面源碼中可以看到,在可以直接查找到訂閱信息的情況比較簡(jiǎn)單神帅,直接將查找到的信息進(jìn)行緩存即可再姑,下面分析一下找不到訂閱信息的時(shí)候,通過(guò)反射方法進(jìn)行查找:
//通過(guò)反射方式找御,獲取到訂閱者中所有的訂閱方法
private void findUsingReflectionInSingleClass(FindState findState) {
// 1 獲取訂閱者內(nèi)所有方法
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, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
// 2 遍歷所有方法元镀,找到所有的訂閱方法,并將所有的訂閱方法進(jìn)行緩存到 findState 中
for (Method method : methods) {
int modifiers = method.getModifiers();
//如果是非靜態(tài)霎桅、非抽象栖疑、public 修飾的
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
//只有一個(gè)參數(shù)
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
// Subscribe 進(jìn)行注解
if (subscribeAnnotation != null) {
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
//符合條件的進(jìn)行緩存
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}
認(rèn)真看一下每一個(gè)步驟,可以發(fā)現(xiàn)其實(shí)就是通過(guò)反射查找到符合條件的訂閱方法滔驶,然后將訂閱方法緩存到findState中遇革,同時(shí)對(duì)注解的訂閱方法進(jìn)行校驗(yàn),給出提示瓜浸,在查找到所有的訂閱方法之后澳淑,代碼執(zhí)行到了,a1.1.1) findUsingReflectionInSingleClass 執(zhí)行完畢插佛,然后 findUsingInfo()在循環(huán)查找并緩存訂閱者中所有的訂閱方法杠巡,至此, 在a1.1) findUsingInfo 方法也執(zhí)行完畢雇寇,成功找到了所有的訂閱方法氢拥,a1)蚌铜、中忽略索引的情況已經(jīng)執(zhí)行完畢
查找完成之后,我們?cè)诨剡^(guò)頭看一下a1)嫩海、findSubscriberMethods 方法中不忽略 索引的方法 findUsingReflection()
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
findUsingReflectionInSingleClass(findState);
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
這段代碼就非常簡(jiǎn)單了冬殃,上面已經(jīng)分析過(guò)了,直接使用反射查找到所有的方法叁怪,并進(jìn)行緩存审葬;
在查找到所有的訂閱方法之后,將所有的方法進(jìn)行緩存到 METHOD_CACHE 奕谭,至此涣觉,注冊(cè)第一步就完成了;
無(wú)論使用哪一種方法血柳,到這里都已經(jīng)找到所有的訂閱方法了官册,在再次返回到 register()方法中:
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
這段代碼通過(guò)循環(huán),將所有的訂閱方法和訂閱者關(guān)聯(lián)起來(lái)难捌,看一下subscribe 方法
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;
//創(chuàng)建訂閱者和訂閱方法的封裝對(duì)象
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//由于所有的訂閱方法都只有一個(gè)參數(shù)膝宁,并且subscriptionsByEventType 是以 訂閱方法的參數(shù)的class為key,Subscription集合為value的
// //的map集合根吁,所以员淫,根據(jù)eventType 可以找到所有的含有此參數(shù)的訂閱方法所在的訂閱者
// 根據(jù)訂閱方法的參數(shù)類型,查找到所有包含有該Event 的訂閱者婴栽,Event 即為訂閱方法的參數(shù)
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);
}
}
//根據(jù)優(yōu)先級(jí)满粗,將當(dāng)前的訂閱者插入訂閱集合
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;
}
}
//根據(jù)訂閱者,查找訂閱者里面所有的Event 類型愚争,并儲(chǔ)存
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
//對(duì)于粘性事件,則立刻執(zhí)行
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);
}
}
}
至此挤聘,訂閱者和訂閱事件進(jìn)行了關(guān)聯(lián)轰枝,并且進(jìn)行了緩存,register()完成
2组去、事件發(fā)布
事件發(fā)布一般通過(guò)以下兩種方法進(jìn)行發(fā)布鞍陨,postSticky 為發(fā)送粘性事件
EventBus.getDefault().post(new Event("我是測(cè)試event數(shù)據(jù)"));
EventBus.getDefault().postSticky(new Event("我是測(cè)試event數(shù)據(jù)"));
首先先分析一下post 事件
public void post(Object event) {
//獲取到ThreadLocal中儲(chǔ)存的數(shù)據(jù),并將當(dāng)前post的數(shù)據(jù)添加到隊(duì)列中
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
//如果當(dāng)前沒(méi)有在post 事件
if (!postingState.isPosting) {
postingState.isMainThread = isMainThread();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
//通過(guò)循環(huán)取出隊(duì)列中所有數(shù)據(jù)从隆,并進(jìn)行訂閱
try {
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
//此處僅判斷是否允許繼承诚撵,如果允許就會(huì)執(zhí)行父類中的訂閱方法,不作為重點(diǎn)關(guān)注內(nèi)容
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) {
logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
//subscriptionsByEventType键闺,key 為訂閱方法的參數(shù)類型寿烟,value為訂閱者和訂閱方法封裝的list集合
//根據(jù)register()的源碼,已經(jīng)知道在注冊(cè)的時(shí)候辛燥,會(huì)將訂閱者和訂閱方法緩存到subscriptionsByEventType里面
synchronized (this) {
subscriptions = subscriptionsByEventType.get(eventClass);
}
//如果post的Event有訂閱者接收筛武,循環(huán)進(jìn)行訂閱
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;
}
// 訂閱方法的執(zhí)行方法缝其,會(huì)根據(jù)threadMode將訂閱方法在不同的線程進(jìn)行執(zhí)行
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 MAIN_ORDERED:
if (mainThreadPoster != null) {
mainThreadPoster.enqueue(subscription, event);
} else {
// temporary: technically not correct as poster not decoupled from subscriber
invokeSubscriber(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);
}
}
post方法本身并不復(fù)雜,重點(diǎn)關(guān)注一下postToSubscription()方法徘六,在此方法中内边,根據(jù)不同的線程進(jìn)行處理,執(zhí)行訂閱方法待锈,到這里漠其,事件發(fā)布就完成了,訂閱方法也已經(jīng)被執(zhí)行了竿音,完成了事件的分發(fā)
3和屎、反注冊(cè)訂閱者
反注冊(cè)就非常簡(jiǎn)單了,僅僅是將緩存的訂閱者及其訂閱方法移除即可
public synchronized void unregister(Object subscriber) {
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
typesBySubscriber.remove(subscriber);
} else {
logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}
從代碼中可以看出谍失,反注冊(cè)的時(shí)候?qū)⒆?cè)時(shí)候緩存在 typesBySubscriber 和subscriptionsByEventType 中的數(shù)據(jù)移除眶俩,完成了反注冊(cè)