1. 程式人生 > >自己動手寫事件總線(EventBus)

自己動手寫事件總線(EventBus)

equal 觀察者 tcl 由於 初始化 pat scribe 倉庫 最終

本文由雲+社區發表

事件總線核心邏輯的實現。

EventBus的作用

Android中存在各種通信場景,如Activity之間的跳轉,ActivityFragment以及其他組件之間的交互,以及在某個耗時操作(如請求網絡)之後的callback回調等,互相之之間往往需要持有對方的引用,每個場景的寫法也有差異,導致耦合性較高且不便維護。以ActivityFragment的通信為例,官方做法是實現一個接口,然後持有對方的引用,再強行轉成接口類型,導致耦合度偏高。再以Activity的返回為例,一方需要設置setResult,而另一方需要在onActivityResult做對應處理,如果有多個返回路徑,代碼就會十分臃腫。而SimpleEventBus

(本文最終實現的簡化版事件總線)的寫法如下:


public class MainActivity extends AppCompatActivity {

    TextView mTextView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mTextView = findViewById(R.id.tv_demo);

        mTextView.setText("MainActivity");

        mTextView.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);

                startActivity(intent);

            }

        });

        EventBus.getDefault().register(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)

    public void onReturn(Message message) {

        mTextView.setText(message.mContent);

    }

    @Override

    protected void onDestroy() {

        super.onDestroy();

        EventBus.getDefault().unregister(this);

    }

}

來源Activity


public class SecondActivity extends AppCompatActivity {

    TextView mTextView;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mTextView = findViewById(R.id.tv_demo);

        mTextView.setText("SecondActivity,點擊返回");

        mTextView.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Message message = new Message();

                message.mContent = "從SecondActivity返回";

                EventBus.getDefault().post(message);

                finish();

            }

        });

    }

}

效果如下:

技術分享圖片

似乎只是換了一種寫法,但在場景愈加復雜後,EventBus能夠體現出更好的解耦能力。

背景知識

主要涉及三方面的知識:

  1. 觀察者模式(or 發布-訂閱模式)

  2. Android消息機制

  3. Java並發編程

本文可以認為是greenrobot/EventBus這個開源庫的源碼閱讀指南,筆者在看設計模式相關書籍的時候了解到這個庫,覺得有必要實現一下核心功能以加深理解。

實現過程

EventBus的使用分三個步驟:註冊監聽、發送事件和取消監聽,相應本文也將分這三步來實現。

註冊監聽

定義一個註解:


@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface Subscribe {

    ThreadMode threadMode() default ThreadMode.POST;

}

greenrobot/EventBus還支持優先級和粘性事件,這裏只支持最基本的能力:區分線程,因為如更新UI的操作必須放在主線程。ThreadMode如下:


public enum ThreadMode {

    MAIN, // 主線程

    POST, // 發送消息的線程

    ASYNC // 新開一個線程發送

}

在對象初始化的時候,使用register方法註冊,該方法會解析被註冊對象的所有方法,並解析聲明了註解的方法(即觀察者),核心代碼如下:


public class EventBus {

    ...

    public void register(Object subscriber) {

        if (subscriber == null) {

            return;

        }

        synchronized (this) {

            subscribe(subscriber);

        }

    }

    ...

    private void subscribe(Object subscriber) {

        if (subscriber == null) {

            return;

        }

        // TODO 巨踏馬難看的縮進

        Class<?> clazz = subscriber.getClass();

        while (clazz != null && !isSystemClass(clazz.getName())) {

            final Method[] methods = clazz.getDeclaredMethods();

            for (Method method : methods) {

                Subscribe annotation = method.getAnnotation(Subscribe.class);

                if (annotation != null) {

                    Class<?>[] paramClassArray = method.getParameterTypes();

                    if (paramClassArray != null && paramClassArray.length == 1) {

                        Class<?> paramType = convertType(paramClassArray[0]);

                        EventType eventType = new EventType(paramType);

                        SubscriberMethod subscriberMethod = new SubscriberMethod(method, annotation.threadMode(), paramType);

                        realSubscribe(subscriber, subscriberMethod, eventType);

                    }

                }

            }

            clazz = clazz.getSuperclass();

        }

    }

    ...

