【Android】事件总线 EventBus

46 阅读13分钟

我的练习项目的结构是 Fragment 内有 RecyclerView 的两层嵌套结构,要给内层 RecyclerView Item 注册点击跳转外层 Fragment,把对应的 Fragment 加入前台返回栈中,我最开始的想法是回调,这样整体结构就是两层回调,由外层的 Fragment 给内层 Item 注册点击事件,代码逻辑是

/* ---Fragment--- */

OuterRecyclerViewAdapter adapter = new OuterRecyclerViewAdapter(Recommend.recommendsPageDataSimulation());
adapter.setOnInnerItemClickListener(item -> {
    FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    PlaylistFragment playListFragment = PlaylistFragment.newInstance("", "");

    transaction.replace(R.id.fragment_container, playListFragment); // replace会销毁启动页实例
transaction.addToBackStack(null);

    transaction.commit();
});

/* --- OuterRecyclerView ---*/

public interface OnInnerItemClickListener {
    void onInnerItemClick(Playlist item);
}

private OnInnerItemClickListener innerItemClickListener;

public void setOnInnerItemClickListener(OnInnerItemClickListener listener) {
    this.innerItemClickListener = listener;
}

((InnerRecyclerViewAdapter)content.getAdapter()).setOnItemClickListener(item -> {
    if (innerItemClickListener != null) {
        innerItemClickListener.onInnerItemClick(item);
    }
});

/* --- InnerRecyclerView --- */

public interface OnItemClickListener {
    void onItemClick(Playlist item);
}

private OnItemClickListener listener;

public void setOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
}

holder.itemView.setOnClickListener(v -> {
    if (listener != null) listener.onItemClick(playlist);
});

/* 原谅我没做逻辑解耦 因为练手项目不涉及数据处理层 */

可以看到我用 OnInnerItemClickListener 和 OnItemClickListener 两种接口来做回调传递,最后在 Fragment 用 replace 这种糟糕的方式做跳转逻辑,使得 OuterRecyclerView 在点击 Item 后返回会重置到最顶部,同时带来不少的额外性能开销。

如何省去这两层回调链?抛开 Jetpack 的 LiveData + ViewModel 模式来看,合适的选择是使用事件总线,其是基于发布 - 订阅的事件订阅机制,能够让无法获得引用或不应获得引用的两组模块间进行解耦通信,而不在意各自的持有结构。提到事件总线可以想到 EventBus、otto 和基于响应流变成 RxJava 的 RxBus,在这里我们择重说明 EventBus 的使用和底层实现机制。

EventBus 的使用方式

EventBus 有 3 种核心要素,分别是任意类型的事件类对象 Event、在任意线程位置发送事件的时间发布者 Publisher 与接受和处理事件的事件订阅者 Subscriber,其中 Subscriber 在 EventBus3.0 前只能使用 onEvent、onEventMainThread、onEventBackgroundThread 和 onEventAsync 作为方法名,在后续版本可以使用 @Subscribe (threadMode = ThreadMode.XXX) 注解来自定义方法名,而前面提到的 4 种方法名也就对应 EventBus 的 4 种线程模式 ThreadMode:

  1. POSITING:若仅使用 @Subscribe 作为注解则默认为该模式,POSITING 模式的事件的发布和事件处理方法的执行在相同线程运行,使用该模式可能会造成 ANR,要区分不同的业务场景;
  2. MAIN:事件处理在 UI 线程执行,该模式同样容易造成 ANR;
  3. BACKGROUND:若事件在 UI 线程发布则事件处理方法会在新线程中执行;若在非 UI 线程发布则逻辑和 POSITING 相同。
  4. ASYNC:任何线程发出的事件都会在新线程中执行,Android 禁止非 UI 线程进行 UI 更新操作,EventBus 同样遵守该原则,所以使用 BACKGROUND 和 ASYNC 模式的 EventBus 不要更新 UI。

EventBus 的 API 很精简,在订阅事件的位置注册事件后即可进行发送和处理事件操作,具体的代码是

/* ---Subscriber--- */

EventBus.getDefault().register(this); 
/* EventBus.getDefault()用来获得默认的EventBus实例,也可以通过构造方法自定义EventBus */

@Subscribe (threadMode = ThreadMode.XXX)
public void XXXX(Message message) {
    ...
} 
/* 其中Message可以是任意类 */

EventBus.getDefault().unregister(this);
/* 最后记得取消注册 */

/* ---Publisher--- */

EventBus,getDefault().post(messageEvent);

所以开篇提到的我项目遇到的问题,改用 EventBus 逻辑就非常简单啦,值得注意的是 EventBus 的 post 方法没有空参重载,所以要传递 1 种实体类:

/* ---Fragment--- */

EventBus.getDefault().register(this);  
/* 处理UI操作的事件注册建议设置在onStart方法 */

@Subscribe (threadMode = ThreadMode.MAIN)
public void transaction(String param) {
    FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    PlaylistFragment playListFragment = PlaylistFragment.newInstance(param, "");

    transaction.replace(R.id.fragment_container, playListFragment); // replace会销毁启动页实例
transaction.addToBackStack(null);

    transaction.commit();
}

