不忘初心 砥砺前行, Tomorrow Is Another Day !html
本文概要:java
Android中消息传递有多种方式.android
当咱们须要在多个地方接收事件通知,此时接口回调过于繁琐;广播又显得资源浪费.这时就须要用到EventBus了. EventBus是一种基于观察者模式,下降组件之间耦合,简化通讯的方式.
官方介绍地址: github.com/greenrobot/…git
EventBus的三剑客 :github
事件由发布者经过EvenentBus传递给订阅者.缓存
EventBus的四模型 :bash
在3.0以前只能固定的方法来指定线程模型:app
public class DataEvent{
String data;
public DataEvent(String data){
this.data = data;
}
}
复制代码
EventBus.getDefault().register(this);
复制代码
//发布普通事件
EventBus.getDefault().post(new DataEvent());
//发布黏性事件
EventBus.getDefault().postSticky(new DataEvent());
复制代码
//指定线程模型和优先级
@Subscriber (threadMode = ThreadMode.MAIN,priority = 0)
public void onDataEvent(DataEvent dataEvent){
}
复制代码
EventBus.getDefault().unregister(this); 
复制代码
上述示例中,使用了黏性事件和优先级. 接下来看二者概念.async
EventBus的原理主要理解注册-发布事件-反注册流程三个方面.ide
一样EventBus也定义了咱们须要用到的注解.
@Documented
@Retention(RetentionPolicy.RUNTIME)//运行时注解
@Target({ElementType.METHOD})//修饰方法
public @interface Subscribe {
//定义了三个参数
ThreadMode threadMode() default ThreadMode.POSTING;
boolean sticky() default false;
int priority() default 0;
}
复制代码
先初始化:使用单例模式双重检查DCL方式建立EventBus对象.
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;
}
复制代码
初始化完成后,就须要调用register方法进行注册.
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
//获取当前订阅者全部订阅方法(findSubscriberMethods)
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
//遍历全部方法,将他们保存起来(经过subscribe保存).
subscribe(subscriber, subscriberMethod);
}
}
}
复制代码
接着看如何查到全部订阅方法的
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
//若是有缓存,则直接返回
if (subscriberMethods != null) {
return subscriberMethods;
}
//无缓存,则经过反射活编译时生成的代码找到订阅方法集合(具体实现稍后在分析,先了解到这就行)
if (ignoreGeneratedIndex) {
//反射获取
subscriberMethods = findUsingReflection(subscriberClass);
} else {
//3.0开启索引加速
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;
}
}
复制代码
当找到全部订阅方法后,会保存订阅方法.咱们看保存的具体过程
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<>();
//核心map,事件类型对应的Subscription订阅信息.
//保存在subscriptionsByEventType(Map,key:eventType(事件类型的class对象);
// value:CopyOnWriteArrayList<Subscription> (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;
}
}
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
//次核心map,订阅者对应的全部事件类型
// 保存在typesBySubscriber(Map, key:subscriber(订阅者); value:List<Class<?>>(事件类型的class对象集合))
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);
}
}
}
复制代码
注册的时候核心是对核心Map和次核心Map,进行添加操做.那么反注册则是删除操做.
public synchronized void unregister(Object subscriber) {
//从次核心Map取出全部事件类型
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
//从核心Map,移除订阅信息
unsubscribeByEventType(subscriber, eventType);
}
//从次核心Map,移除订阅者
typesBySubscriber.remove(subscriber);
} else {
logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
//从核心Map,移除订阅信息的具体过程
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有很大关联,接着看发布事件流程.
因为Post方法内部调用了postSingleEvent来,其内部最终调用了postSingleEventForEventType发送事件,这里直接看该方法.
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
//从核心Map,根据事件类型找到全部订阅信息
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);
}
}
复制代码
最后咱们对整个EventBus流程作一个总结.
准备工做:
先初始化,使用单例模式双重检查DCL方式建立EventBus对象.
注册:
获取当前订阅者全部订阅方法(findSubscriberMethods)
- 若是有缓存(METHOD_CACHE),则直接返回; 无缓存,则经过++反射或者编译时生成的代码++来找到订阅方法集合,而后缓存起来返回.
遍历全部方法,将他们保存起来(经过subscribe保存).
- 组合订阅信息,Subscription(订阅信息) = Subscriber(订阅者) + SubscriberMethods(订阅方法)
- 保存在事件类型对应的订阅信息集合的Map中.(核心Map)
- ++保存在订阅者对应的全部事件类型(次核心Map)++
反注册:
- 根据当前订阅者,从次核心Map找到全部订阅事件类型.
- 遍历根据订阅事件,从核心Map找到对应全部订阅信息.
- 最后一次移除订阅信息,订阅者.
发布事件
- 根据当前事件类型,从核心Map找到全部订阅信息,遍历调用.
以前在讲解EventBus的注册过程时,只是简单得讲到获取全部的订阅方法,若是没有缓存时,则从反射或者编译时注解生成的代码中获取.接下来继续看反射相关代码
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");
}
}
}
复制代码
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
//获取订阅信息(getSubscriberInfo方法在编译时索引类里)
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);
}
复制代码
到此为止,咱们知道3.0以前经过反射获取订阅的方法,对于性能上是有所欠缺.因此在3.0后提供了(Subscribe Index)索引加速,其实本质就是注解处理器的应用,这样就不用经过反射了,性能上获得了很大的提升.
接下来看如何开启索引. greenrobot.org/eventbus/do…
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
//指定生成的索引文件名以及日志打印
arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex', verbose : 'true' ]
}
}
}
}
dependencies {
implementation 'org.greenrobot:eventbus:3.1.1'
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}
复制代码
最后咱们在MainActivity准备一些订阅方法方法
@Subscribe(threadMode = ThreadMode.MAIN, priority = 0)
public void onDataEventOne(Integer dataEvent) {
}
@Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
public void onDataEventTwo(String dataEvent) {
}
复制代码
接着编译下项目,此时在build-generated-source-apt的包下,经过注解处理器生成了咱们本身定义的索引文件MyEventBusIndex.java
public class MyEventBusIndex implements SubscriberInfoIndex {
private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;
static {
//索引Map,key为订阅者,value为订阅者信息
SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();
//添加订阅者信息到Map中
putIndex(
//
new SimpleSubscriberInfo(org.jasonhww.eventbusdemo.MainActivity.class, true,
new SubscriberMethodInfo[] {
//建立订阅方法对象
new SubscriberMethodInfo("onDataEventOne", Integer.class, ThreadMode.MAIN),
new SubscriberMethodInfo("onDataEventTwo", String.class, ThreadMode.MAIN, 1, false),
}));
}
//将订阅信息添加到索引Map中
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时添加咱们配置好的索引.
//方法一,使用自定义的EventBus
EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
//方法二,使用默认的EventBus,大多数状况下采起这种就行.
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
EventBus eventBus = EventBus.getDefault();
复制代码
在以前分析编译时注解和ButterKnife,都有讲到注解处理器的运用.一样咱们依葫芦画瓢来看EventBus是如何利用注解处理器去生成咱们的索引类的.
相比以前分析的,这里注解处理器类上多了一个@SupportedOptions(value = {"eventBusIndex", "verbose"})配置,这里对应了最开始gradle的配置.
@SupportedAnnotationTypes("org.greenrobot.eventbus.Subscribe")
@SupportedOptions(value = {"eventBusIndex", "verbose"})
复制代码
这里直接看注解处理器process方法里,最重要的两步.
待生成的java文件信息存储在methodsByClass中.
private void collectSubscribers(Set<? extends TypeElement> annotations, RoundEnvironment env, Messager messager) {
for (TypeElement annotation : annotations) {//遍历全部注解类对应的TypeElement
//获取被注解的元素(如MainActivity的onDataEventOne方法对应的元素对象)
Set<? extends Element> elements = env.getElementsAnnotatedWith(annotation);
for (Element element : elements) {
if (element instanceof ExecutableElement) {
ExecutableElement method = (ExecutableElement) element;
if (checkHasNoErrors(method, messager)) {
//获取被注解所在的类
TypeElement classElement = (TypeElement) method.getEnclosingElement();
//存入Map中,生成文件时调用.
methodsByClass.putElement(classElement, method);
}
} else {
messager.printMessage(Diagnostic.Kind.ERROR, "@Subscribe is only valid for methods", element);
}
}
}
}
复制代码
private void createInfoIndexFile(String index) {
BufferedWriter writer = null;
try {
JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(index);
int period = index.lastIndexOf('.');
String myPackage = period > 0 ? index.substring(0, period) : null;
String clazz = index.substring(period + 1);
writer = new BufferedWriter(sourceFile.openWriter());
if (myPackage != null) {
writer.write("package " + myPackage + ";\n\n");
}
writer.write("import org.greenrobot.eventbus.meta.SimpleSubscriberInfo;\n");
writer.write("import org.greenrobot.eventbus.meta.SubscriberMethodInfo;\n");
writer.write("import org.greenrobot.eventbus.meta.SubscriberInfo;\n");
writer.write("import org.greenrobot.eventbus.meta.SubscriberInfoIndex;\n\n");
writer.write("import org.greenrobot.eventbus.ThreadMode;\n\n");
writer.write("import java.util.HashMap;\n");
writer.write("import java.util.Map;\n\n");
writer.write("/** This class is generated by EventBus, do not edit. */\n");
writer.write("public class " + clazz + " implements SubscriberInfoIndex {\n");
writer.write(" private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;\n\n");
writer.write(" static {\n");
writer.write(" SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();\n\n");
//此里面调用收集信息时存入的Map(methodsByClass)
writeIndexLines(writer, myPackage);
writer.write(" }\n\n");
writer.write(" private static void putIndex(SubscriberInfo info) {\n");
writer.write(" SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);\n");
writer.write(" }\n\n");
writer.write(" @Override\n");
writer.write(" public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {\n");
writer.write(" SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);\n");
writer.write(" if (info != null) {\n");
writer.write(" return info;\n");
writer.write(" } else {\n");
writer.write(" return null;\n");
writer.write(" }\n");
writer.write(" }\n");
writer.write("}\n");
} catch (IOException e) {
throw new RuntimeException("Could not write source for " + index, e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
//Silent
}
}
}
}
复制代码
这样就成功的将订阅者的全部订阅方法都保存在了索引文件了,这样最终注册的时候就不须要再利用反射区查找订阅方法了.
最后仍是老规矩作一个小的总结.
所谓索引加速,就是利用注解处理器经过解析注解信息,生成java文件的索引类,将全部订阅的方法保存在索引类而已.
因为本人技术有限,若有错误的地方,麻烦你们给我提出来,本人不胜感激,你们一块儿学习进步.
参考连接: