1. 粘性事件
粘性事件就是在发送事件之后再订阅该事件也能收到该事件,跟黏性广播类似。
首先看下粘性事件的发布方式:
EventBus.getDefault().postSticky("hello, eventbus!");
我们看下发布粘性事件方法的源码
private final Map<Class<?>, Object> stickyEvents;
public void postSticky(Object event) {synchronized (stickyEvents) {
// 先将事件缓存在 stickyEvents 中
// stickyEvents 是一个 Map,key 是事件类型,value 是事件
stickyEvents.put(event.getClass(), event);
}
// Should be posted after it is putted, in case the subscriber wants to remove immediately
// 然后把事件发布出去,如果没有注册,不会执行订阅事件方法
post(event);
}
postSticky 方法主要做了两件事情,先是把事件缓存在 stickyEvents 中,然后通过 post 方法把事件发布出去,这个方法之前已经分析过,这里不在分析了。
发布完粘性事件后,当订阅粘性事件方法,完成注册后就会立即执行。核心的注册流程是之前分析的 register 方法,其中 subscribe 方法一段代码就是处理粘性事件
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
// ......
// 如果在订阅事件方法时,@Subscribe 注解中的 sticky 设置为 true,代表粘性事件
if (subscriberMethod.sticky) {
// eventInheritance 默认为 true,代表继承关系事件也会被执行
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);
}
}
}
可以看出,处理粘性事件是在注册时,遍历 stickyEvents,然后交给 checkPostStickyEventToSubscription 处理
private void checkPostStickyEventToSubscription(Subscription newSubscription, Object stickyEvent) {if (stickyEvent != null) {// If the subscriber is trying to abort the event, it will fail (event is not tracked in posting state)
// --> Strange corner case, which we don't take care of here.
postToSubscription(newSubscription, stickyEvent, isMainThread());
}
}
checkPostStickyEventToSubscription 方法最终调用线程切换方法 postToSubscription,由它完成事件的处理。
2.Subscriber Index
前面分析 EventBus 注册时,默认实现是通过反射技术查找注册类中所有的订阅事件的方法,如果当前注册类中有很多订阅事件方法,通过反射技术就会影响运行时性能。所以在 EventBus3.0 通过 APT(注解处理器) 技术在编译器就查找所有订阅事件方法,生成一个辅助的索引 SUBSCRIBER_INDEX 保存所有的订阅方法。
要在项目编译时查找订阅事件的方法信息,首先要在 app 的 build.gradle 中加入如下配置:
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
// APT 设置的参数,指定辅助索引类名和包名
arguments = [eventBusIndex : 'com.example.myapp.MyEventBusIndex']
}
}
}
}
dependencies {
implementation 'org.greenrobot:eventbus:3.1.1'
// 引入注解处理器
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}
然后在项目的 Application 中添加如下配置,以生成一个默认的 EventBus 单例:
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
EventBus eventBus = EventBus.getDefault();
编译后,就会生成 MyEventBusIndex 类,源码如下:
public class MyEventBusIndex implements SubscriberInfoIndex {
private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;
static {SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();
putIndex(new SimpleSubscriberInfo(com.example.eventbus.demo.MainActivity.class, true,
new SubscriberMethodInfo[] {
new SubscriberMethodInfo("onMessageEventMain", com.example.eventbus.demo.MessageEvent.class,
ThreadMode.MAIN, 0, true),
}));
putIndex(new SimpleSubscriberInfo(com.example.eventbus.demo.TestActivity.class, true,
new SubscriberMethodInfo[] {
new SubscriberMethodInfo("onMessageEventMainForTest", com.example.eventbus.demo.MessageEvent.class,
ThreadMode.MAIN, 0, true),
}));
}
private static void putIndex(SubscriberInfo info) {SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
}
@Override
public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
if (info != null) {return info;} else {return null;}
}
}
其中 SUBSCRIBER_INDEX 是一个 Map,保存当前注册类的 Class 和订阅事件方法信息。
我们先从使用 MyEventBusIndex 这个索引类方式入手
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
调用 EventBus 的 builder 方法创建一个 EventBusBuilder 对象,然后调用它的 addIndex 方法,添加索引类
public EventBusBuilder addIndex(SubscriberInfoIndex index) {if (subscriberInfoIndexes == null) {subscriberInfoIndexes = new ArrayList<>();
}
subscriberInfoIndexes.add(index);
return this;
}
从上面代码可以看出,把生成的索引类添加到 subscriberInfoIndexes 集合中,然后调用 installDefaultEventBus 方法,创建默认的 EventBus 实例
public EventBus installDefaultEventBus() {synchronized (EventBus.class) {if (EventBus.defaultInstance != null) {
throw new EventBusException("Default instance already exists." +
"It may be only set once before it's used the first time to ensure consistent behavior.");
}
EventBus.defaultInstance = build();
return EventBus.defaultInstance;
}
}
public EventBus build() {
// this 代表是 EventBusBuilder 实例,这样 EventBus 就可以拿到 subscriberInfoIndexes 集合
return new EventBus(this);
}
其中 subscriberInfoIndexes 集合通过 EventBus 构造传给 EventBus
EventBus(EventBusBuilder builder) {
// ...
indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
builder.strictMethodVerification, builder.ignoreGeneratedIndex);
// ...
}
之前在分析注册流程的时,其中 findUsingInfo 方法就会处理这个索引类,我们在来看下源码
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
// FindState
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {// 如果没有使用 APT( 注解处理器) 生成订阅方法索引,返回 null,则进入 else 语句中
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);
}
// 从父类中继续查找,直到父类为 null
findState.moveToSuperclass();}
// 返回注册类中所有的订阅方法,并释放 findState 中状态,同时把 findState 对象放回缓存池中
return getMethodsAndRelease(findState);
}
当我们使用 APT 生成了并使用了索引类的时,就不会通过反射技术查找注册类中所有的订阅事件方法了。
private SubscriberInfo getSubscriberInfo(FindState findState) {if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
if (findState.clazz == superclassInfo.getSubscriberClass()) {return superclassInfo;}
}
// subscriberInfoIndexes 就是通过 addIndex 方法添加索引类集合
if (subscriberInfoIndexes != null) {for (SubscriberInfoIndex index : subscriberInfoIndexes) {SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
if (info != null) {return info;}
}
}
return null;
}
subscriberInfoIndexes 集合就是通过 addIndex 方法创建的,并保存了索引类实例 MyEventBusIndex。
使用 Subscriber Index,避免了通过反射技术处理,提高了性能。
3.AsyncExecutor
AsyncExecutor 与线程池类似,但是可以处理运行异常。
public static AsyncExecutor create() {return new Builder().build();}
通过 AsyncExecutor 静态方法 create 可以创建一个 AsyncExecutor 实例对象,然后调用它的 execute 方法执行任务
public void execute(final RunnableEx runnable) {threadPool.execute(new Runnable() {
@Override
public void run() {
try {runnable.run();
} catch (Exception e) {
Object event;
try {event = failureEventConstructor.newInstance(e);
} catch (Exception e1) {eventBus.getLogger().log(Level.SEVERE, "Original exception:", e);
throw new RuntimeException("Could not create failure event", e1);
}
if (event instanceof HasExecutionScope) {((HasExecutionScope) event).setExecutionScope(scope);
}
eventBus.post(event);
}
}
});
}
当发生异常时,会发布 ThrowableFailureEvent 事件,我们可以订阅该事件进行相应的处理。execute 方法接收的参数是 RunnableEx 对象
public interface RunnableEx {void run() throws Exception;
}
RunnableEx 中的 run 方法是可以抛出异常的。
我们可以根据 AsyncExecutor 另一个静态方法 builder,修改默认的线程池 threadPool,失败的事件类型 failureEventType
// AsyncExecutor.java 源码
public static Builder builder() {return new Builder();
}
public static class Builder {
private Executor threadPool;
private Class<?> failureEventType;
public Builder threadPool(Executor threadPool) {
this.threadPool = threadPool;
return this;
}
public Builder failureEventType(Class<?> failureEventType) {
this.failureEventType = failureEventType;
return this;
}
}
EventBus 到此所有的源码已经分析完了~~~