EventBus3.0源码解析

一、观察者模式:

  • 简介:观察者模式是设计模式中的一种。它是为了定义对象间的一种一对多的依赖关系,即当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
  • 重点:在这个模式中主要包含两个重要的角色:发布者订阅者(又称观察者)。对应EventBus来说,发布者即发送消息的一方(即调用EventBus.getDefault().post(event)的一方),订阅者即接收消息的一方(即调用EventBus.getDefault().register()的一方)。

二、从入口分析:

在activity的oncreate中register,在ondestory中unregister

EventBus.getDefault().register(this);
EventBus.getDefault().unregister(this);
static volatile EventBus defaultInstance;
/** Convenience singleton for apps using a process-wide EventBus instance. */
public static EventBus getDefault() {
    if (defaultInstance == null) {
        synchronized (EventBus.class) {
            if (defaultInstance == null) {
                defaultInstance = new EventBus();
            }
        }
    }
    return defaultInstance;
}

从这里可以看出,eventbus是采用双重校验加锁的形式的标准的单例模式获取的eventbus实例

/**
 * Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
 * are no longer interested in receiving events.
 * 

* Subscribers have event handling methods that must be annotated by {@link Subscribe}. * The {@link Subscribe} annotation also allows configuration like {@link * ThreadMode} and priority. */ public void register(Object subscriber) { Class subscriberClass = subscriber.getClass(); List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass); synchronized (this) { for (SubscriberMethod subscriberMethod : subscriberMethods) { subscribe(subscriber, subscriberMethod); } } }

由于我们传入的this,由getClass获取到订阅者的class对象,然后根据subscriberMethodFinder.findSubscriberMethods方法可以找出所有的订阅方法。

List findSubscriberMethods(Class subscriberClass) {
    List 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;
    }
}

如果缓存列表中有对应class的订阅方法列表,则返回。我们这里第一次创建,缓存列表为空。查找前面的创建过程,ignoreGeneratedIndex默认为false,进入else代码块,后面生成subscriberMethods方法成功的话会加入缓存列表中,失败的话会throw异常。

现在回过来查看findUsingInfo方法,

private List 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);
}
private FindState prepareFindState() {
    synchronized (FIND_STATE_POOL) {
        for (int i = 0; i < POOL_SIZE; i++) {
            FindState state = FIND_STATE_POOL[i];
            if (state != null) {
                FIND_STATE_POOL[i] = null;
                return state;
            }
        }
    }
    return new FindState();
}

这个方法是创建一个FindState类,通过两种方法获取,一种是从FIND_STATE_POOL池中取出可用的FindState,如果没有的话,则直接new一个FindState对象。

下面看initForSubscriber方法,主要做了一些初始化的工作:

void initForSubscriber(Class subscriberClass) {
    this.subscriberClass = clazz = subscriberClass;
    skipSuperClasses = false;
    subscriberInfo = null;
}
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;
        }
    }
    if (subscriberInfoIndexes != null) {
        for (SubscriberInfoIndex index : subscriberInfoIndexes) {
            SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
            if (info != null) {
                return info;
            }
        }
    }
    return null;
}

由于初始化的时候subscriberInfoIndexes和subscriberInfo都为空,所以这里直接返回null。

接着看下面的方法:

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

这个方法的逻辑是:通过反射的方法获取订阅者类中的所有方法,然后在这些方法中查找以Subscribe注解的方法,先经过一轮检查,然后看看findState.subscriberMethods中是否存在,如果没有的话,将方法名、eventType,优先级等等封装成一个SubscriberMethod对象缓存到findState.subscriberMethods中。

什么是sticky event:sticky event,中文名为粘性事件。普通事件是先注册,然后发送事件才能收到;而粘性事件,在发送事件之后再订阅该事件也能收到。此外,粘性事件会保存在内存中,每次进入都会去内存中查找获取最新的粘性事件,除非你手动解除注册。

从以上分析可以看出,方法名可以任意定义,但是必须为public的且非静态的

现在分析getMethodAndRelease方法

private List getMethodsAndRelease(FindState findState) {
    List subscriberMethods = new ArrayList<>(findState.subscriberMethods);
    findState.recycle();
    synchronized (FIND_STATE_POOL) {
        for (int i = 0; i < POOL_SIZE; i++) {
            if (FIND_STATE_POOL[i] == null) {
                FIND_STATE_POOL[i] = findState;
                break;
            }
        }
    }
    return subscriberMethods;
}

这里将subscriberMethods列表直接返回,同时会把findState做相应处理,存储在FindState池中,方便下一次使用,提高性能。

三、分析subcribe方法:返回subscriberMethods之后,register方法的最后会调用subscribe方法

// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
    Class eventType = subscriberMethod.eventType;
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
    CopyOnWriteArrayList 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> 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).
            Set, Object>> entries = stickyEvents.entrySet();
            for (Map.Entry, 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);
        }
    }
}
  • 首先,根据subscriberMethod.eventType,在subscriptionsByEventType去查找一个CopyOnWriteArrayList ,如果没有则创建一个新的CopyOnWriteArrayList;
  • 然后将这个CopyOnWriteArrayList放入subscriptionsByEventType中,这里的subscriptionsByEventType是一个Map,key为eventType,value为CopyOnWriteArrayList,这个Map非常重要,后续还会用到它;
  • 接下来,就是添加newSubscription,它属于Subscription类,里面包含着subscriber和subscriberMethod等信息,同时这里有一个优先级的判断,说明它是按照优先级添加的。优先级越高,会插到在当前List靠前面的位置;
  • typesBySubscriber这个类也是一个Map,key为subscriber,value为subscribedEvents,即所有的eventType列表,这个类我找了一下,发现在EventBus#isRegister()方法中有用到,应该是用来判断这个Subscriber是否已被注册过。然后将当前的eventType添加到subscribedEvents中;
  • 最后,判断是否是sticky。如果是sticky事件的话,到最后会调用checkPostStickyEventToSubscription()方法。
这里其实就是将所有含@Subscribe注解的订阅方法最终保存在subscriptionsByEventType中。

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

然后调用EventBus的postToSubscription方法将消息发送出去。

四、接下来分析post,看EventBus是如何将消息发送出去的

/** Posts the given event to the event bus. */
public void post(Object event) {
    PostingThreadState postingState = currentPostingThreadState.get();
    List 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;
        }
    }
} 
  

查看代码,发现:

private final ThreadLocal currentPostingThreadState = new ThreadLocal() {
    @Override
    protected PostingThreadState initialValue() {
        return new PostingThreadState();
    }
};

也就是说,currentPostingThreadState是一个ThreadLocal类型的,存储了PostingTheadState对象,

/** For ThreadLocal, much faster to set (and get multiple values). */
final static class PostingThreadState {
    final List eventQueue = new ArrayList();
    boolean isPosting;
    boolean isMainThread;
    Subscription subscription;
    Object event;
    boolean canceled;
} 
  

发现PostingTheadState是一个final的类,里面存储了eventQueue、event等。然后将传入的event保存到了当前线程的一个变量PostingThreadState的eventQueue中。

这里涉及到两个标志位:isMainThread(判断当前是否是UI线程)和isPosting(防止方法多次调用)

最后用到了postSingleEvent方法:

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
    Class eventClass = event.getClass();
    boolean subscriptionFound = false;
    if (eventInheritance) {
        List> 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));
        }
    }
}

这里先获取event的class类型,然后有一个eventInheritance标志位判断。默认为true,他会拿到event父类的class类型,设置为false,可以在一定程度上提高性能。

  • 接下来是lookupAllEventTypes()方法,就是取出Event及其父类和接口的class列表,当然重复取的话会影响性能,所以它也有做一个eventTypesCache的缓存,这样不用都重复调用getClass()方法。
  • 然后是postSingleEventForEventType()方法,这里就很清晰了,就是直接根据Event类型从subscriptionsByEventType中取出对应的subscriptions,与之前的代码对应,最后调用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 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);
    }
}

这里会根据threadMode来判断应该在哪个线程中去执行方法:

  • POSTING:执行invokeSubscriber()方法,就是直接反射调用;
  • MAIN:首先去判断当前是否在UI线程,如果是的话则直接反射调用,否则调用mainThreadPoster#enqueue(),即把当前的方法加入到队列之中,然后通过handler去发送一个消息,在handler的handleMessage中去执行方法。具体逻辑在HandlerPoster.java中;
  • MAIN_ORDERED:与上面逻辑类似,顺序执行我们的方法;
  • BACKGROUND:判断当前是否在UI线程,如果不是的话直接反射调用,是的话通过backgroundPoster.enqueue()将方法加入到后台的一个队列,最后通过线程池去执行;
  • ASYNC:与BACKGROUND的逻辑类似,将任务加入到后台的一个队列,最终由Eventbus中的一个线程池去调用,这里的线程池与BACKGROUND逻辑中的线程池用的是同一个。

补充:BACKGROUND和ASYNC有什么区别呢?
BACKGROUND中的任务是一个接着一个的去调用,而ASYNC则会即时异步运行,具体的可以对比AsyncPoster.java和BackgroundPoster.java两者代码实现的区别。

你可能感兴趣的:(源码分析)