image.png
序言
EventBus是一个Android事件发布/订阅框架,通过解耦发布者和订阅者简化事件传递。事件传递可用在四大组件/业务帮助类中使用,也可在主线程和子线程中使用,功能十分强大。下面来介绍一下EventBus的使用及原理。
使用EventBus
EventBus的使用方法很简单,首先在build.gradle中配置EventBus依赖库:
implementation 'org.greenrobot:eventbus:3.0.0'
然后在Application的onCreate方法中设置EventBus索引:
//应用 EventBus 索引
EventBus.builder().addIndex(new MyEventBusIndex())
.strictMethodVerification(!BuildConfig.DEBUG).
installDefaultEventBus();
如果某个Activity需要接收EventBus事件,就需要在onCreate方法中注册到EventBus,在onDestroy方法中取消注册:
@Override
protected void onCreate(Bundle savedInstanceState) {
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
}
Activity中除了注册和取消注册EventBus,还需要指明接收哪种类型的事件:
@Subscribe(threadMode = ThreadMode.MAIN)
fun onEventMainThread(event: LoginEvent) {
//do something
}
之后只需要在其他地方发送对应类型的事件,该Activity就能处理这个事件了:
public void someWhere() {
//发送登录成功事件
EventBus.getDefault().post(new LoginEvent(LoginEvent.EVENT_ID_LOGIN));
}
整体来说,EventBus的使用过程很简单,有几点需要注意的:
-
粘性事件:EventBus支持粘性事件,只需要在@Subscribe注解中添加
sticky = true
,然后在发送事件的地方调用postSticky
方法即可。粘性事件和普通事件的区别在于:在订阅者A注册之前,EventBus发送了普通事件C和粘性事件S,那么A注册成功之后,可以立刻接收到S,而不能接收C。之后的普通事件和粘性事件都可以正常接收。 - ThreadMode:决定在哪个线程中处理事件,默认是POSTING线程,实际情况大多数情况是主线程
- 订阅者:做项目的过程中,订阅者多数是Activity或者Fragment,但实际上订阅者可以是任何类型的对象
EventBus原理
1. EventBus初始化
上面介绍了EventBus使用方法,下面就来介绍一下EventBus的原理,首先我们看一下EventBus的初始化过程,如果需要使用EventBus索引,那么就是在Application中配置EventBus索引,并且初始化EventBus:
EventBus.builder().addIndex(new MyEventBusIndex())
.strictMethodVerification(!BuildConfig.DEBUG).
installDefaultEventBus();
EventBus采用了Builder模式,构建EventBus对象时往EventBusBuilder中添加MyEventBusIndex索引,接下来看一下MyEventBusIndex类:
/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;
static {
SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();
putIndex(new SimpleSubscriberInfo(com.bs.trade.financial.view.fragment.PositionRevenueFragment.class, true,
new SubscriberMethodInfo[] {
new SubscriberMethodInfo("onEventMainThread", com.bs.trade.financial.event.FinancialPositionEvent.class,
ThreadMode.MAIN),
new SubscriberMethodInfo("onScrollToTopEvent", com.bs.trade.financial.helper.event.ScrollToTop.class,
ThreadMode.MAIN),
}));
//此处省略一大波代码,跟上面的代码类似
//...
}
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;
}
}
}
这个类是编译过程中根据注解自动生成的类,包含了项目中所有EventBus订阅者,并且实现了SubscriberInfoIndex接口,提供getSubscriberInfo方法来获取某个订阅者可接收事件的方法信息。
添加了MyEventBusIndex索引后,紧接着调用EventBusBuilder.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() {
return new EventBus(this);
}
EventBus也采用了单例模式,这里调用build方法创建了这个单例对象,看一下EventBus的构造方法:
EventBus(EventBusBuilder builder) {
subscriptionsByEventType = new HashMap<>();
typesBySubscriber = new HashMap<>();
stickyEvents = new ConcurrentHashMap<>();
mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);
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;
}
构造方法中初始化了很多成员变量,其中有些成员很重要,后面我们会提到的。
创建了EventBus单例对象,提供给外部使用,只需要调用EventBus.getDefault来获取该对象:
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
2. 订阅者注册
上面分析了EventBus的初始化过程,接下来看一下,订阅者怎么注册到EventBus中:
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
首先会通过SubscriberMethodFinder来找到订阅者中接收事件的方法(就是使用用注解@Subscriber修饰的方法),然后循环调用subscribe(subscriber, subscriberMethod)方法:
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);
}
}
}
这个方法比较长,但做的事情很简单,就是把订阅者、接收事件的方法、事件类型分类保存起来,主要是保存在下面几个成员变量中:
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
private final Map<Object, List<Class<?>>> typesBySubscriber;
private final Map<Class<?>, Object> stickyEvents;
简单说明一下这几个变量:
-
subscriptionsByEventType
: 保存EventType对应的Subscription对象(Subscription对象包含两个变量subscriber和subscriberMethod)。 -
typesBySubscriber
:保存EventType对应的订阅者 -
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, Looper.getMainLooper() == Looper.myLooper());
}
}
然后调用postToSubscription方法,这个方法下面会介绍,主要作用是发送事件。
总结一下对粘性事件的处理:检查在订阅者注册之前是否发送过该类事件的粘性事件,如果有的话就会单独给这个注册者重新发送一次,该注册者就可以接受到该粘性事件,上面介绍的粘性事件的特性,跟代码逻辑正好吻合。
再看回register方法,前面提到过该方法中会通过SubscriberMethodFinder来找到订阅者的接收事件方法,那么看看这个找的过程是什么样的。首先,subscriberMethodFinder对象是在EventBus的构造方法中初始化:
subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
builder.strictMethodVerification, builder.ignoreGeneratedIndex);
接着看一下SubscriberMethodFinder的构造方法:
SubscriberMethodFinder(List<SubscriberInfoIndex> subscriberInfoIndexes, boolean strictMethodVerification,
boolean ignoreGeneratedIndex) {
this.subscriberInfoIndexes = subscriberInfoIndexes;
this.strictMethodVerification = strictMethodVerification;
this.ignoreGeneratedIndex = ignoreGeneratedIndex;
}
很简单,就是初始化几个成员变量:
-
subscriberInfoIndexes
:是构造EventBus对象时配置的MyEventBusIndex对象 -
strictMethodVerification
:也是构造EventBus对象时配置的一个参数(Debug模式为false,Release模式为true) -
ignoreGeneratedIndex
:默认值为false
SubscriberMethodFinder会调用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;
}
}
上面说过ignoreGeneratedIndex的值为false,所以会调用findUsingInfo方法:
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
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);
}
这里出现了一个FindState类,它是SubscriberMethodFinder的静态内部类,作用是保存当前查找的订阅者的信息,定义如下:
static class FindState {
final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
final Map<Class, Object> anyMethodByEventType = new HashMap<>();
final Map<String, Class> subscriberClassByMethodKey = new HashMap<>();
final StringBuilder methodKeyBuilder = new StringBuilder(128);
Class<?> subscriberClass;
Class<?> clazz;
boolean skipSuperClasses;
SubscriberInfo subscriberInfo;
//省略一些方法
}
SubscriberMethodFinder中使用了一个FindState池,类似于线程池,需要使用的时候从池中取出一个FindState来使用,用完之后释放资源,重新回到池中。目的是为了防止创建过多的FindState对象而导致资源浪费:
private static final FindState[] FIND_STATE_POOL = new FindState[POOL_SIZE];
怎样使用FindState去找subscriber事件方法的细节这里就不介绍,代码逻辑是从索引中找到对应的订阅者的接收事件方法,感兴趣的同学可以自行去看代码。
到此,总结一下订阅者注册的过程:订阅者注册到EventBus,EventBus通过MyEventBusIndex找到订阅者接收事件的方法信息,保存起来。如果有接收sticky事件的方法,就会查找之前是否发送过这种sticky事件,如果发送过,就会重新单独给这个订阅者发送一次。
3. 发送事件
接下来,分析一下发送事件的过程,以及EventBus如何把事件分发给订阅者。我们通常调用EventBus的post方法来发送事件:
public void post(Object event) {
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
if (!postingState.isPosting) {
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;
}
}
}
方法中涉及到一个PostingThreadState类型的对象currentPostingThreadState,我们看一下它的定义:
private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
@Override
protected PostingThreadState initialValue() {
return new PostingThreadState();
}
};
它是一个ThreadLocal对象,Android消息机制一文中介绍过该类,它可以区分线程来保存对象。由于EventBus是在主线程中初始化的,所以此处创建了一个主线程的PostingThreadState对象。下面看看PostingThreadState的定义:
final static class PostingThreadState {
final List<Object> eventQueue = new ArrayList<Object>();
boolean isPosting;
boolean isMainThread;
Subscription subscription;
Object event;
boolean canceled;
}
它是用来保存不同线程中的事件队列和发送状态。让我们回到post方法,继续分析发送事件的过程。
代码逻辑是把事件添加到PostingThreadState的队列中,之后调用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));
}
}
}
着重看postSingleEventForEventType方法:
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方法,介绍sticky事件时提到过的:
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);
}
}
通常,多数情况下,我们都是在主线程中接收并处理事件,所以就分析一下MAIN分支:
- 当前线程是主线程:直接通过反射调用订阅者接收对应事件的方法
- 当前线程非主线程:通过mainThreadPoster来发送事件,至于mainThreadPoster是个什么东西,相信大家都能猜出来。没错,它就是一个Handler对象,用来切换线程(哪里有线程切换,哪里就有Handler)。
我们就来验证一下猜想是否正确:
private final HandlerPoster mainThreadPoster;
//EventBus构造方法中初始化
mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);
接着看HandlerPoster定义:
final class HandlerPoster extends Handler {
private final PendingPostQueue queue;
private final int maxMillisInsideHandleMessage;
private final EventBus eventBus;
private boolean handlerActive;
HandlerPoster(EventBus eventBus, Looper looper, int maxMillisInsideHandleMessage) {
super(looper);
this.eventBus = eventBus;
this.maxMillisInsideHandleMessage = maxMillisInsideHandleMessage;
queue = new PendingPostQueue();
}
void enqueue(Subscription subscription, Object event) {
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
queue.enqueue(pendingPost);
if (!handlerActive) {
handlerActive = true;
if (!sendMessage(obtainMessage())) {
throw new EventBusException("Could not send handler message");
}
}
}
}
@Override
public void handleMessage(Message msg) {
boolean rescheduled = false;
try {
long started = SystemClock.uptimeMillis();
while (true) {
PendingPost pendingPost = queue.poll();
if (pendingPost == null) {
synchronized (this) {
// Check again, this time in synchronized
pendingPost = queue.poll();
if (pendingPost == null) {
handlerActive = false;
return;
}
}
}
eventBus.invokeSubscriber(pendingPost);
long timeInMethod = SystemClock.uptimeMillis() - started;
if (timeInMethod >= maxMillisInsideHandleMessage) {
if (!sendMessage(obtainMessage())) {
throw new EventBusException("Could not send handler message");
}
rescheduled = true;
return;
}
}
} finally {
handlerActive = rescheduled;
}
}
}
果然是Handler,它在主线程中初始化(只能处理主线程的消息),在enqueue方法中发送一个PendingPost消息,然后切换到主线程,在handlerMessage中处理该消息,也是调用EventBus的invokeSubscriber方法,就会回调订阅者的事件接收方法。
到这里我们就分析完了发送事件和处理事件的过程。
总结
EvnetBus是一个事件发布/订阅框架,订阅者注册的时候会保存订阅者、接收事件的方法及事件类型。发送事件就会找到对应的订阅者和接收事件的方法,回调这些方法。订阅者就可以接收到事件了。
EventBus很好的解耦了事件的订阅者和发送者,在实际项目中有广泛的应用。
对EventBus还不了解的同学,赶紧动手用起来!!
网友评论