EventBus简析

1.基本使用

1.1 定义事件

public static class MessageEvent { /* Additional fields if needed */ }

1.2 在需要订阅的方法上添加注解并制定方法执行的线程,在相应的生命周期中注册以及反注册

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};
@Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }

1.3 发送事件

EventBus.getDefault().post(new MessageEvent());

通过上面简单的3个步骤,就可以完成事件的发布以及订阅。

2.主要原理

  • 注册
EventBus.getDefault().register(this);

EventBus.getDefault()双重校验锁单列模式创建默认的EventBus

public static EventBus getDefault() {
    EventBus instance = defaultInstance;
    if (instance == null) {
        synchronized (EventBus.class) {
            instance = EventBus.defaultInstance;
            if (instance == null) {
                instance = EventBus.defaultInstance = new EventBus();
            }
        }
    }
    return instance;
}

register方法主要完成了订阅对象中的所有订阅方法的查找,然后将订阅者、订阅方法、订阅方法的参数类型按照一定关系通过map缓存起来,便于post事件时候订阅方法的执行以及最后的注销

public void register(Object subscriber) {
    // 拿到当前对象的class
    Class subscriberClass = subscriber.getClass();
    // 1.通过subscriberMethodFinder来查找当前对象中的所有订阅方法
    List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
    synchronized (this) {
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            // 2.将订阅者、订阅方法、订阅方法的参数类型缓存起来
            subscribe(subscriber, subscriberMethod);
        }
    }
}

SubscriberMethodFinder是查找订阅方法的辅助类,如果我们没用AnnotationProcessor生成index,ignoreGeneratedIndex就是false,先看通过反射来获取到订阅者所有添加了Subscribe注解的方法

List findSubscriberMethods(Class subscriberClass) {
    // 缓存中取所有订阅的方法,SubscriberMethod是一个订阅方法封装的实体类
    List subscriberMethods = METHOD_CACHE.get(subscriberClass);
    // 缓存中存在直接返回
    if (subscriberMethods != null) {
        return subscriberMethods;
    }
    // ignoreGeneratedIndex是否使用反射查找,默认false
    if (ignoreGeneratedIndex) {
        subscriberMethods = findUsingReflection(subscriberClass);
    } else {
        // 这里最后还是会调用findUsingReflectionInSingleClass
        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;
    }
}

findUsingInfo方法最终会调用findUsingReflectionInSingleClass,来完成订阅对象以及其非系统父类中的订阅方法的扫描,主要借助了SubscriberMethodFinder的静态内部类FindState

private List findUsingInfo(Class subscriberClass) {
    // 从FIND_STATE_POOL中查找一个FindState,FIND_STATE_POOL就是一个默认size为4的FindState数组
    FindState findState = prepareFindState();
    // 初始化传入subscriberClass
    findState.initForSubscriber(subscriberClass);
    while (findState.clazz != null) {
        // 这里按默认配置取出来是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);
}

findUsingReflectionInSingleClass方法通过反射获取当前类中添加了Subscribe注解的public的非static非abstract的只有一个参数的方法,放入到以订阅者的class类型为key方法集合为value的Map中

// 订阅方法缓存的Map
private static final Map, List> METHOD_CACHE = new ConcurrentHashMap<>();
    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) {
        // 获取方法的修饰符 PUBLIC: 1 STATIC: 8
        int modifiers = method.getModifiers();
        // 这里判断订阅方法必须是public非static非abstract
        if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
            // 拿到参数的类型
            Class[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length == 1) {// 方法参数个数必须为1
                // 获取方法的注解
                Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
                if (subscribeAnnotation != null) {
                    // 拿到唯一参数的类型
                    Class eventType = parameterTypes[0];
                    if (findState.checkAdd(method, eventType)) {
                        // 拿到注解上的线程类型,没指定默认是ThreadMode.POSTING,也就是post事件的线程
                        ThreadMode threadMode = subscribeAnnotation.threadMode();
                        // 将订阅方法封装成SubscriberMethod添加到list中
                        findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                                subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                    }
                }
                // 不为1抛出异常
            } 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");
        }
    }
}

再来分析使用了annotationProcessorOptions的情况,在build.gradle中增加以下配置

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ]
            }
        }
    }
}
 
dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

在我们编译后会生成MyEventBusIndex类,创建EventBus时只需要传入MyEventBusIndex

// 初始化传入MyEventBusIndex
EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();

apt生成的MyEventBusIndex

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

    static {
        SUBSCRIBER_INDEX = new HashMap, SubscriberInfo>();
        // 将订阅的方法添加到SUBSCRIBER_INDEX map中
        putIndex(new SimpleSubscriberInfo(com.dream.eventbustest.MainActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("test", String.class),
        }));

    }

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

这样在findUsingInfo方法中就可以直接获取到订阅方法集合,避免了在运行时使用反射,提高性能。


接下来继续看subscribe(subscriber, subscriberMethod)方法,里面涉及到了几个map:

// key为订阅方法的参数类型的class,value是对订阅者以及订阅方法的封装的集合,
// 便于post事件时反射执行所有订阅的方法
private final Map, CopyOnWriteArrayList> subscriptionsByEventType;

// key为订阅者,value为当前订阅者中所有订阅方法参数类型class的集合,
// 主要用于注销时,清除subscriptionsByEventType中的订阅方法
private final Map>> typesBySubscriber;

