项目地址 :https://github.com/greenrobot/EventBus

这个项目个人感觉就是为了解决回调事件过多的,比方说A函数在做完以后 要调用b类的c函数,那我们通常的做法就是 定义一个接口 然后再A函数所属的类里面注册这个接口。

然后a函数做完以后 直接调用这个接口即可。但是这种方法写多了以后确实很麻烦,于是EventBus就是用来解决这种场景的。

和以往一样,我们只解析他的源码,如果你要学习他的用法请自行谷歌。

我们就从register函数开始说起。

  private synchronized void register(Object subscriber, boolean sticky, int priority) {
//这个list就是方法的集合
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass());
//这个循环的目的就是为了用 方法对象 来构造 Subscription对象
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod, sticky, priority);
}
}

首先来看一下SubscriberMethod这个类,

 /*
* Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.event; import java.lang.reflect.Method; import android.util.Log; /**
* 这个类就是描述方法用的
*/
final class SubscriberMethod {
final Method method;
final ThreadMode threadMode;
final Class<?> eventType;
/** Used for efficient comparison */
/**
* 这个methodString 实际上就是用来描述SubscriberMethod对象的,尤其是在重写的equals方法里 起到关键的作用
* 就类似于这种结构
* com.example.administrator.eventbustest.ItemDetailFragment#onEventMainThread(com.example.administrator.eventbustest.Item
*其实也很好理解就是 包名+类名#方法名(参数的类型
* 注意这里参数的类型也是全路径名 包名+类名
*/
String methodString; SubscriberMethod(Method method, ThreadMode threadMode, Class<?> eventType) {
this.method = method;
this.threadMode = threadMode;
this.eventType = eventType;
} @Override
public boolean equals(Object other) {
if (other instanceof SubscriberMethod) {
checkMethodString();
SubscriberMethod otherSubscriberMethod = (SubscriberMethod) other;
otherSubscriberMethod.checkMethodString();
// Don't use method.equals because of http://code.google.com/p/android/issues/detail?id=7811#c6
return methodString.equals(otherSubscriberMethod.methodString);
} else {
return false;
}
} private synchronized void checkMethodString() {
if (methodString == null) {
// Method.toString has more overhead, just take relevant parts of the method
StringBuilder builder = new StringBuilder(64);
builder.append(method.getDeclaringClass().getName());
builder.append('#').append(method.getName());
builder.append('(').append(eventType.getName());
methodString = builder.toString();
}
} @Override
public int hashCode() {
return method.hashCode();
}
}

然后我们来看看这个SubscriberMethod对象组成的集合 是怎么构造出来的。

 List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
String key = subscriberClass.getName();
List<SubscriberMethod> subscriberMethods;
synchronized (methodCache) {
subscriberMethods = methodCache.get(key);
} if (subscriberMethods != null) {
return subscriberMethods;
}
subscriberMethods = new ArrayList<SubscriberMethod>();
Class<?> clazz = subscriberClass;
HashSet<String> eventTypesFound = new HashSet<String>();
StringBuilder methodKeyBuilder = new StringBuilder();
while (clazz != null) {
String name = clazz.getName();
//这个地方判断如果是这些类,那么就直接跳出这个while循环
//注意name的值 也是包名+类名,所以这里就是过滤掉基础的sdk的那些方法
//如果你有引用其他公共lib库的话 你也可以过滤他们的包,
if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.")) {
// Skip system classes, this just degrades performance
break;
} // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again)
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
String methodName = method.getName();
Log.e("burning", "methodName == " + methodName);
//只有那些以onEvent开头的方法才是我们需要的
if (methodName.startsWith(ON_EVENT_METHOD_NAME)) {
int modifiers = method.getModifiers();
//注意这个地方判断方法属性的技巧 与 操作
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
//如果参数只有一个
if (parameterTypes.length == 1) {
String modifierString = methodName.substring(ON_EVENT_METHOD_NAME.length());
//取ThreadMode
ThreadMode threadMode;
if (modifierString.length() == 0) {
threadMode = ThreadMode.PostThread;
} else if (modifierString.equals("MainThread")) {
threadMode = ThreadMode.MainThread;
} else if (modifierString.equals("BackgroundThread")) {
threadMode = ThreadMode.BackgroundThread;
} else if (modifierString.equals("Async")) {
threadMode = ThreadMode.Async;
} else {
if (skipMethodVerificationForClasses.containsKey(clazz)) {
continue;
} else {
throw new EventBusException("Illegal onEvent method, check for typos: " + method);
}
}
Class<?> eventType = parameterTypes[0];
methodKeyBuilder.setLength(0);
methodKeyBuilder.append(methodName);
methodKeyBuilder.append('>').append(eventType.getName());
//onEventMainThread>java.lang.String
//methodKey就是上面的形式,可以看出来是方法名>参数 的格式
String methodKey = methodKeyBuilder.toString();
//这个地方先去这个hashset里面add这个key,当然了,如果你这个hashset里面已经有这个key
//那必然是add不成功的,只有add成功返回true以后括号内的代码才会得到执行
if (eventTypesFound.add(methodKey)) {
// Only add if not already found in a sub class
//这个地方就是构造SubscriberMethod对象放到list里 准备返回
subscriberMethods.add(new SubscriberMethod(method, threadMode, eventType));
}
}
} else if (!skipMethodVerificationForClasses.containsKey(clazz)) {
Log.d(EventBus.TAG, "Skipping method (not public, static or abstract): " + clazz + "."
+ methodName);
}
}
}
//这里注意还在大的while循环内,所以你传进去的类自己查完一遍方法以后 还会去找他的父类继续查询方法
//一直遍历到父类为 那些java android开头的基类为止!
clazz = clazz.getSuperclass(); }
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass + " has no public methods called "
+ ON_EVENT_METHOD_NAME);
} else {
synchronized (methodCache) {
methodCache.put(key, subscriberMethods);
}
return subscriberMethods;
}
}

