1. register
EventBus.getDefault().register(this);
- 在register 中主要获取 在Activity中 定义的EventBus 方法,并封装成SubscriberMethod 保存在一个集合中,主要 方法 findSubscriberMethods
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
// 获取类里面的方法集合
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
- findSubscriberMethods ,通过自定义获取 或者 通过反射获取 响应方法。
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
List<SubscriberMethod> 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;
}
}
- findUsingInfo 自定义获取响应方法,可以看到,如果有自定义设置的信息,直接拿来使用,如果没有还是要通过反射去查找所有方法。
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
// 获取一个 FindState 用来存储订阅者信息,里面使用 FIND_STATE_POOL 池的缓存 FindState
FindState findState = prepareFindState();
// 对FindState 初始化
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
// 设置订阅者信息,用户将注册的方法提前保存在 SubscriberInfo 中
// 简单点讲,自己麻利滴 将这个类及父类需要响应的方法列表列出来,我就不一一去找了,
// 通过EventBusBuilder addIndex设置,不过基本上很少用到
findState.subscriberInfo = getSubscriberInfo(findState);
if (findState.subscriberInfo != null) {
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
// 通过反射查找方法,并且去除非 响应事件的方法
findUsingReflectionInSingleClass(findState);
}
// 移动到父类
findState.moveToSuperclass();
}
// 缓存并返回
return getMethodsAndRelease(findState);
}
-
findUsingReflectionInSingleClass 通过反射获取方法,获取Eventbus 的响应方法后封装成一个 SubscriberMethod 并添加进 findState.subscriberMethods 集合中。
SubscriberMethod 参数:method 目标方法,eventType 事件类型,threadMode 响应的线程, priority 优先级,sticky 是否是粘性事件
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
// 只返回当前 class 的方法
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();
//订阅的方法 must be public, non-static, and non-abstract
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
// 参数集合
Class<?>[] parameterTypes = method.getParameterTypes();
// 参数至少要一个 就是我们的 定义的类 如 FirstBean
if (parameterTypes.length == 1) {
// 再获取自定义注解
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
// 事件类型 如 FirstBean
Class<?> eventType = parameterTypes[0];
// 对方法检查方法签名
if (findState.checkAdd(method, eventType)) {
// 获取 ThreadMode
ThreadMode threadMode = subscribeAnnotation.threadMode();
// 封装成 SubscriberMethod 保存
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");
}
}
}
- getMethodsAndRelease step 3 中 最后 要将 FindState 缓存到 FIND_STATE_POOL 中 后续中就可以直接获取,避免重新new 对象
private List<SubscriberMethod> getMethodsAndRelease(FindState findState) {
// 之前保存的所有 订阅的方法
List<SubscriberMethod> subscriberMethods = new ArrayList<>(findState.subscriberMethods);
findState.recycle();
synchronized (FIND_STATE_POOL) {
// 放进 FIND_STATE_POOL 缓存里,但是只能缓存4个
for (int i = 0; i < POOL_SIZE; i++) {
if (FIND_STATE_POOL[i] == null) {
FIND_STATE_POOL[i] = findState;
break;
}
}
}
return subscriberMethods;
}
- OK 方法找了,并且知道方法在哪个线程响应,优先级等属性,但是并没有关联到具体哪个类。step 1 在同步代码块中 循环每一个subscriberMethod 开始关联
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
// 获取类里面的方法集合
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
- subscribe 方法较长,先分析前半部分,将 订阅者 和 订阅的方法 封装成 Subscription 根据优先级放入 CopyOnWriteArrayList 然后以 key = 事件类型,value = CopyOnWriteArrayList 存入subscriptionsByEventType 这个Map集合中。当post 发送数据时,就能根据事件类型 从这个Map 取出哪些类的哪些方法需要回调。
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
// 获取响应方法的 事件类型
Class<?> eventType = subscriberMethod.eventType;
// 将订阅者(响应的类) 和 每一个响应方法 封装成 Subscription
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
// 根据事件类型 先获取响应这个事件的集合 集合元素就是上面封装的 Subscription
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
// 同一事件类型的 读写集合集合 key 事件类型;value 封装的 Subscription
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) {
// 根据优先级 将封装 Subscription 对象 放进集合
subscriptions.add(i, newSubscription);
break;
}
}
... 省略后半 ...
}
- subscribe 后段 typesBySubscriber 同样是Map,key = 订阅者(Activity) value = List(Activity订阅的所有事件的集合),当然是留给取消注册的时候用啦。
粘性事件,就是在订阅者订阅之前 已经产生了内容,如果在注册方法时设置成粘性,那么之前产生的内容会直接进入响应。
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
.. 省略前段...
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);
}
}
}
- OK 数据订阅完成,下面进入数据发送 post
2. post
- ThreadLocal 可以存储一个对象,这个对象可以理解为 线程本地变量与线程绑定 和Handler中 用来保存 Looper 以及 稍微了解下ThreadLocal.ThreadLocalMap 应该就能明白他的功能了。
- post,首先取出线程本地变量 PostingThreadState, 然后将事件添加到队列中,判断当前线程是否是主线程 也保存在PostingThreadState 中,其他参数就不一一介绍了。
public void post(Object event) {
// ThreadLocal 线程本地变量,每次get 获取当前线程 设置的 PostingThreadState对象
PostingThreadState postingState = currentPostingThreadState.get();
// PostingThreadState 对象中保存当前线程的执行队列
List<Object> eventQueue = postingState.eventQueue;
// 将事件 加入队列中
eventQueue.add(event);
// 没有正在发送数据
if (!postingState.isPosting) {
// 获取当前线程 赋值给 postingState
postingState.isMainThread = isMainThread();
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;
}
}
}
- postSingleEvent eventInheritance 默认 false,在postSingleEventForEventType中具体分发事件
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 {
// 默认 eventInheritance false,
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));
}
}
}
- postSingleEventForEventType 根据 事件类型 取出所有订阅此事件的集合,通过 postToSubscription分发
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> 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 根据注册方法时 指定的回调线程,选择回调的线程
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);
}
}
- invokeSubscriber 取出subscription 中保存的 订阅者,和订阅方法进行回调。到这里基本完成回调。
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);
}
}
- MAIN_ORDERED 回调方式 mainThreadPoster 是一个HandlerPoster 对象,继承了Handler,里面封装了一个队列 PendingPostQueue,有点类似 MessageQueue,值得学习。
if (mainThreadPoster != null) {// 在队列中有序执行
mainThreadPoster.enqueue(subscription, event);
} else {
// temporary: technically not correct as poster not decoupled from subscriber
invokeSubscriber(subscription, event);
}
- backgroundPoster和 asyncPoster 都实现了 Runnable 放在线程池中异步调用。
3. unregister
- unregister 就是删除集合中保存的 订阅者信息
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) {
// 根据事件类型 获取所有订阅封装类 Subscription
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--;
}
}
}
}
总结
- 重新学习了 ThreadLocal
- 对“池”的使用,给我提供了灵感,解决项目中内存抖动的问题,之前Message okhttp连接池等都没有想到在项目中使用,
- PendingPostQueue 虽然封装的简单,但确实更适合借鉴,