// key为订阅方法的参数类型class,value是订阅方法的参数对象
// 主要是在发送粘性事件保存数据,以便在注册时能够执行到先订阅的粘性事件方法
private final Map, Object> stickyEvents;

上面分析了涉及的几个容器的作用,具体的处理过程看subscribe,在subscribe方法中还有对订阅方法优先级的处理以及粘性事件的处理

// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
    // 这里eventType是订阅方法的参数类型
    Class eventType = subscriberMethod.eventType;
    // 将订阅者与订阅方法封装到Subscription实体类中
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
    // key为eventType, value为CopyOnWriteArrayList的map中获取之前是否添加过
    CopyOnWriteArrayList subscriptions = subscriptionsByEventType.get(eventType);
    if (subscriptions == null) {
        // 没取到说明是第一次订阅参数类型为eventType的方法
        subscriptions = new CopyOnWriteArrayList<>();
        // 加入到map中
        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++) {
        // 根据优先级加入到list中,优先级高加在前面,在后面post的时候就会先执行
        if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
            subscriptions.add(i, newSubscription);
            break;
        }
    }
    // typesBySubscriber这个map就是key为订阅对象,value为所有订阅方法的class的list集合
    List> subscribedEvents = typesBySubscriber.get(subscriber);
    if (subscribedEvents == null) {
        // 还是同样的操作,取不到新创建一个,添加进去
        subscribedEvents = new ArrayList<>();
        typesBySubscriber.put(subscriber, subscribedEvents);
    }
    subscribedEvents.add(eventType);
    // 如果是粘性事件,这里说明下在Android开发中,Sticky事件指事件消费者在事件发布之后才注册的也能接收到该事件的特殊类型。
    if (subscriberMethod.sticky) {
        if (eventInheritance) {// 这里默认为true
            // 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).

            // 这里stickyEvents是一个map,在postSticky时会添加事件进去
            Set, Object>> entries = stickyEvents.entrySet();
            for (Map.Entry, Object> entry : entries) {
                Class candidateEventType = entry.getKey();
                if (eventType.isAssignableFrom(candidateEventType)) {
                    Object stickyEvent = entry.getValue();
                    // 这里就直接发送Sticky事件,因为之前以前调用了postSticky,所以注册后直接发送
                    checkPostStickyEventToSubscription(newSubscription, stickyEvent);
                }
            }
        } else {
            Object stickyEvent = stickyEvents.get(eventType);
            checkPostStickyEventToSubscription(newSubscription, stickyEvent);
        }
    }
}
  • 发送事件

主要就是将事件加入到list中,然后逐个的发送

public void post(Object event) {
    // currentPostingThreadState是一个ThreadLocal,来保存各自线程的PostingThreadState
    PostingThreadState postingState = currentPostingThreadState.get();
    List eventQueue = postingState.eventQueue;
    // 将事件加入到list中
    eventQueue.add(event);

    if (!postingState.isPosting) {// 默认false,标记是否发送事件
        // 判断是否是主线程
        postingState.isMainThread = isMainThread();
        postingState.isPosting = true;// 开始发送事件
        if (postingState.canceled) {
            throw new EventBusException("Internal error. Abort state was not reset");
        }
        try {
            while (!eventQueue.isEmpty()) {
                // 将list中的所有事件发送出去
                postSingleEvent(eventQueue.remove(0), postingState);
            }
        } finally {
            // 最后将标志置为false
            postingState.isPosting = false;
            postingState.isMainThread = false;
        }
    }
}
 
 

postSingleEvent

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
    // 拿到发送事件的class类型
    Class eventClass = event.getClass();
    boolean subscriptionFound = false;
    if (eventInheritance) {// 默认true
        // 查找当前事件类型的所有父类以及实现接口
        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) {
            logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
        }
        if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
                eventClass != SubscriberExceptionEvent.class) {
            post(new NoSubscriberEvent(this, event));
        }
    }
}

postSingleEventForEventType

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class eventClass) {
    CopyOnWriteArrayList 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方法才真正的通过订阅方法的线程情况来反射执行

private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
    switch (subscription.subscriberMethod.threadMode) {
        case POSTING:
            // 反射执行订阅方法在post事件的线程
            invokeSubscriber(subscription, event);
            break;
        case MAIN:
            if (isMainThread) {
                // post线程是主线程直接执行
                invokeSubscriber(subscription, event);
            } else {
                // 加入到队列中,然后通过handler来实现线程切换
                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);
    }
}
  • 注销

将Map中保存的当前订阅者的所有订阅方法的信息移除

/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object subscriber) {
    // 拿到当前订阅者中所有订阅方法的参数类型
    List> subscribedTypes = typesBySubscriber.get(subscriber);
    if (subscribedTypes != null) {
        for (Class eventType : subscribedTypes) {
            // 通过eventType解除订阅
            unsubscribeByEventType(subscriber, eventType);
        }
        typesBySubscriber.remove(subscriber);
    } else {
        logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
    }
}
/** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
private void unsubscribeByEventType(Object subscriber, Class eventType) {
    // 通过订阅方法的参数类型拿到所有的订阅方法信息
    List 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--;
            }
        }
    }
}

3.总结

EventBus的整个事件流程还是很清晰的,一张图展示整个关系。


EventBus简析_第1张图片
EventBus-Publish-Subscribe.png

你可能感兴趣的:(EventBus简析)