Android事件总线框架设计:EventBus3.0源码详解与架构分析(下)

博客主页

1.粘性事件

粘性事件就是在发送事件之后再订阅该事件也能收到该事件,跟黏性广播类似。

首先看下粘性事件的发布方式:

EventBus.getDefault().postSticky("hello, eventbus!");

我们看下发布粘性事件方法的源码

    private final Map, Object> stickyEvents;

    public void postSticky(Object event) {
        synchronized (stickyEvents) {
            // 先将事件缓存在stickyEvents中
            // stickyEvents是一个Map,key是事件类型,value是事件
            stickyEvents.put(event.getClass(), event);
        }
        // Should be posted after it is putted, in case the subscriber wants to remove immediately
        // 然后把事件发布出去,如果没有注册,不会执行订阅事件方法
        post(event);
    }

postSticky方法主要做了两件事情,先是把事件缓存在stickyEvents中,然后通过post方法把事件发布出去,这个方法之前已经分析过,这里不在分析了。

发布完粘性事件后,当订阅粘性事件方法,完成注册后就会立即执行。核心的注册流程是之前分析的register方法,其中subscribe方法一段代码就是处理粘性事件

    private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        Class eventType = subscriberMethod.eventType;
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
        // ......

        // 如果在订阅事件方法时,@Subscribe注解中的sticky设置为true,代表粘性事件
        if (subscriberMethod.sticky) {
            // eventInheritance默认为true,代表继承关系事件也会被执行
            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);
            }
        }
    }

可以看出,处理粘性事件是在注册时,遍历stickyEvents,然后交给checkPostStickyEventToSubscription处理

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

checkPostStickyEventToSubscription方法最终调用线程切换方法postToSubscription,由它完成事件的处理。

2.Subscriber Index

前面分析EventBus注册时,默认实现是通过反射技术查找注册类中所有的订阅事件的方法,如果当前注册类中有很多订阅事件方法,通过反射技术就会影响运行时性能。所以在EventBus3.0通过APT(注解处理器)技术在编译器就查找所有订阅事件方法,生成一个辅助的索引SUBSCRIBER_INDEX保存所有的订阅方法。

要在项目编译时查找订阅事件的方法信息,首先要在 app 的 build.gradle 中加入如下配置:

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                // APT 设置的参数,指定辅助索引类名和包名
                arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ]
            }
        }
    }
}

dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    // 引入注解处理器
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

然后在项目的 Application 中添加如下配置,以生成一个默认的 EventBus 单例:

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();

EventBus eventBus = EventBus.getDefault();

编译后,就会生成MyEventBusIndex类,源码如下:

public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map, SubscriberInfo> SUBSCRIBER_INDEX;

    static {
        SUBSCRIBER_INDEX = new HashMap, SubscriberInfo>();

        putIndex(new SimpleSubscriberInfo(com.example.eventbus.demo.MainActivity.class, true,
                new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onMessageEventMain", com.example.eventbus.demo.MessageEvent.class,
                    ThreadMode.MAIN, 0, true),
        }));

        putIndex(new SimpleSubscriberInfo(com.example.eventbus.demo.TestActivity.class, true,
                new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onMessageEventMainForTest", com.example.eventbus.demo.MessageEvent.class,
                    ThreadMode.MAIN, 0, true),
        }));

    }

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

其中SUBSCRIBER_INDEX是一个Map,保存当前注册类的Class和订阅事件方法信息。

我们先从使用MyEventBusIndex这个索引类方式入手

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();

调用EventBus的builder方法创建一个EventBusBuilder对象,然后调用它的addIndex方法,添加索引类

    public EventBusBuilder addIndex(SubscriberInfoIndex index) {
        if (subscriberInfoIndexes == null) {
            subscriberInfoIndexes = new ArrayList<>();
        }
        subscriberInfoIndexes.add(index);
        return this;
    }