/* (onStop) */
EventBus.getDefault().unRegister(this);

/* --- InnerRecyclerView --- */

holder.itemView.setOnClickListener(v -> {
    String param = "ive been triggered";
    EventBus,getDefault().post(param);
});

EventBus 支持黏性事件,通过在发送方 Punisher 调用 postSticky 方法表示黏性事件,即发送事件后再 register 注册该事件的实例也能收到该事件,同时事件处理方也要在注解加入 sticky == true 来接受 EventBus 的校验检查。

EventBus 的源码实现

构造方法

我们在使用方式中可以反复看到 EventBus.getDefault 方法,该方法的实现是

static volatile EventBus defaultInstance;

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

private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();

public EventBus() {
    this(DEFAULT_BUILDER);
}

其中 defaultInstance 成员字段禁止指令重排序,因为 Java 创建对象分 3 步:分配内存、初始化对象和实例名指向内存地址,若进行重排序则有可能造成某线程看到实例名非空,也就是完成实例名指向内存地址操作,但初始化对象操作未完成,此时读到的是半成品实例。

若 instance 非空则尝试取锁,取锁后再次检查 instance 是否非空,空则调用空参构造方法创建实例,是标准的双重检查锁 DCL 实现,我们写单例也适用该模式。

空参构造指向 EventBus 预设的 EventBusBuilder 空参构造实例,调用该实例参与的有参构造,这样将空参的 EventBus 配置归到有参构造统一实现,也方便后续拓展且解耦 EventBus 和 Builder,很优美的代码逻辑。EventBusBuilder 表示我们经常能见到的建造者模式,相关代码是

public static EventBusBuilder builder() {
    return new EventBusBuilder();
}

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

注册方法

register 首先会获得订阅者的类信息,我们知道形如 String 类元信息的存储形式是 Class,所以在这里使用泛型通配符表示接收所有可能的类信息。接着调用 SubscriberMethodFinder 实例的 findSubscriberMethods 方法查找获得订阅者的所有注册方法,最后会同步遍历调用 subscribe 将所有注册方法添加到 EventBus 内部表中。

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

SubscriberMethod 表示订阅者的注册方法,该类的 eventType 字段携带注册方法的参数,也就是注册方法所接收的事件类型,Subscription 表示订阅者和某注册方法的封装对象,我们前面提到的 EventBus 内部表指 subscriptionsByEventType 成员,该字段表示事件类型到 Subscription 集合的哈希映射,typesBySubscriber 表是订阅者到事件类型集合的反向表。subscribe 方法首先获得该注册方法的事件类型,再尝试在内部表查找该事件类型对应的 Subscription 集合是否存在该订阅者对象,是则抛出重复注册异常,接着对 Subscription 集合的所有注册方法按优先级 priority 进行排序。

优先级是 EventBus3.x 的新概念,使用方式和粘性事件类似,在注解加入 priority = x,x 的取值限制就是 int 的取值范围,不设置优先级的默认 x 值为 0,值得注意的是相同优先级且相同事件类型的注册方法会被视作重复注册抛出异常,所以类似方法重载,不同优先级但相同事件类型的注册方法会被 EventBus 认作不同的注册方法。

回到 subscribe 方法,优先级排序可以视作插入排序,在注册方法加入集合后,EventBus 会将事件类型记录到 typesBySubscriber 表中,最后会进行黏性事件操作:如果 subscriberMethod 是黏性事件,则该事件会立即派发给 subscriber。

private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
private final Map<Object, List<Class<?>>> typesBySubscriber;

// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
    Class<?> eventType = subscriberMethod.eventType;
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
    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);
        }
    } /* 为让子类事件也可以派发到父类类型订阅者的继承处理 */
}

SubscriberMethodFinder 逻辑

SubscriberMethodFinder 是 EventBus 用来获得订阅者所有注册方法的反射工具类,主调用链是 findSubscriberMethods - findUsingInfo - findUsingReflectionInSingleClass,因为类可能有多个实例要注册事件总线,所以 EventBus 设有 METHOD_CACHE 字段表示曾经可能注册该类时的方法缓存。findSubscriberMethods 方法首先要查是否有这些方法缓存,没有则按照 EventBus 实例创建时的 ignoreGeneratedIndex 字段进行反射扫描或 APT 自动生成代码查找,在实际业务场景还是推荐用 APT 的,但在这里我们关注相对常规的反射逻辑。

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
    List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
    if (subscriberMethods != null) {
        return subscriberMethods;
    }

    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 中间方法,其实调用 getSubscriberInfo 方法仍然是在尝试使用 APT 预生成的索引,如果存在则避免反射直接拿到方法集合,否则会调用 findUsingReflectionInSingleClass。

