再学Android之EventBus源码分析

栏目: Android · 发布时间: 5年前

内容简介:市场上已经有很多分析EventBus的优秀文章,笔者在看了EventBus的源码之后也是看了不少文章,在这里表示感谢。 为什么要阅读源码?在我看来,当没有一个大佬带你前行的时候,源码就是你的老师,看这些优秀的框架的源码,的确能学到不少东西,这里先总结一下,通过EventBus我们能学到什么对于EventBus,这里就不作太多介绍,相信每一个做Android的应该都知道这个优秀的框架首先我们先来看一下EventBus的常规用法: EventBus.getDefault().register(this); 接下

市场上已经有很多分析EventBus的优秀文章,笔者在看了EventBus的源码之后也是看了不少文章,在这里表示感谢。 为什么要阅读源码?在我看来,当没有一个大佬带你前行的时候,源码就是你的老师,看这些优秀的框架的源码,的确能学到不少东西,这里先总结一下,通过EventBus我们能学到什么

  • DCL单例模式
  • Builder建造者模式
  • 享元模式
  • 反射
  • 线程池的使用

对于EventBus,这里就不作太多介绍,相信每一个做Android的应该都知道这个优秀的框架

首先我们先来看一下EventBus的常规用法: EventBus.getDefault().register(this); 接下来我们来分布看下都做了什么。

EventBus.getDefault()

我们先来看一下,getDefault做了什么

static volatile EventBus defaultInstance;
private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();

/** 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;
    }
    
    public EventBus() {
        this(DEFAULT_BUILDER);
    }
    
    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;
    }
    
    
复制代码

EventBus的实例构建采用了线程安全的DCL单例模式,并且实例defaultInstance加了volatile关键字。 真正的构建实例则是采用了builder模式。在EventBus的构造函数中有几个需要我们注意的地方:

private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
    private final Map<Object, List<Class<?>>> typesBySubscriber;
    private final Map<Class<?>, Object> stickyEvents;
    private final ExecutorService executorService;
    
    
    EventBus(EventBusBuilder builder) {
        
        subscriptionsByEventType = new HashMap<>();
        typesBySubscriber = new HashMap<>();
        stickyEvents = new ConcurrentHashMap<>();
        subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
                builder.strictMethodVerification, builder.ignoreGeneratedIndex);
        executorService = builder.executorService;
        ...
    }
    
public class EventBusBuilder {
    private final static ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();

    ExecutorService executorService = DEFAULT_EXECUTOR_SERVICE;

    EventBusBuilder() {
    }
}
    
复制代码

下面先简单说下上面三个集合有什么用:

  • subscriptionsByEventType 通过eventType去获取Subsription,事件发送完之后,在寻找订阅着的时候会用到
  • typesBySubscriber 通过Subscripber(订阅者)去获取所有的eventtype,在注册和反注册的时候会用到。
  • stickyEvents 粘性事件的集合

那么Subscription是什么? 是一个封装了订阅类和订阅方法的一个对象

final class Subscription {
    final Object subscriber;
    final SubscriberMethod subscriberMethod;
    }
    
复制代码

那么SubscriberMethod又是什么? 是一个封装了被@Subscribe注解的方法的一些信息

public class SubscriberMethod {
    final Method method;
    final ThreadMode threadMode;
    final Class<?> eventType;
    final int priority;
    final boolean sticky;
    /** Used for efficient comparison */
    String methodString;
}

复制代码

我们再来看一下线程池创建比较有意思的地方: 默认的线程池是静态的,而构建对象的时候则引用了默认的。这样便巧妙的避开了线程池的不必要的创建。 下面再简单介绍下Executors.newCachedThreadPool();这是 java 为我们提供的一个线程池,大小不受限,使用一个不存储元素的阻塞队列SynchronousQueue。当然如果我们不满意这种线程池的话,还可以自己去配置

EventBusBuilder.class:

public EventBusBuilder executorService(ExecutorService executorService) {
        this.executorService = executorService;
        return this;
    }

复制代码

到这里getDefault的分析结束

register

先来简单看下源码

/**
     * Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
     * are no longer interested in receiving events.
     * <p/>
     * 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<SubscriberMethod> subscriberMethods =
        // 我们要注意这个代码 分析1
        subscriberMethodFinder.findSubscriberMethods(subscriberClass);
        synchronized (this) {
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
            // 订阅 分析2
                subscribe(subscriber, subscriberMethod);
            }
        }
    }
    
复制代码

注释中已经说了,当订阅者不想接收事件的时候,一定要调用unregister。 我们先来看下上面的分析1:

subscriberMethodFinder.findSubscriberMethods(subscriberClass);

subscriberClass我们知道,那么subscriberMethodFinder是什么?在哪初始化的?看名字我们大概知道这是干嘛的,白话讲就是:订阅方法寻找器根据subscriberClass去寻找订阅方法,也就是找到 订阅类里面所有的订阅方法

EventBus(EventBusBuilder builder) {
        subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
                builder.strictMethodVerification, builder.ignoreGeneratedIndex);
    }
    
复制代码

上述代码中的ignoreGeneratedIndex参数需要我们注意,下面会分析

下面我们来看下 subscriberMethodFinder.findSubscriberMethods(subscriberClass)的源码

private static final Map<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new ConcurrentHashMap<>();

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        // 先看缓存中有没有该订阅类的订阅方法的集合,有的话就直接返回
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }

        // ignoreGeneratedIndex是有没有使用APT,默认为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;
        }
    }
复制代码

上面代码的逻辑不复杂,我们一步一步分析。首先判断缓存中有没有该订阅类的订阅方法的集合,有的话直接返回,没有的话根据ignoreGeneratedIndex字段判断是使用APT去查找还是使用反射的方式去查找,找到之后就存储在缓存中,我们看到,METHOD_CACHE使用的是ConcurrentHashMap线程安全的map类,既然ignoreGeneratedIndex参数默认为false,那么下面我们来分析下findUsingInfo

findUsingInfo

在看这个源码之前,我们先来看下SubscriberMethodFinder的内部类FindState的源码,看名字的意思是,查找状态

FindStte

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;

        void initForSubscriber(Class<?> subscriberClass) {
            this.subscriberClass = clazz = subscriberClass;
            skipSuperClasses = false;
            subscriberInfo = null;
        }

        void recycle() {
            subscriberMethods.clear();
            anyMethodByEventType.clear();
            subscriberClassByMethodKey.clear();
            methodKeyBuilder.setLength(0);
            subscriberClass = null;
            clazz = null;
            skipSuperClasses = false;
            subscriberInfo = null;
        }

        boolean checkAdd(Method method, Class<?> eventType) {
            // 2 level check: 1st level with event type only (fast), 2nd level with complete signature when required.
            // Usually a subscriber doesn't have methods listening to the same event type.
            Object existing = anyMethodByEventType.put(eventType, method);
            if (existing == null) {
                return true;
            } else {
                if (existing instanceof Method) {
                    if (!checkAddWithMethodSignature((Method) existing, eventType)) {
                        // Paranoia check
                        throw new IllegalStateException();
                    }
                    // Put any non-Method object to "consume" the existing Method
                    anyMethodByEventType.put(eventType, this);
                }
                return checkAddWithMethodSignature(method, eventType);
            }
        }

        private boolean checkAddWithMethodSignature(Method method, Class<?> eventType) {
            methodKeyBuilder.setLength(0);
            methodKeyBuilder.append(method.getName());
            methodKeyBuilder.append('>').append(eventType.getName());

            String methodKey = methodKeyBuilder.toString();
            Class<?> methodClass = method.getDeclaringClass();
            Class<?> methodClassOld = subscriberClassByMethodKey.put(methodKey, methodClass);
            if (methodClassOld == null || methodClassOld.isAssignableFrom(methodClass)) {
                // Only add if not already found in a sub class
                return true;
            } else {
                // Revert the put, old class is further down the class hierarchy
                subscriberClassByMethodKey.put(methodKey, methodClassOld);
                return false;
            }
        }

        //  向上查找他的父类
        void moveToSuperclass() {
            if (skipSuperClasses) {
                clazz = null;
            } else {
                clazz = clazz.getSuperclass();
                String clazzName = clazz.getName();
                /** Skip system classes, this just degrades performance. */
                if (clazzName.startsWith("java.") || clazzName.startsWith("javax.") || clazzName.startsWith("android.")) {
                    clazz = null;
                }
            }
        }
    }
