现创建了一个Android开发水友圈,圈内会不定时更新一些Android中高级的进阶资料,欢迎大家带着技术问题来讨论,共同成长进步!(包含资深UI工程师,Android底层开发工程师,Android架构师,原生性能优化及混合优化,flutter专精);希望有技术的大佬加入,水圈内解决的问题越多获得的权利越大!
需要源码的直接点击文末链接获取,本文较长建议收藏后食用
eventBus的优点缺点
- 优点
- List item
- 简单统一数据传递
- 清晰明了的主次线程
- 使用class传递数据(是的,最好用的地方用一个class来传递数据,这下传一个class,就可以携带各种各样的数据了,摆脱了用Bundle传递list和数组简直太爽了
- 在activity与activity,或者Service与activity传递大数据时的唯一选择。因为序列化大数据进行传递时,是十分耗时缓慢的。用EventBus是最优解法。
缺点
- 滥用它,EventBus可以大量解耦项目,但是如果你大量的使用它会产生一个非常危险的后果,你需要定义大量的常量或者新的实体类来区分接收者。管理EventBus的消息类别将会你的痛苦
- 在非前台组件中使用它,不只在Activity或者Service,广播里使用它。 而是将它使用到每一个工具类 或者
后台业务类,除了让数据发送与接收更加复杂。别忘记了Java本身就是面对对象语言,它有接口、抽象可以实现来发送与接收数据。你可以用各种设计模式,比如观察者模式,来更好的优化与独立自己的业务。不需要依赖EventBus。 - EventBus,并不是真正的解耦。请不要在你独立的模块里使用EventBus来分发。你这个模块如果那天要直接放入另外一个项目里,你怎么解耦EventBus?最好,还是多使用接口与Activity本身的数据传递方式。
liveData
- 当然,eventBus存在的时间已经很长了,现在呢,也慢慢的落伍了. 像Android最新提供的LiveData,还有rxBus等等
- LiveData
的支持注册监听,非常适合Android刷新机制。无序手动解除注册,无内存泄漏问题,因为有LifeCycle帮我们做生命周期的监听 - observe模式,拥有生命周期,在界面可见的时候才会触发回调,保证UI更新的是最新数据。
- observeForever:该模式就是一直监听,只要数据源变化都会回调,无生命周期。跟EventBus和RxBus一样
05 事件总线的定义
- 事件总线这个概念对你来说可能很陌生,但提到观察者(发布-订阅)模式,你也许就很熟悉。事件总线是对发布-订阅模式的一种实现。它是一种集中式事件处理机制,允许不同的组件之间进行彼此通信而又不需要相互依赖,达到一种解耦的目的。
- 举个栗子:就比如说我们的饭店,以前是需要出去发广告,当面拉客. 现在是什么呢?
现在就在美团等网站上去注册这么一个xxx小炒的店,客户呢,根据美团上这个店去下单付款,美团呢,派棋手给我们送到家,这样就让我们的饭店和客户完全达到无接触但完成交易.
总的来说就是解耦嘛.
06 实现事件总线技术的三大核心步骤
- Event:事件可以是任意类型的对象
- 自定义对象:userInfo 或者 OrderInfo等等
- Subscriber:事件订阅者,事件处理的方法名可以自定义
- 需要在方法上添加注解@Subscriber,并指定线程模式(有默认值也可自定义)
- Publisher:事件发布者,可以在任意线程任意位置发送事件
- 根据post方法的参数类型,会自动调用订阅相应类型事件的方法.
07 事件总线框架
- 按照ppt讲
开始写代码
- 首先创建一个module,名字就叫icc吧
- 然后咱们需要做什么? 咱们是不是需要一个一个单例的bus对象,以及一个subscriber注解啊?
- 创建DNBus单例
public class IccBus {
private static final IccBus ourInstance = new IccBus();
public static IccBus getInstance() {
return ourInstance;
}
}
- 创建一个subscriber注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Subscriber {
String[] value();
}
- 好,做完这些准备工作,我们需要做什么呢? 咱们之前画的图,事件总线是不是有几个存储事件对应关系的位置啊? 所以我们接下来先创建这好.
/**
* 执行表
* key:标签 tag
* value:方法执行封装类的集合
*/
private Map<String, List<IccMethodInvoke>> mInvokeMap;
/**
* 标签缓存表
* key:当前类对应的class
* value:标签(tag)的集合
*/
private Map<Class<?>, List<String>> mTagCacheMap;
public static IccBus getInstance() {
return ourInstance;
}
private IccBus() {
mInvokeMap = new HashMap<>();
mTagCacheMap = new HashMap<>();
}
- 这里还需要创建一个对我们注册了@subscriber注解的方法的封装, 还有一个就是,最后要执行的方法进行封装.
public class IccMethod {
private String tag;
private Method Method;
public IccMethod(String tag, Method method) {
this.tag = tag;
Method = method;
}
public String getTag() {
return tag;
}
public IccMethod setTag(String tag) {
this.tag = tag;
return this;
}
public Method getMethod() {
return Method;
}
public IccMethod setMethod(Method method) {
Method = method;
return this;
}
}
public class IccMethodInvoke {
private Object object;
private IccMethod iccMethod;
public IccMethodInvoke(Object object, IccMethod iccMethod) {
this.object = object;
this.iccMethod = iccMethod;
}
public Object getObject() {
return object;
}
public IccMethodInvoke setObject(Object object) {
this.object = object;
return this;
}
public IccMethod getIccMethod() {
return iccMethod;
}
public IccMethodInvoke setIccMethod(IccMethod iccMethod) {
this.iccMethod = iccMethod;
return this;
}
}
- 好,那么我们这些准备工作就真的做完了,这个时候我们需要做什么? 我们是不是开始写注册和反注册啊? 对吧? 首先先写注册吧?
public void register(Object subscriber) {
if (subscriber == null) {
return;
}
//查找到实际注册的方法
findIccMethod(subscriber);
}
- 写findIccMethod
private void findIccMethod(Object subscriber) {
//要拿到哪些注解和方法对应的对象,我们是不是还是先拿到class对象?
Class<?> subscriberClazz = subscriber.getClass();
//然后就创建一个list对象,用来保存等找到的方法(这里后面写)
List<IccMethod> iccMethods = new ArrayList<>();
//先拿到我的们注册对象里面实际注册的方法,通过getDeclaredMethods拿到的
Method[] methods = subscriberClazz.getDeclaredMethods();
//然后非常简单,拿到methods后,是不是就遍历一下,看看哪些方法是我想要的?
for (Method method : methods) {
//遍历后,我们肯定是要找到有@Subscriber注解的方法嘛! 所以所,我们对每个方法都去获取他的注解
Subscriber subscriberAnnotation = method.getAnnotation(Subscriber.class);
//如果获取到Subscriber注解等于空,说明该方法并没有添加Subscriber注解,所以直接continue
if (subscriberAnnotation == null) {
continue;
}
//如果有的话,那么咱们就直接通过Subscriber.value方法拿到用户写的tag对吧?
String[] tags = subscriberAnnotation.value();
//然后就可以创建我们的iccMethod,并且添加到列表,记录下这些tag和method对应关系嘛
//所以回到上面创建List<IccMethod> iccMethods
for (String tag : tags) {
iccMethods.add(new IccMethod(tag, method));
}
}
//最后拿到的这些东西,我们通过addInfoToInvokeAndTagCache去添加
if (iccMethods.size() > 0) {
addInfoToInvokeAndTagCache(iccMethods, subscriberClazz, subscriber);
}
}
- 写addInfoToInvokeAndTagCache
private void addInfoToInvokeAndTagCache(List<IccMethod> iccMethods, Class<?> subscriberClazz, Object object) {
//首先我们判断,你新来的这个class对象之前有没有在我们的mTagCacheMap这个保存class和tag对应关系的map中有过注册,如果有,就直接拿出来用,如果没有,那么我们就创建一个新的,对吧?
List<String> tags = mTagCacheMap.get(subscriberClazz);
if (tags == null) {
tags = new ArrayList<>();
mTagCacheMap.put(subscriberClazz, tags);
}
//然后怎么样? 我们是不是吧传递进来的方法集合遍历?
for (IccMethod iccMethod : iccMethods) {
//首先还是拿到tag
String tag = iccMethod.getTag();
//判断一下这个tag是不是已经存在在我们这个tags的list集合里面,如果不存在就添加进去,对吧?
if (!tags.contains(tag)) {
tags.add(tag);
}
//然后通过tag去mInvokeMap里面去拿,map里面存的tag对应的可执行对象的集合
List<IccMethodInvoke> invokes = mInvokeMap.get(tag);
if (invokes == null) {
invokes = new ArrayList<>();
mInvokeMap.put(tag, invokes);
}
//添加一个新的可执行对象
invokes.add(new IccMethodInvoke(object, iccMethod));
}
}
- 写完了订阅,那么我们是不是就写我们的取消订阅啊?
public void unRegister(Object subscriber) {
if (subscriber == null) {
return;
}
//首先是先获取到我们这个subscribe对应的tag的集合嘛,mTagCacheMap的作用也在这里体现了
//如果没有他,我们是不是取消订阅的时候还需要一个个去找啊?
List<String> tags = mTagCacheMap.get(subscriber.getClass());
if (tags == null || tags.size() == 0) {
return;
}
//拿到tags后去遍历一下
for (String tag : tags) {
//再从mInvokeMap哪里拿到对应的IccMethodInvoke的集合
List<IccMethodInvoke> invokes = mInvokeMap.get(tag);
Iterator<IccMethodInvoke> iterator = invokes.iterator();
//最后循环遍历找到对应的subscriber,然后删除掉他
while (iterator.hasNext()) {
IccMethodInvoke iccMethodInvoke = iterator.next();
if (iccMethodInvoke.getObject() == subscriber) {
iterator.remove();
}
}
}
//然后删除掉mTagCacheMap保存的数据,就完事了
mTagCacheMap.remove(subscriber.getClass());
}
- 好,那么我们订阅和取消订阅就写完了,听懂的同学给老师刷朵鲜花,没听懂的同学有什么问题的,可以提出来,老师喝口水,大家呢,也可以在脑海里面,稍微整理一下思路
- ok 休息得差不多了,那么我们刚刚讲完了订阅和取消订阅,接下来该写什么呢? 是不是该写post方法啦? 对吧?
说干就干,咱们就开始写post方法
//首先我们要知道需要传递什么,我们是根据tag来判断事件具体归谁执行的,所以首先肯定是要有tag,其次呢,我们还需要发送参数给对方吧? 所以咱们就写一个可变长度参数在这里
public void post(String tag, Object... params) {
//这里呢,我们就先拿到执行方法集合
List<IccMethodInvoke> invokes = mInvokeMap.get(tag);
if (invokes == null || invokes.size() == 0) {
return;
}
//遍历这个集合 一个个去执行
for (IccMethodInvoke invoke : invokes) {
//获取到方法的封装对象
IccMethod iccMethod = invoke.getIccMethod();
//拿到执行方法所属的对象, invoke方法需要这个对象
Object object = invoke.getObject();
//然后拿到对应的method对象
Method method = iccMethod.getMethod();
//拿到method对象先不着急拿去执行,因为我们不确定你传递过来的参数一定和我们这个method所需要的参数相符合
//所以我们先拿到这个method对象所需参数的数量和类型
Class<?>[] parameterTypes = method.getParameterTypes();
//如果是null,说明不需要参数,那么直接执行
if (parameterTypes == null || parameterTypes.length == 0) {
invokeMethod(method, object, null);
}
//如果不是null 那么就一个个对照,拿到对应的相符合的参数
Object[] p = new Object[parameterTypes.length];
for (int i = 0; i < p.length; i++) {
//isInstance 这个对象能不能被转化为这个类 或者说是 这个对象是否是这个class对应的实例
if (i < params.length && parameterTypes[i].isInstance(params[i])) {
//参数一致
p[i] = params[i];
}else {
p[i] = null;
}
}
//最后再去执行
invokeMethod(method, object, p);
}
}
private void invokeMethod(Method method, Object obj, Object[] params) {
try {
method.invoke(obj, params);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
- 好,那么上面我们的DNBus就写完了,那么我们来测试一下,看看有没有作用好吧?
测试代码!
- 首先让app依赖的我们的icc模块
- 先写activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "IccMethodInvoke";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//订阅
IccBus.getInstance().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消订阅
IccBus.getInstance().unRegister(this);
}
//实际订阅方法1
@Subscriber("tag1")
public void test(String msg) {
Log.d(TAG, "test: " + msg);
}
//实际订阅方法1
@Subscriber("tag2")
public void test2(String msg) {
Log.d(TAG, "test2: " + msg);
}
//点击事件,发送消息
public void postMsgToF(View view) {
//发送消息
IccBus.getInstance().post("mainActivity", "hello 我是mainActivity 你们好吗?");
}
}
- 写fragmentOne
public class FragmentOne extends Fragment {
private static final String TAG = "FragmentOne";
public FragmentOne() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//订阅
IccBus.getInstance().register(this);
View root = inflater.inflate(R.layout.fragment_two, container, false);
//发送消息的点击事件
root.findViewById(R.id.btn_send_msg).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IccBus.getInstance().post("tag1", "hello 我是fragmentOne");
}
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
//取消订阅
IccBus.getInstance().unRegister(this);
}
//实际订阅的方法
@Subscriber("mainActivity")
public void fTest(String msg) {
Log.d(TAG, "fTest: " + msg);
}
}
- 继续写fragmentTwo
public class FragmentTwo extends Fragment {
private static final String TAG = "FragmentTwo";
public FragmentTwo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
IccBus.getInstance().register(this);
View root = inflater.inflate(R.layout.fragment_two, container, false);
root.findViewById(R.id.btn_send_msg).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IccBus.getInstance().post("tag2", "hello 我是fragmentTwo");
}
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
IccBus.getInstance().unRegister(this);
}
@Override
public void onStart() {
super.onStart();
}
@Subscriber("mainActivity")
public void fTest(String msg) {
Log.d(TAG, "fTest: " + msg);
}
}
-
像我们eventbus 传递的参数就是 event,然后记录好注册的方法和 参数对应的关系.然后咱们今天写的这套demo,event就是我们的tag, 也是记录好tag和执行方法对应关系.
-
livedatabus呢,记录的是一个自己设置的key,然后吧对应的livedata和key的关系保存下来,然后谁要绑定对应的事件,就拿对应的livedata去绑定observer,达到这么一个执行关系的记录.
eventBus源码详解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Subscribe {
// 指定事件订阅方法的线程模式,即在那个线程执行事件订阅方法处理事件,默认为POSTING
ThreadMode threadMode() default ThreadMode.POSTING;
// 是否支持粘性事件,默认为false
boolean sticky() default false;
// 指定事件订阅方法的优先级,默认为0,如果多个事件订阅方法可以接收相同事件的,则优先级高的先接收到事件
int priority() default 0;
}
ThreadMode.POSTING:默认模式,在那个线程发送的消息,就在那个线程处理,避免线程切换,效率很高
ThreadMode.MAIN:如在主线程(UI线程)发送事件,则直接在主线程处理事件;如果在自线程发送的消息,则使用handler将其切换到主线程执行
ThreadMode.MAIN_ORDERED:无论那个线程发送消息,都使用handler回到主线程处理该事件
ThreadMode.BACKGROUND:如果在主线程发送消息,则将该事件加入队列,然后通过线程池依次处理事件,如果在子线程发送消息,则直接在当前子线程去处理该事件
ThreadMode.ASYNC:无论在那个线程发送事件,都将事件加入队列,然后通过线程池处理
- getDefault 获取eventBus单利,并且初始化一些map和配置
EventBus(EventBusBuilder builder) {
logger = builder.getLogger();
subscriptionsByEventType = new HashMap<>();
typesBySubscriber = new HashMap<>();
stickyEvents = new ConcurrentHashMap<>();
mainThreadSupport = builder.getMainThreadSupport();
mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
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;
}
- 是一个默认的eventBusBuilder
private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
- 如果有需要的话,我们也可以通过EventBusBuilder来配置EventBus的属性
EventBus.builder()
.eventInheritance(false)
.logSubscriberExceptions(false)
.build()
.register(this);
- 有了EventBus的实例就可以进行注册了
public void register(Object subscriber) {
// 得到当前要注册类的Class对象
Class<?> subscriberClass = subscriber.getClass();
// 根据Class查找当前类中订阅了事件的方法集合,即使用了Subscribe注解、有public修饰符、一个参数的方法
// SubscriberMethod类主要封装了符合条件方法的相关信息:
// Method对象、线程模式、事件类型、优先级、是否是粘性事等
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
// 循环遍历订阅了事件的方法集合,以完成注册
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
- 先看findSubscriberMethods
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
// METHOD_CACHE是一个ConcurrentHashMap,直接保存了subscriberClass和对应SubscriberMethod的集合,以提高注册效率,赋值重复查找。
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
// 由于使用了默认的EventBusBuilder,则ignoreGeneratedIndex属性默认为false,即是否忽略注解生成器
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;
}
}
- findSubscriberMethods()流程很清晰,即先从缓存中查找,如果找到则直接返回,否则去做下一步的查找过程,然后缓存查找到的集合,根据上边的注释可知findUsingInfo()方法会被调用:
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
// 初始状态下findState.clazz就是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.clazz为subscriberClass的父类Class,即需要遍历父类
findState.moveToSuperclass();
}
// 查找到的方法保存在了FindState实例的subscriberMethods集合中。
// 使用subscriberMethods构建一个新的List<SubscriberMethod>
// 释放掉findState
return getMethodsAndRelease(findState);
}
findUsingInfo()方法会在当前要注册的类以及其父类中查找订阅事件的方法,这里出现了一个FindState类,它是SubscriberMethodFinder的内部类,用来辅助查找订阅事件的方法,具体的查找过程在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();
// 如果是public类型,但非abstract、static等
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
// 获得当前方法所有参数的类型
Class<?>[] parameterTypes = method.getParameterTypes();
// 如果当前方法只有一个参数
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
// 如果当前方法使用了Subscribe注解
if (subscribeAnnotation != null) {
// 得到该参数的类型
Class<?> eventType = parameterTypes[0];
// checkAdd()方法用来判断FindState的anyMethodByEventType map是否已经添加过以当前eventType为key的键值对,没添加过则返回true
if (findState.checkAdd(method, eventType)) {
// 得到Subscribe注解的threadMode属性值,即线程模式
ThreadMode threadMode = subscribeAnnotation.threadMode();
// 创建一个SubscriberMethod对象,并添加到subscriberMethods集合
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");
}
}
}
- 到此register()方法中findSubscriberMethods()流程就分析完了,我们已经找到了当前注册类及其父类中订阅事件的方法的集合。接下来分析具体的注册流程,即register()中的subscribe()方法:
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
// 得到当前订阅了事件的方法的参数类型
Class<?> eventType = subscriberMethod.eventType;
// Subscription类保存了要注册的类对象以及当前的subscriberMethod
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
// subscriptionsByEventType是一个HashMap,保存了以eventType为key,Subscription对象集合为value的键值对
// 先查找subscriptionsByEventType是否存在以当前eventType为key的值
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
// 如果不存在,则创建一个subscriptions,并保存到subscriptionsByEventType
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);
}
}
// 添加上边创建的newSubscription对象到subscriptions中
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;
}
}
// typesBySubscribere也是一个HashMap,保存了以当前要注册类的对象为key,注册类中订阅事件的方法的参数类型的集合为value的键值对
// 查找是否存在对应的参数类型集合
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
// 不存在则创建一个subscribedEvents,并保存到typesBySubscriber
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);
}
}
}
- 取消注册
- 核心的方法就是unregister()
public synchronized void unregister(Object subscriber) {
// 得到当前注册类对象 对应的 订阅事件方法的参数类型 的集合
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
// 遍历参数类型集合,释放之前缓存的当前类中的Subscription
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
// 删除以subscriber为key的键值对
typesBySubscriber.remove(subscriber);
} else {
logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
- 看看unsubscribeByEventType里面到底在干啥
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
// 得到当前参数类型对应的Subscription集合
List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
// 遍历Subscription集合
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
// 如果当前subscription对象对应的注册类对象 和 要取消注册的注册类对象相同,则删除当前subscription对象
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}
- 接下来看post方法
public void post(Object event) {
// currentPostingThreadState是一个PostingThreadState类型的ThreadLocal
// PostingThreadState类保存了事件队列和线程模式等信息
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
// 将要发送的事件添加到事件队列
eventQueue.add(event);
// isPosting默认为false
if (!postingState.isPosting) {
// 是否为主线程
postingState.isMainThread = isMainThread();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
// 遍历事件队列
while (!eventQueue.isEmpty()) {
// 发送单个事件
// eventQueue.remove(0),从事件队列移除事件
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
- 所以post()方法先将发送的事件保存的事件队列,然后通过循环出队列,将事件交给postSingleEvent()方法处理:
- private void postSingleEvent(Object event, PostingThreadState
postingState) throws Error {
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
// eventInheritance默认为true,表示是否向上查找事件的父类
if (eventInheritance) {
// 查找当前事件类型的Class,连同当前事件类型的Class保存到集合
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
// 遍历Class集合,继续处理事件
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) {
logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
} - postSingleEvent()方法中,根据eventInheritance属性,决定是否向上遍历事件的父类型,然后用postSingleEventForEventType()方法进一步处理事件:
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
// 获取事件类型对应的Subscription集合
subscriptions = subscriptionsByEventType.get(eventClass);
}
// 如果已订阅了对应类型的事件
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
// 记录事件
postingState.event = event;
// 记录对应的subscription
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 {
// 如果是在子线程发送事件,则将事件入队列,通过Handler切换到主线程执行处理事件
// mainThreadPoster 不为空
mainThreadPoster.enqueue(subscription, event);
}
break;
// 无论在那个线程发送事件,都先将事件入队列,然后通过 Handler 切换到主线程,依次处理事件。
// mainThreadPoster 不为空
case MAIN_ORDERED:
if (mainThreadPoster != null) {
mainThreadPoster.enqueue(subscription, event);
} else {
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);
}
}
- 可以看到,postToSubscription()方法就是根据订阅事件方法的线程模式、以及发送事件的线程来判断如何处理事件,至于处理方式主要有两种:一种是在相应线程直接通过invokeSubscriber()方法,用反射来执行订阅事件的方法,这样发送出去的事件就被订阅者接收并做相应处理了:
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);
}
}
- 另外一种是先将事件入队列(其实底层是一个List),然后做进一步处理,我们以mainThreadPoster.enqueue(subscription,
event)为例简单的分析下,其中mainThreadPoster是HandlerPoster类的一个实例,来看该类的主要实现:
public class HandlerPoster extends Handler implements Poster {
private final PendingPostQueue queue;
private boolean handlerActive;
......
public void enqueue(Subscription subscription, Object event) {
// 用subscription和event封装一个PendingPost对象
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
// 入队列
queue.enqueue(pendingPost);
if (!handlerActive) {
handlerActive = true;
// 发送开始处理事件的消息,handleMessage()方法将被执行,完成从子线程到主线程的切换
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();
......
// 进一步处理pendingPost
eventBus.invokeSubscriber(pendingPost);
......
}
} finally {
handlerActive = rescheduled;
}
}
}
网友评论