从上面代码可以看出,把生成的索引类添加到subscriberInfoIndexes集合中,然后调用installDefaultEventBus方法,创建默认的EventBus实例

    public EventBus installDefaultEventBus() {
        synchronized (EventBus.class) {
            if (EventBus.defaultInstance != null) {
                throw new EventBusException("Default instance already exists." +
                        " It may be only set once before it's used the first time to ensure consistent behavior.");
            }
            EventBus.defaultInstance = build();
            return EventBus.defaultInstance;
        }
    }

    public EventBus build() {
        // this代表是EventBusBuilder实例,这样EventBus就可以拿到subscriberInfoIndexes集合
        return new EventBus(this);
    }

其中subscriberInfoIndexes集合通过EventBus构造传给EventBus

    EventBus(EventBusBuilder builder) {
        // ...
        indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
        subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
                builder.strictMethodVerification, builder.ignoreGeneratedIndex);
        // ...
    }

之前在分析注册流程的时,其中findUsingInfo方法就会处理这个索引类,我们在来看下源码

    private List findUsingInfo(Class subscriberClass) {
        // FindState
        FindState findState = prepareFindState();
        findState.initForSubscriber(subscriberClass);
        while (findState.clazz != null) {
            // 如果没有使用APT(注解处理器)生成订阅方法索引,返回null,则进入else语句中
            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);
            }
            // 从父类中继续查找,直到父类为null
            findState.moveToSuperclass();
        }
        // 返回注册类中所有的订阅方法,并释放findState中状态,同时把findState对象放回缓存池中
        return getMethodsAndRelease(findState);
    }

当我们使用APT生成了并使用了索引类的时,就不会通过反射技术查找注册类中所有的订阅事件方法了。

    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就是通过addIndex方法添加索引类集合
        if (subscriberInfoIndexes != null) {
            for (SubscriberInfoIndex index : subscriberInfoIndexes) {
                SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
                if (info != null) {
                    return info;
                }
            }
        }
        return null;
    }

subscriberInfoIndexes集合就是通过addIndex方法创建的,并保存了索引类实例MyEventBusIndex。

使用Subscriber Index,避免了通过反射技术处理,提高了性能。

3.AsyncExecutor

AsyncExecutor与线程池类似,但是可以处理运行异常。

    public static AsyncExecutor create() {
        return new Builder().build();
    }

通过AsyncExecutor静态方法create可以创建一个AsyncExecutor实例对象,然后调用它的execute方法执行任务

    public void execute(final RunnableEx runnable) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } catch (Exception e) {
                    Object event;
                    try {
                        event = failureEventConstructor.newInstance(e);
                    } catch (Exception e1) {
                        eventBus.getLogger().log(Level.SEVERE, "Original exception:", e);
                        throw new RuntimeException("Could not create failure event", e1);
                    }
                    if (event instanceof HasExecutionScope) {
                        ((HasExecutionScope) event).setExecutionScope(scope);
                    }
                    eventBus.post(event);
                }
            }
        });
    }

当发生异常时,会发布ThrowableFailureEvent事件,我们可以订阅该事件进行相应的处理。execute方法接收的参数是RunnableEx对象

    public interface RunnableEx {
        void run() throws Exception;
    }

RunnableEx中的run方法是可以抛出异常的。

我们可以根据AsyncExecutor另一个静态方法builder,修改默认的线程池threadPool,失败的事件类型failureEventType

// AsyncExecutor.java源码
    public static Builder builder() {
        return new Builder();
    }

   public static class Builder {
        private Executor threadPool;
        private Class failureEventType;

        public Builder threadPool(Executor threadPool) {
            this.threadPool = threadPool;
            return this;
        }

        public Builder failureEventType(Class failureEventType) {
            this.failureEventType = failureEventType;
            return this;
        }
   }

EventBus到此所有的源码已经分析完了~~~

如果我的文章对您有帮助,不妨点个赞鼓励一下(^_^)

你可能感兴趣的:(android,eventbus)