复制代码

看完源码之后,我们知道这个其实就是用于保存订阅类的一些信息

private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
        // 采用享元模式,即对象复用,来构建FindState
        FindState findState = prepareFindState();
        // 初始化FindState的一些信息
        findState.initForSubscriber(subscriberClass);
        
        while (findState.clazz != null) {
            // 这里默认是去APT生成的文件中去查找,这里先不说
            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 {
            // 不使用APT的话会调用到这里
                findUsingReflectionInSingleClass(findState);
            }
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);
    }
    
SubscriberMethodFinder#prepareFindState:

//享元模式的应用,也就是对象的复用,防止大量的重复的实例话对象,减小内存,提高效率
    private static final int POOL_SIZE = 4;
    private static final FindState[] FIND_STATE_POOL = new FindState[POOL_SIZE];
    
     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();
        }
复制代码

上面源码中getSubscriberInfo其实就是去APT生成的文件中去查找订阅的方法,但是使用APT的话需要我么自己去配置,不配置的话默认肯定返回null,那么我们先来分析一下: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
            if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
            // 拿到参数列表,进行第二次过滤,即参数的个数必须是1
                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();
                            // 然后构建一个SubscriberMethod添加到findState.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");
            }
        }
    }

复制代码

我们先简单描述下上面方法的执行流程: 先反射获取方法,然后根据各种条件进行判断,满足的就添加到findState中,不满足的话就抛出异常

在回到findUsingInfo里面,方法的最后调用了getMethodsAndRelease,我们来简单看下:

private List<SubscriberMethod> getMethodsAndRelease(FindState findState) {
// 构建一个新的集合,将FindState中的订阅方法(subscriberMethods)添加到新的集合里面
        List<SubscriberMethod> 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;
    }
复制代码

我们再回到findSubscriberMethods

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }

        if (ignoreGeneratedIndex) {
        // 不实用APT的话,调用下面的方法
            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;
        }
    }
复制代码

我们可以看到不使用APT的话,调用的是findUsingReflection(subscriberClass)

private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
        FindState findState = prepareFindState();
        findState.initForSubscriber(subscriberClass);
        while (findState.clazz != null) {
            findUsingReflectionInSingleClass(findState);
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);
    }
复制代码

我们看到,其实跟findUsingInfo的源码差不多,只是没有调用getSubscriberInfo,而是直接调用了findUsingReflectionInSingleClass

我们在回到register方法

public void register(Object subscriber) {
        Class<?> subscriberClass = subscriber.getClass();
        List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
        synchronized (this) {
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
                subscribe(subscriber, subscriberMethod);
            }
        }
    }
复制代码

拿到订阅方法的集合之后,循环遍历得到subscriberMethod,然后调用subscribe(subscriber, subscriberMethod);

我们先来简单说下这个方法的作用

  • subscriptionsByEventType添加数据
  • typesBySubscriber添加数据
  • 处理事件的优先级
  • 处理粘性事件
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        // 从订阅方法中得到事件类型
        Class<?> eventType = subscriberMethod.eventType;
        // 构建Subscription
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
        // 判断subscriptionsByEventType有没有,没有的话构建出来,然后往里面添加数据
        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();
        // 处理事件的优先级
        //  这里判断subscriberMethod的优先级是否是大于集合中的subscriberMethod的优先级,如果是,把newSubscription插进去
     //  这也表明了subscription中priority大的在前,这样在事件分发时就会先获取。
        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);
            }
        }
    }
