一文分析EventBus-事件总线的使用方法和实现原理
作者:互联网
前言
本文主要从源码的角度来分析事件总线 EventBus 的实现原理, EventBus 是常用的消息传递的方式之一,其他常见的消息传递的方式还包括 Handler
、BroadcastReceiver
、Listener
。通过本篇你在掌握 EventBus 基本使用的基础之上,能够掌握 EventBus 的实现原理。下面的框架图可以清晰的看到这一点。
一、定义事件类
作为事件的发布者,需要定义所发布的事件的类:
public class MessageEvent {
private String msg;
public MessageEvent(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
二、注册/取消注册响应事件
作为事件的订阅者,需要把响应事件的对象注册到EventBus当中:EventBus.getDefault().register(obj)
当不需要处理某个类型的事件的时候,取消对这个事件的监听:EventBus.getDefault().unregister(obj)
三、声明和注释订阅方法,选择指定线程模式
作为事件的订阅者,需要定义事件的响应方法,方法名称可以随意取,方法的形参类型,必须和监听的事件对象类型一致:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
Toast.makeText(this, event.getMsg (),
Toast.LENGTH_SHORT).show();
}
3.1 四种线程模式
事件订阅者可以通过注解的方式选择处理事件的方法所在的线程:
-
PostThread: 如果事件处理函数指定了线程模型为PostThread,那么事件的发布和接收处理会在同一个线程当中。
-
BackgroundThread: 如果事件处理函数指定了线程模型为BackgroundThread,那么如果事件是在UI线程中发布出来的,那么该事件处理函数就会在新的子线程中运行,如果事件发布本来就是非UI线程中发布出来 的,那么该事件处理函数直接在发布事件的线程中执行。
-
MainThread: 如果事件处理函数指定了线程模型为MainThread,那么不论事件对象是在哪个线程中发布出来的,该事件处理函数都会在UI线程中执行。
-
Async: 如果事件处理函数指定了线程模型为Async,那么无论事件在哪个线程发布,该事件处理函数都会在新建的子线程中执行。
3.2 黏性事件
通过注解的方式设置sticky为true,那么事件处理函数则可以处理上一次的事件对象:
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
四、EventBus 3.0源码详解
4.1 注册流程
/**
* Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
* are no longer interested in receiving events.
* <p/>
* Subscribers have event handling methods that must be annotated by {@link Subscribe}.
* The {@link Subscribe} annotation also allows configuration like {@link
* ThreadMode} and priority.
*/
public void register(Object subscriber) {
//通过注册的对象得到其类的class对象
Class<?> subscriberClass = subscriber.getClass();
//通过类的class对象得到此对象的订阅方法列表
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod :subscriberMethods) {
//线程同步,遍历订阅方法列表,注册每一个订阅方法
subscribe(subscriber, subscriberMethod);
}
}
}
代码subscriberMethodFinder.findSubscriberMethods(subscriberClass)获取订阅方法列表具体如下:
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
//在缓存中查找此class对象对应的订阅方法列表
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
//是否忽略注解器生成的MyEventBusIndex类
if (ignoreGeneratedIndex) {
//通过反射机制得到订阅者类class对象对应的订阅事件方法列表
subscriberMethods = findUsingReflection(subscriberClass);
} else {
//从注解器生成的MyEventBusIndex类中获得订阅类的订阅方法列表
subscriberMethods = findUsingInfo(subscriberClass);
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
//缓存此class对象的订阅方法列表
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
通过反射机制获取订阅方法列表:
private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
//遍历当前class对象和其父类中的订阅方法
findUsingReflectionInSingleClass(findState);
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
findUsingReflectionInSingleClass方法:
private void findUsingReflectionInSingleClass(FindState findState) {
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;
}
//遍历此类的方法
for (Method method : methods) {
int modifiers = method.getModifiers();
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
//形参只有一个的函数
if (parameterTypes.length == 1) {
//得到此函数的注解信息对象
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
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");
}
}
}
至此,我们得到了所有的订阅函数列表,下一步,会对每一个订阅函数进行注册:
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;
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);
}
}
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;
}
}
//通过订阅者对象得到其对应的监听事件类型列表
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
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);
}
}
事件的注册流程实际是从监听者对象和消息事件两个维度,将对方分别添加到自己对应的列表当中,具体可以通过以下流程图总结:
4.2发布流程
/** Posts the given event to the event bus. */
public void post(Object event) {
//通过ThreadLocal机制得到当前线程的postingState对象
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
//在此线程的eventQueue中添加此事件对象
eventQueue.add(event);
if (!postingState.isPosting) {
//判断当前线程是否UI线程
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;
}
}
}
ThreadLocal机制可以存储各个线程的局部数据;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));
}
}
}
事件消息对象具体的分发函数: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);
}
}
至此,我们完成了事件消息对象的分发流程,以下流程图来总结post的过程:
4.3 取消注册流程
/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object subscriber) {
//根据订阅者对象得到其对应的事件类型列表
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
//遍历事件类型列表,得到每个事件类型对应的订阅者对象列表,遍历这个列表,如果是此观察者对象,则从列表中删除
unsubscribeByEventType(subscriber, eventType);
}
//typesBySubscriber中删除此订阅者对象
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
unsubscribeByEventType函数:
/** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
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--;
}
}
}
}
取消注册的流程总结如下:
-
通过观察者类对象通过MAP表得到其对应的事件类class对象列表.
-
遍历list列表,通过事件类class对象得到其在MAP表中对应的观察者类对象列表。
-
遍历此观察者对象列表,判断如果列表中存在需要取消的注册观察者对象,则从对象列表中删除此观察者对象。
-
从第1步中得到的MAP对象中删除以取消注册的观察者对象为key的映射项目。
-
完成unregister过程。
标签:订阅,一文,对象,总线,列表,线程,事件,EventBus,event 来源: https://blog.csdn.net/nufuli123/article/details/122831854