private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
    FindState findState = prepareFindState();
    findState.initForSubscriber(subscriberClass);
    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 方法,该方法就是在执行反射查询逻辑,检查各方法的访问权限、参数数量、是否携带 @Subscribe 注解与非静态和抽象方法,将符合条件的方法封装成 SubscriberMethod 加入列表。

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
        try {
            methods = findState.clazz.getMethods();
        } catch (LinkageError error) { // super class of NoClassDefFoundError to be a bit more broad...
            String msg = "Could not inspect methods of " + findState.clazz.getName();
            if (ignoreGeneratedIndex) {
                msg += ". Please consider using EventBus annotation processor to avoid reflection.";
            } else {
                msg += ". Please make this class visible to EventBus annotation processor to avoid reflection.";
            }
            throw new EventBusException(msg, error);
        }
        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) {
                    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");
        }
    }
}

事件发送

首先来看静态内部类 PostingThreadState。该类用于记录 EventBus Publisher 在发送事件时的执行现场状态。其内部维护了一个事件处理队列;而 currentPostingThreadState 字段是基于 ThreadLocal 实现的,因此不同线程通过 currentPostingThreadState 获取到的 PostingThreadState 实例彼此独立,对应的事件队列也互不干扰。正因如此,EventBus 才能依据 ThreadMode 实现事件在不同线程间的派发。

EventBus 允许在订阅方法内部再次调用 post。此时,由于首次发送事件时已经将 isPosting 字段设为 true,只有在完成该事件关联的所有订阅方法处理后才会将其重置为 false。因此,嵌套调用 post 时不会重新启动整个发送流程,而是仅将新事件加入队列,等待统一派发。

回到 post 方法本身,当 isPosting 为 false 时,流程会尝试清空事件队列,将其中的所有事件逐一派发给订阅者。其核心逻辑是循环取出队列中的事件,并调用 postSingleEvent 完成派送。

至于在订阅方法内部调用 post 时,isPosting 是否可能为 false,答案是否定的。isPosting 设为 true 的时机是首次进入事件派发流程;只有当当前事件的所有订阅方法执行完毕后才会被重置为 false。因此,在订阅方法内部的重复调用仍属于同一派发过程的一部分,不会出现 isPosting 为 false 的情况。

final static class PostingThreadState {
    final List<Object> eventQueue = new ArrayList<>();
    boolean isPosting;
    boolean isMainThread;
    Subscription subscription;
    Object event;
    boolean canceled;
}

private final ThreadLocal<PostingThreadState> currentPostingThreadState = new ThreadLocal<PostingThreadState>() {
    @Override
    protected PostingThreadState initialValue() {
        return new PostingThreadState();
    }
};

public void post(Object event) {
    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 方法中出现的 eventInheritance 字段用于控制事件分发方式:该字段决定当前事件在发送时,是否应同时触发其父类类型所对应的订阅方法。当 eventInheritance 为 true 时,系统将调用 lookupAllEventTypes 方法,从事件本身一路向上查找到 Object 的完整继承链,并依次调用 postSingleEventForEventType 方法,尝试向各层级类型的订阅者进行投递;当 eventInheritance 为 false 时,事件仅按照其实际类型进行派发。subscriptionFound 字段用于记录当前事件(包含继承链的父类事件在内)是否至少存在一个对应的订阅方法。最后的逻辑中,当未找到任何订阅者时,会依据配置条件触发相关分支,其中 NoSubscriberEvent 是一个用于标记本次事件无人处理的空事件类型。若开发者为 NoSubscriberEvent 注册了订阅方法,则可在其中显式获知本次事件投递无人响应。

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
    Class<?> eventClass = event.getClass();
    boolean subscriptionFound = false;
    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));
        }
    }
}

所以 postSingleEvent 方法仍然不是投递流程的核心,我们来到 postSingleEventForEventType 方法,该方法首先会同步取出事件对应的订阅者集合 subscriptions ,将事件和各订阅对象传递给 postingState,最后执行 postToSubscription 方法。

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
    CopyOnWriteArrayList<Subscription> subscriptions;
    synchronized (this) {
        subscriptions = subscriptionsByEventType.get(eventClass);
    }
    if (subscriptions != null && !subscriptions.isEmpty()) {
        for (Subscription subscription : subscriptions) {
            postingState.event = event;
            postingState.subscription = subscription;
            boolean aborted;
            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 方法,在这层就能明确看到 ThreadMode 对事件派发行为的影响。方法内部根据订阅者声明的线程模式执行不同的逻辑。invokeSubscriber 会直接通过反射调用订阅方法;如果当前提交事件的线程并非主线程,而订阅方法的执行要求发生在主线程,则会通过 mainThreadPoster 的 enqueue 方法将事件封装并加入主线程的队列中。mainThreadPoster 属于 HandlerPoster 类型,它继承自 Handler,事件入队后将交由 Android 的消息队列体系调度执行。

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

取消注册

注册方法有提到,typesBySubscriber 字段表示订阅者到订阅类型集合的反向表,取消注册会首先找到该订阅者对应的事件集合,遍历集合调用 unsubscribeByEventType 方法来查事件对应的订阅者集合,将该订阅者移除,最后在 typesBySubscriber 移除该订阅者。

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

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