然后我们来看看register函数里面 5-6行 那个循环遍历做了什么

首先我们看看这个循环调用的方法:

 /**
* @param subscriber 方法所述的类的 包名+类名
* @param subscriberMethod
* @param sticky
* @param priority
*/
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod, boolean sticky, int priority) {
//这个eventtype就是方法的参数的类名
Class<?> eventType = subscriberMethod.eventType; CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
Subscription newSubscription = new Subscription(subscriber, subscriberMethod, priority);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<Subscription>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
} // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again)
// subscriberMethod.method.setAccessible(true);
//这个就是优先级高的位置在前面
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || newSubscription.priority > subscriptions.get(i).priority) {
subscriptions.add(i, newSubscription);
break;
}
} List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<Class<?>>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType); if (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);
}
}
}

10-13行 我们可以看出来 这个函数 主要是为了构造subscription这个list对象。

  /**
* 这个map 存储方法的地方 key就是eventType,value就是copyOnWriteArrayList value就是方法的一切
*/
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;

我们可以看看这个类是什么

 /**
* 这个类里面包含有SubscriberMethod类对象,
* subscriber
*/
final class Subscription {
//这个实际上就是描述方法所属的类的
final Object subscriber;
//描述方法的类
final SubscriberMethod subscriberMethod;
//优先级
final int priority;
/**
* Becomes false as soon as {@link EventBus#unregister(Object)} is called, which is checked by queued event delivery
* {@link EventBus#invokeSubscriber(PendingPost)} to prevent race conditions.
*/
volatile boolean active; Subscription(Object subscriber, SubscriberMethod subscriberMethod, int priority) {
this.subscriber = subscriber;
this.subscriberMethod = subscriberMethod;
this.priority = priority;
active = true;
} @Override
public boolean equals(Object other) {
if (other instanceof Subscription) {
Subscription otherSubscription = (Subscription) other;
return subscriber == otherSubscription.subscriber
&& subscriberMethod.equals(otherSubscription.subscriberMethod);
} else {
return false;
}
} @Override
public int hashCode() {
return subscriber.hashCode() + subscriberMethod.methodString.hashCode();
}
}

所以总结起来,SubscriberMethod 就是对方法的描述,而我们的SubscriberMethod 实际上就是Subscription的子集,Subscription除了有描述方法的对象以外,还有这个方法所属的类,

而我们的register方法总体来说 就是先通过findSubscriberMethods方法 取得我们注册类(就是你register调用的时候传的this)所需要的的那些方法(注意不是每个方法都需要 只选择自己需要的)

然后把这些方法 做一个list,最后再通过便利这个list : 用每一个SubscriberMethod 对象和这个方法所需的类(包名+类名) 来构造出一个Subscription对象,然后把这个对象

存储在SubscriptionsByEventType里,注意这个map的key 实际上就是eventType,而value则代表方法的list,换句话说。

这个SubscriptionsByEventType 是一个键值对,它的key 实际上就是我们的类名,value则是这个类里面我们需要存储的方法的list!

这就是register的大致流程,我们再来看看post 流程即可。

  /**
* Posts the given event to the event bus.
*/
public void post(Object event) {
PostingThreadState postingState = currentPostingThreadState.get();
//这个地方可以看出来是每次有人调用post方法的时候 都会从postingState取出这个队列,然后把这个事件放到这个队列里
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event); //这个判断isPosting 主要是为了保证同一时间只能有一个线程在处理括号体里的内容
//currentPostingThreadState 是用threadlocal来构造的 所以保证了同步性
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;
}
}
}

先看看第5行的postingState是什么

  /**
* 静态类,里面除了有一个队列以外,还有几个标志位,以及一个Subscription
*/
final static class PostingThreadState {
final List<Object> eventQueue = new ArrayList<Object>();
boolean isPosting;
boolean isMainThread;
Subscription subscription;
Object event;
boolean canceled;
}

这个地方就能看出来,我们每次调用post 都是往eventQueue里面添加一个事件,而12行开始则是从队列里面

取事件来处理,注意12行开始 一次性只能允许一个线程使用~同步的