    private void realSubscribe(Object subscriber, SubscriberMethod method, EventType eventType) {

        CopyOnWriteArrayList<Subscription> subscriptions = mSubscriptionsByEventtype.get(subscriber);

        if (subscriptions == null) {

            subscriptions = new CopyOnWriteArrayList<>();

        }

        Subscription subscription = new Subscription(subscriber, method);

        if (subscriptions.contains(subscription)) {

            return;

        }

        subscriptions.add(subscription);

        mSubscriptionsByEventtype.put(eventType, subscriptions);

    }

    ...

}

執行過這些邏輯後,該對象所有的觀察者方法都會被存在一個Map中,其Key是EventType,即觀察事件的類型,Value是訂閱了該類型事件的所有方法(即觀察者)的一個列表,每個方法和對象一起封裝成了一個Subscription類:


public class Subscription {

    public final Reference<Object> subscriber;

    public final SubscriberMethod subscriberMethod;

    public Subscription(Object subscriber, 

                        SubscriberMethod subscriberMethod) {

        this.subscriber = new WeakReference<>(subscriber);// EventBus3 沒用弱引用?

        this.subscriberMethod = subscriberMethod;

    }

    @Override

    public int hashCode() {

        return subscriber.hashCode() + subscriberMethod.methodString.hashCode();

    }

    @Override

    public boolean equals(Object obj) {

        if (obj instanceof Subscription) {

            Subscription other = (Subscription) obj;

            return subscriber == other.subscribe

                    && subscriberMethod.equals(other.subscriberMethod);

        } else {

            return false;

        }

    }

}

如此,便是註冊監聽方法的核心邏輯了。

消息發送

消息的發送代碼很簡單:


public class EventBus {

    ...

    private EventDispatcher mEventDispatcher = new EventDispatcher();

    private ThreadLocal<Queue<EventType>> mThreadLocalEvents = new ThreadLocal<Queue<EventType>>() {

        @Override

        protected Queue<EventType> initialValue() {

            return new ConcurrentLinkedQueue<>();

        }

    };

    ...

    public void post(Object message) {

        if (message == null) {

            return;

        }

        mThreadLocalEvents.get().offer(new EventType(message.getClass()));

        mEventDispatcher.dispatchEvents(message);

    }

    ...

}

比較復雜一點的是需要根據註解聲明的線程模式在對應的線程進行發布:


public class EventBus {

    ...

    private class EventDispatcher {

        private IEventHandler mMainEventHandler = new MainEventHandler();

        private IEventHandler mPostEventHandler = new DefaultEventHandler();

        private IEventHandler mAsyncEventHandler = new AsyncEventHandler();

        void dispatchEvents(Object message) {

            Queue<EventType> eventQueue = mThreadLocalEvents.get();

            while (eventQueue.size() > 0) {

                handleEvent(eventQueue.poll(), message);

            }

        }

        private void handleEvent(EventType eventType, Object message) {

            List<Subscription> subscriptions = mSubscriptionsByEventtype.get(eventType);

            if (subscriptions == null) {

                return;

            }

            for (Subscription subscription : subscriptions) {

                IEventHandler eventHandler =  getEventHandler(subscription.subscriberMethod.threadMode);

                eventHandler.handleEvent(subscription, message);

            }

        }

        private IEventHandler getEventHandler(ThreadMode mode) {

            if (mode == ThreadMode.ASYNC) {

                return mAsyncEventHandler;

            }

            if (mode == ThreadMode.POST) {

                return mPostEventHandler;

            }

            return mMainEventHandler;

        }

    }// end of the class

    ...

}

三種線程模式分別如下,DefaultEventHandler(在發布線程執行觀察者放方法):


public class DefaultEventHandler implements IEventHandler {

    @Override

    public void handleEvent(Subscription subscription, Object message) {

        if (subscription == null || subscription.subscriber.get() == null) {

            return;

        }

        try {

            subscription.subscriberMethod.method.invoke(subscription.subscriber.get(), message);

        } catch (IllegalAccessException | InvocationTargetException e) {

            e.printStackTrace();

        }

    }

}

MainEventHandler(在主線程執行):


public class MainEventHandler implements IEventHandler {

    private Handler mMainHandler = new Handler(Looper.getMainLooper());

    DefaultEventHandler hanlder = new DefaultEventHandler();

    @Override