复制代码

接下来看一下unregister方法

public synchronized void unregister(Object subscriber) {
        // typesBySubscriber通过subscriber去拿到所有的事件类型
        List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
        if (subscribedTypes != null) {
            for (Class<?> eventType : subscribedTypes) {
            // 重点看下这个方法
                unsubscribeByEventType(subscriber, eventType);
            }
            // 从集合中移除
            typesBySubscriber.remove(subscriber);
        } else {
            logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
        }
    }
    
    
    
复制代码
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--;
                }
            }
        }
    }
复制代码

接下来我们看下post的源码

post

我们先来了解下EventBus的一个内部类PostingThreadState

final static class PostingThreadState {
        // 事件队列
        final List<Object> eventQueue = new ArrayList<>(); 
        // 是否正在发送事件
        boolean isPosting;
        // 是否在祝线程
        boolean isMainThread;
        // 封装了订阅类和订阅方法的对象
        Subscription subscription;
        // 事件
        Object event;
        // 是否取消
        boolean canceled;
    }
复制代码

EventBus.post()

private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
        @Override
        protected PostingThreadState initialValue() {
            return new PostingThreadState();
        }
    };
    
    
 public void post(Object event) {
 
        PostingThreadState postingState = currentPostingThreadState.get();
        List<Object> eventQueue = postingState.eventQueue;
        eventQueue.add(event);
// 这个线程必须没事件正在发送,才会去处理事件
        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()) {
                // 重点分析下这个方法
                    postSingleEvent(eventQueue.remove(0), postingState);
                }
            } finally {
                postingState.isPosting = false;
                postingState.isMainThread = false;
            }
        }
    }
复制代码

从上面的代码中的判断if (!postingState.isPosting)我们可以知道,不要在被订阅的方法中往同一个线程中再次发送是事件,因为这样是不处理的,所以我们收不到这个事件

下面我们来看一下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) {
                logger.log(Level.FINE, "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) {
// 通过subscriptionsByEventType,根据eventType拿到订阅的集合
        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方法,主要做的就是线程的调度,主要利用Handler和前面介绍的线程池

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);
        }
    }
复制代码

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);
        }
    }
复制代码

上面的整个流程分析下来,我们大概看的出来,APT插件解决的只是EventBus.register的时候,方法的获取不使用反射,但是在EventBus.post的时候,还是通过反射去执行注解的方法

一直再说APT,那么究竟是什么,该怎么用

在3.0之后增加了注解处理器,在程序的编译时候,就可以根据注解生成相对应的代码,相对于之前的直接通过运行时反射,大大提高了程序的运行效率 那我们怎么配置让EventBus使用注解器生成的代码呢?,SubscriberInfoIndex就是一个接口,而注解生成器生成的类也是继承它 我们先回到前面查找注册方法的代码

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);
    }
复制代码

我么前面分析的时候,直接走了else的分析,那么if分之什么时候会执行呢?getSubscriberInfo(findState) != 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;
           }
       }
      //  判断subscriberInfoIndexes 是否为null,默认为null,当我们使用apt插件构建代码 的时候,可以手动的调用EventBusBuilder的addIndex,将subscriberInfoIndexes 进行赋值。
       if (subscriberInfoIndexes != null) {
           for (SubscriberInfoIndex index : subscriberInfoIndexes) {
               SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
               if (info != null) {
                   return info;
               }
           }
       }
       return null;
   }
   
复制代码

那么我们该如何配置呢? 首先我们在我们的Activity里面写两个订阅方法

