Android事件總線:EventBus

最近去維護另外一個項目,各種庫使用的都比較老,eventbus使用的是2.x版本,於是來個升級,順便讀下eventbus的源碼,在此做個筆記:

EventBus2.x升級3.x
2.x與3.x之間的對應關係:
onEvent--註解ThreadMode.POSTING;
onEventMainThread--註解ThreadMode.MAIN
onEventAsync--註解ThreadMode.BACKGROUND
onEventBackgroundThread--註解ThreadMode.ASYNC
1.包名替換

Eventbus升級之後包名變了,可以ctrl+shift+r全局替換,也可以給studio設置自動導入包名將
import de.greenrobot.event.EventBus;替換成 import org.greenrobot.eventbus.EventBus; 全部替換.

2.方法需要使用註解方式

ctrl+shift+f 全局搜索, 依次添加上面提到的4個註解方法.
例如對onEvent方法,搜索"public void onEvent (" 添加"@Subscribe(threadMode = ThreadMode.POSTING)"
3.EventBus 3.0版本去掉了registerSticky, 換成到每個方法的註解中配置sticky = true. 搜索".registerSticky(", registerSticky替換回register, 再在該類下的方法註解上添加sticky.

EventBus源碼解讀:
1.EventBus的構造方法
/** 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的構造方法都做了什麼:

...
private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
...
public EventBus() {
    this(DEFAULT_BUILDER);
}

this調用了eventbus的另一個構造方法:

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

這裏採用建造者模式,通過構造一個EventbusBuilder來對Eventbus進行配置

2.訂閱註冊

獲取到Eventbus後,就可以將訂閱者註冊到Eventbus中,註冊方法如下:

public void register(Object subscriber) {
    Class<?> subscriberClass = subscriber.getClass();
    //查找訂閱者(傳進來的subscriber)的所有訂閱方法的集合
    List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
    synchronized (this) {
        //遍歷訂閱者的方法,完成註冊
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }
    }
}
1)查找訂閱者的訂閱方法

可以看到,註冊方法做了兩件事:①查找訂閱者中所有訂閱方法,②訂閱者的註冊。SubscriberMethod類主要用來保存訂閱方法Method對象、線程模式(threadMode)、事件類型(eventType)、優先級(priority)、是否粘性事件(sticky)等。findSubscriberMethods如下:

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
    //檢查緩存中是否有
    List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
    if (subscriberMethods != null) {
        return subscriberMethods;
    }
    ```
    //判斷是否忽略註解器生成的MyEventBusIndex(參考http://greenrobot.org/eventbus/documentation/subscriber-index/)
    //默認false,可以通過EventbusBuilder來設置。因此我們使用通常使用的是findUsingInfo方法來獲取訂閱方法集合
    ```
    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;
    }
}

接下來看下findUsingInfo方法:

private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
    FindState findState = prepareFindState();
    findState.initForSubscriber(subscriberClass);
    while (findState.clazz != null) {
        //獲取訂閱者信息
        findState.subscriberInfo = getSubscriberInfo(findState);
        //如果我們通過EventBuilder配置了MyEventBusIndex,便會獲取到subscriberInfo
        if (findState.subscriberInfo != null) {
            SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
            for (SubscriberMethod subscriberMethod : array) {
                if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
                    findState.subscriberMethods.add(subscriberMethod);
                }
            }
            //沒有通過EventBuilder配置MyEventBusIndex,調用此方法將訂閱信息保存到findState,默認沒有配置,走此方法
        } else {
            findUsingReflectionInSingleClass(findState);
        }
        findState.moveToSuperclass();
    }
    //對findState做回收處理,並返回訂閱方法的集合
    return getMethodsAndRelease(findState);
}

more沒有配置MyEventBusIndex,下面看下findUsingReflectionInSingleClass方法:

private void findUsingReflectionInSingleClass(FindState findState) {
    Method[] methods;
    try {
        //通過反射獲取訂閱者中的所有方法,這個方法比getMethods()快,特別是代碼比較多的類如Activity
        methods = findState.clazz.getDeclaredMethods();
    } catch (Throwable th) {
        methods = findState.clazz.getMethods();
        findState.skipSuperClasses = true;
    }
    for (Method method : methods) {
        //獲取方法的修飾符,eg. public
        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中
                        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");
        }
    }
}

至此,查找訂閱者的訂閱方法結束。

2)訂閱者的註冊過程

查找完訂閱者的訂閱方法之後,就開始對再所有訂閱者方法進行註冊,回到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);
    //根據eventType獲取訂閱對象集合,如果爲空則重新創建
    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();
    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);
        }
    }
}

subscribe主要做了兩件事,①將Subscription根據evntType封裝到subscriptionsByEventType中,將subscribedEvents根據subscriber封裝到typeBySubscriber中;②對粘性事件的處理。

3.事件發送

在獲取到Eventbus對象之後,通過post方法,完成對事件的發送:

/** Posts the given event to the event bus. */
public void post(Object event) {
    //PostingThreadState 保存着事件隊列(eventQueue)和線程狀態信息(isPosting,isMainThread,subscription,event,canceled)
    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;
        }
    }
}

看看postSingleEvent做了什麼:

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
    Class<?> eventClass = event.getClass();
    boolean subscriptionFound = false;
    //是否向上查找父類,默認true,可以通過EventBusBuilder配置
    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));
        }
    }
}

通過postSingleEventForEventType方法對事件進行處理:

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
    CopyOnWriteArrayList<Subscription> subscriptions;
    synchronized (this) {
        //取出事件對應的訂閱對象集合
        subscriptions = subscriptionsByEventType.get(eventClass);
    }
    if (subscriptions != null && !subscriptions.isEmpty()) {
        //將事件event和對應的Subscription(訂閱對象)傳遞給postingState,通過調用postToSubscription方法對事件進行處理
        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:
            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);
    }
}

根據訂閱方法的線程模式,分別處理,如果是Main,如果提交的線程是主線程,則通過反射直接運行訂閱的方法,若不是主線程,需要mainThreadPoster將我們的訂閱事件添加到主線程隊列中。mainThreadPoster繼承自Handler,通過Handler將訂閱方法切換到主線程。
至此,事件發送結束。

4.訂閱者取消註冊

取消註冊是獲取Eventbus對象之後,調用unregieter方法。

/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object 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());
    }
}

/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object 
typesBySubscriber是一個map集合,在訂閱的時候使用過。先通過subscriber找到subscribedTypes集合,然後將subscriber對應的eventTpye從typesBySubscriber中移除。遍歷subscribedTypes並調用unsubscribeByEventType方法:
/** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
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--;
            }
        }
    }
}

從subscriptions移除Subscriptions中移除,完成取消訂閱。

參考資料:
1.https://www.jianshu.com/p/e28e1692d0c7
2.https://github.com/greenrobot/EventBus
3.《Android進階之光》(劉望舒)第七章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章