    public void handleEvent(final Subscription subscription, final Object message) {

        mMainHandler.post(new Runnable() {

            @Override

            public void run() {

                hanlder.handleEvent(subscription, message);

            }

        });

    }

}

AsyncEventHandler(新開一個線程執行):


public class AsyncEventHandler implements IEventHandler {

    private DispatcherThread mDispatcherThread;

    private IEventHandler mEventHandler = new DefaultEventHandler();

    public AsyncEventHandler() {

        mDispatcherThread = new DispatcherThread(AsyncEventHandler.class.getSimpleName());

        mDispatcherThread.start();

    }

    @Override

    public void handleEvent(final Subscription subscription, final Object message) {

        mDispatcherThread.post(new Runnable() {

            @Override

            public void run() {

                mEventHandler.handleEvent(subscription, message);

            }

        });

    }

    private class DispatcherThread extends HandlerThread {

        // 關聯了AsyncExecutor消息隊列的Handle

        Handler mAsyncHandler;

        DispatcherThread(String name) {

            super(name);

        }

        public void post(Runnable runnable) {

            if (mAsyncHandler == null) {

                throw new NullPointerException("mAsyncHandler == null, please call start() first.");

            }

            mAsyncHandler.post(runnable);

        }

        @Override

        public synchronized void start() {

            super.start();

            mAsyncHandler = new Handler(this.getLooper());

        }

    }

}

以上便是發布消息的代碼。

註銷監聽

最後一個對象被銷毀還要註銷監聽,否則容易導致內存泄露,目前SimpleEventBus用的是WeakReference,能夠通過GC自動回收,但不知道greenrobot/EventBus為什麽沒這樣實現,待研究。註銷監聽其實就是遍歷Map,拿掉該對象的訂閱即可:


public class EventBus {

    ...

    public void unregister(Object subscriber) {

        if (subscriber == null) {

            return;

        }

        Iterator<CopyOnWriteArrayList<Subscription>> iterator = mSubscriptionsByEventtype.values().iterator();

        while (iterator.hasNext()) {

            CopyOnWriteArrayList<Subscription> subscriptions = iterator.next();

            if (subscriptions != null) {

                List<Subscription> foundSubscriptions = new LinkedList<>();

                for (Subscription subscription : subscriptions) {

                    Object cacheObject = subscription.subscriber.get();

                    if (cacheObject == null || cacheObject.equals(subscriber)) {

                        foundSubscriptions.add(subscription);

                    }

                }

                subscriptions.removeAll(foundSubscriptions);

            }

            // 如果針對某個Event的訂閱者數量為空了,那麽需要從map中清除

            if (subscriptions == null || subscriptions.size() == 0) {

                iterator.remove();

            }

        }

    }

    ...

}

以上便是事件總線最核心部分的代碼實現,完整代碼見vimerzhao/SimpleEventBus,後面發現問題更新或者進行升級也只會改動倉庫的代碼。

局限性

由於時間關系,目前只研究了EventBus的核心部分,還有幾個值得深入研究的點,在此記錄一下,也歡迎路過的大牛指點一二。

性能問題

實際使用時,註解和反射會導致性能問題,但EventBus3已經通過Subscriber Index基本解決了這一問題,實現也非常有意思,是通過註解處理器(Annotation Processor)把耗時的邏輯從運行期提前到了編譯期,通過編譯期生成的索引來給運行期提速,這也是這個名字的由來。

可用性問題

如果訂閱者很多會不會影響體驗,畢竟原始的方法是點對點的消息傳遞,不會有這種問題,如果部分訂閱者尚未初始化怎麽辦。等等。目前EventBus3提供了優先級和粘性事件的屬性來進一步滿足開發需求。但是否徹底解決問題了還有待驗證。

跨進程問題

EventBus是進程內的消息管理機制,並且從開源社區的反饋來看這個項目是非常成功的,但稍微有點體量的APP都做了進程拆分,所以是否有必要支持多進程,能否在保證性能的情況下提供同等的代碼解耦能力,也值得繼續挖掘。目前有lsxiao/Apollo和Xiaofei-it/HermesEventBus等可供參考。

參考

  • greenrobot/EventBus

  • hehonghui/AndroidEventBus

  • 《Android開發藝術探索》第十章

此文已由作者授權騰訊雲+社區發布


自己動手寫事件總線(EventBus)