@Subscribe(threadMode = ThreadMode.MAIN,sticky = true,priority = 10)
    public void getEventbusData(String string){
        Log.e("WFQ", "getEventbusData: " + string);
    }
    @Subscribe(threadMode = ThreadMode.BACKGROUND,sticky = false,priority = 100)
    public void getEventbusData2(Integer integer){
        Log.e("WFQ", "getEventbusData2: " + integer);
    }
    
复制代码

配置我们的gradle

android {
    defaultConfig {

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [eventBusIndex : applicationId + '.MyEventBusIndex']
            }
        }
    }
}



implementation 'org.greenrobot:eventbus:3.1.1'
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
复制代码

build项目,这个时候就会生成我们的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.wfq.demo0815.RxJavaMVP.demo.Demo1Activity.class, true,
                new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("getEventbusData", String.class, ThreadMode.MAIN, 50, true),
            new SubscriberMethodInfo("getEventbusData2", Integer.class, ThreadMode.BACKGROUND, 100, false),
        }));

    }

    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.getDefault().register();

getdefault采用单例模式和建造者模式构建EventBus的实例,在构建实例的时候初始化了线程池,还初始化了几个map,分别是subscriptionsByEventType:根据eventType去找到Subscription的集合,这个集合是线程安全的(COW), typesBySubsciber:根据订阅类去找到eventType的集合类,其实这两个是有关联的,根据订阅类去找到eventType的结合,遍历集合,得到eventType,然后根据eventType去subscriptionsByEventType中去找到Subscription的集合,遍历集合的得到subcription,这个是封装了订阅类和订阅方法的一个对象,拿到订阅方法之后我们就可以在发送事件时候去调用被订阅的方法了。那么这两个集合都是在哪里进行put的呢? 查找到订阅方法之后调用了subscribe,在这个方法里面去put的,我们来看下怎么查找订阅方法的?

在register里面,调用了subscriberMethodFinder.findSubscriberMethods(subscriberClass) 在这个方法里,会先判断缓存中有没有这个类订阅的方法的集合,有的话直接将集合返回,没有的话会去判断 ignoreGeneratedIndex参数,该参数表示是否忽略APT,默认为false,也就是使用APT为我们生成的文件,调用 findUsingInfo(subscriberClass),在这个方法里,会使用享元模式,实例话FindState,这个FindState就是用来保存订阅类的一些信息,包括订阅方法的集合等,然后会去调用getSubscriberInfo(findState),即去APT为我们生成的文件中去寻找注册的方法,找打的话会把它添加在findstate的注册方法的集合中去,找不到的话就使用反射去获取这个订阅类的所有注册方法,然后将结果添加到findState的注册方法的集合中去,最后调用getMethodsAndRelease, 在这个方法里,会新建集合,获取findState里方法的集合,并释放findState里面的参数

EventBus.getDefault().post();

创建了一个ThreadLocal,PostingThreadState里的变量: List eventQueue isPosting isMainThread Subscription event calceled

post的作用就是将事件添加到PostingThreadState的事件队列中去,然后遍历这个List,调用postSingleEvent, 既然要发送单个事件,就要知道订阅这个事件的所有方法,通过subscriptionsByEventType拿到Subscriptions的COW,接着遍历这个线程安全的类,拿到SUbscription,然后调用postToSubscrition,在这个方法里进行了线程调度, 最终都会调用invokeSubscriber,在这里通过反射调用方法


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

笨办法学Python 3

笨办法学Python 3

[美]泽德 A. 肖 / 王巍巍 / 人民邮电出版社 / 2018-6-1 / CNY 59.00

本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用。这本书以习题的方式引导读者一步一步学习编程,从简单的打印一直讲到完整项目的实现,让初学者从基础的编程技术入手,最终体验到软件开发的基本过程。本书是基于Python 3.6版本编写的。 本书结构非常简单,除“准备工作”之外,还包括52个习题,其中26个覆盖了输入/输出、变量和函数3个主题,另外26个......一起来看看 《笨办法学Python 3》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具