EventBus源碼理解

EventBus源碼理解

EventBus是我們在開發中經常使用的開源庫,使用起來比較簡單,而且源碼看起來不是很吃力。受到廣大開發者的喜愛~

綜述 !


EventBus-Publish-Subscribe.png

上面這張圖片很好的解釋了EventBus工作流程,簡單來說就是事件被提交到EventBus之后進行查找所有訂閱該事件的方法然后執行這些方法.

獲取EventBus實例(單例模式)

使用了雙重判斷的方式,防止并發的問題,還能極大的提高效率。

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

構造方法

public EventBus() {
        this(DEFAULT_BUILDER);
    }

注冊

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

這里面其中參數就是訂閱者,也就是我們寫的this,register方法主要完成兩件事,查找訂閱者中所有的訂閱方法,然后通過遍利訂閱著的訂閱方法完成訂閱操作。我們首先看下findSubscriberMethods這個方法:

//從緩存中獲取SubscriberMethod集合
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }
//ignoreGeneratedIndex是否忽略注解器生成的MyEventBusIndex
        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;
        }
    }

SubscriberMethod 這個類中主要是用保存訂閱方法的Method對象,線程模式,事件類型,優先級,是否粘性事件等屬性,主要是兩個方法findUsingReflection(subscriberClass),findUsingInfo(subscriberClass),這兩個方法的區別就是有沒有配置subscriberInfo

findUsingInfo

 private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
 //在FindState里面,它保存了一些訂閱者的方法以及對訂閱方法的校驗
        FindState findState = prepareFindState();
        findState.initForSubscriber(subscriberClass);
        // 如果我們通過EventBusBuilder配置了MyEventBusIndex,便會獲取到subscriberInfo 通常情況下我們下代碼的時候并沒有配置~
        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);
    }

# findUsingReflectionInSingleClass

  • 沒有通過EventBusBuilder配置MyEventBusIndex的情況下就執行這個方法了
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) {
                     //保存到findState對象當中
                        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");
            }
        }
    }

回到register這個方法中,上面我們分析了尋找訂閱著方法部分,接下來就是注冊了

subscribe

// Must be called in synchronized block
    private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
    //獲取訂閱者方法中的訂閱事件
        Class<?> eventType = subscriberMethod.eventType;
        //創建一個Subscription來保存訂閱者和訂閱方法
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
        //獲取當前訂閱事件中Subscription的List集合
        CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
        if (subscriptions == null) {
         //該事件對應的Subscription的List集合不存在,則重新創建并保存在subscriptionsByEventType中
            subscriptions = new CopyOnWriteArrayList<>();
            subscriptionsByEventType.put(eventType, subscriptions);
        } else {
        //肯定藥判斷訂閱者是否已經被注冊啦
            if (subscriptions.contains(newSubscription)) {
                throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
                        + eventType);
            }
        }
//將newSubscription按照訂閱方法的優先級插入到subscriptions中
        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中
        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);
            }
        }
    }

這個方法才是真正的注冊,上面我們說的知識尋找訂閱者訂閱事件的方法。概括來說首先會根據subscriber和subscriberMethod來創建一個Subscription集合subscriptions,然后根據事件類型eventType獲取事件集合并把他們添加到typesBySubscriber中,然后把Subscription對象添加到subscriptions中。

事件的發送

首先藥獲取EventBus對象,然后通過Post方法進行事件的發送

   
    public void post(Object event) {
    //PostingThreadState保存著事件隊列和線程狀態信息
        PostingThreadState postingState = currentPostingThreadState.get();
        //獲取事件隊列,并將當前事插入到事件隊列中
        List<Object> 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;
            }
        }
    }

上面在訂閱的時候我們以訂閱事件為key,將Subscription的List集合作為Value保存到了一個Map中 ,下面這個方法就是通過key來取出集合

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

這就是我們在接收信息的時候所用到的幾個方法,具體含義就不再啰嗦了。到這里啊管理EventBus所涉及的源碼分析的差不多了,雖然還有好多地方沒有分析到位,但大體的思路是有的。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 原文鏈接:http://blog.csdn.net/u012810020/article/details/7005...
    tinyjoy閱讀 565評論 1 5
  • 項目到了一定階段會出現一種甜蜜的負擔:業務的不斷發展與人員的流動性越來越大,代碼維護與測試回歸流程越來越繁瑣。這個...
    fdacc6a1e764閱讀 3,210評論 0 6
  • 先吐槽一下博客園的MarkDown編輯器,推出的時候還很高興博客園支持MarkDown了,試用了下發現支持不完善就...
    Ten_Minutes閱讀 582評論 0 2
  • 前邊文章主要跟大家大概講了下EventBus的用法和注解,接下來則是從源碼角度來看EventBus的內部處理 Ev...
    Hohohong閱讀 3,255評論 1 5
  • EventBus注冊與反注冊流程源碼分析 從上述代碼可以看出,注冊的大致流程為: 查找到訂閱者類的所有訂閱方法 注...
    嘎啦果安卓獸閱讀 1,201評論 0 6