然后继续看是怎么处理的。

  /**
* @param event 方法的参数的类名
* @param postingState
* @throws Error
*/
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);
//这个地方就是取出Subscription对象的的所有信息!发消息也是在这个函数里发送的
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));
}
}
}

继续跟进去

 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;
}

可以看出来 2-6行 就是从我们register流程里存储的键值对里 把我们存放的方法给取出来。,

取出来以后 就可以反射调用他们的方法了

   /**
* 这个类就是反射执行方法 并且是最终执行回调方法的地方
*
* @param subscription
* @param event
* @param isMainThread
*/
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {
case PostThread:
invokeSubscriber(subscription, event);
break;
case MainThread:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case BackgroundThread:
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);
}
}

13-18行 这个case 如果是主线程,那么就直接反射方法,如果不是的话 则要放到主线程handler里执行。

  private final HandlerPoster mainThreadPoster;
  //这个就是主线程handler初始化
mainThreadPoster = new HandlerPoster(this, Looper.getMainLooper(), 10);
 /*
* Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.event; import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock; 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;
}
}
}

51-77行 就是我们实际最终调用的地方。

同样的 我们在看看20-26行的这个background这个case

 final class BackgroundPoster implements Runnable {

     private final PendingPostQueue queue;
private final EventBus eventBus; private volatile boolean executorRunning; BackgroundPoster(EventBus eventBus) {
this.eventBus = eventBus;
queue = new PendingPostQueue();
} public void enqueue(Subscription subscription, Object event) {
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
queue.enqueue(pendingPost);
if (!executorRunning) {
executorRunning = true;
eventBus.getExecutorService().execute(this);
}
}
} @Override
public void run() {
try {
try {
while (true) {
PendingPost pendingPost = queue.poll(1000);
if (pendingPost == null) {
synchronized (this) {
// Check again, this time in synchronized
pendingPost = queue.poll();
if (pendingPost == null) {
executorRunning = false;
return;
}
}
}
eventBus.invokeSubscriber(pendingPost);
}
} catch (InterruptedException e) {
Log.w("Event", Thread.currentThread().getName() + " was interruppted", e);
}
} finally {
executorRunning = false;
}
}

一看就知道 他是runnable对象 必然是在后台 在子线程内执行,同时他也是一次性只能做一次操作,完成一个事件,

最后我们来看看Async这个case:

 /*
* Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.event; /**
* Posts events in background.
*
* @author Markus 并发执行任务,在线程池内执行
*/
class AsyncPoster implements Runnable { private final PendingPostQueue queue;
private final EventBus eventBus; AsyncPoster(EventBus eventBus) {
this.eventBus = eventBus;
queue = new PendingPostQueue();
} public void enqueue(Subscription subscription, Object event) {
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
queue.enqueue(pendingPost);
eventBus.getExecutorService().execute(this);
} @Override
public void run() {
PendingPost pendingPost = queue.poll();
if(pendingPost == null) {
throw new IllegalStateException("No pending post available");
}
eventBus.invokeSubscriber(pendingPost);
} }

这个地方和background相同的就是也是在非主线程,在子线程内执行,但是这个地方是在线程池内执行,可以并发执行多个任务,

而我们的background 则一次性只能执行一个任务,这是2者之间的区别。

最新文章

  1. visual c++ 2010安装失败导致CRM2015安装失败
  2. javascript简单的认识下return语句+2015的总结+2016的展望
  3. 如何正确做 Web端压力测试?
  4. iOS上传文件,有关http上传协议-RFC1867
  5. Directory.GetCurrentDirectory
  6. ecstore 后台登陆跳转到 api失败,中心请求网店API失败
  7. Weblogic11g安装
  8. 如何为linux释放内存和缓存
  9. PHP判断一个变量是否可以通过foreach进行遍历
  10. 一步一步学J2SE-ConcurrentHashMap原理
  11. 一个APP页面一个接口
  12. CSS3实现轴心为x轴的3D数字圆环
  13. Healwire Online Pharmacy 3.0 Cross Site Request Forgery / Cross Site Scripting
  14. 在本机eclipse中创建maven项目,查看linux中hadoop下的文件、在本机搭建hadoop环境
  15. 【学习总结】GirlsInAI ML-diary day-8-list列表
  16. gitlab-ci + k8s 之gitlab-ci(一)
  17. Linux下输入某些命令时会提示:bash:command not found
  18. JS 数组常用的方法
  19. Java8新特性 集合的stream的map
  20. SpringDataRedis事务 专题

热门文章

  1. IDA 与VC 加载符号表
  2. 【转】terminal 快捷键
  3. 纯CSS实现的右侧底部简洁悬浮效果
  4. awk除去重复行
  5. 瞬态电压抑制二极管(TVS)选用原则
  6. 如何在Java客户端调用RESTful服务
  7. *在Win7中安装JDK1.7并配置环境变量
  8. (八)ASP.NET自定义用户控件(1)
  9. 总结Allegro元件封装(焊盘)制作方法[修整]
  10. 4.cadence原理图,环境设置[原创]