在 深入理解EventBus - 基本使用 已经初步了解了EventBus如何使用,下面从Thread Mode、Sticky Event、EventBus单例模式创建等方面,深入了解EventBus的使用,以满足各种场景的使用。
Configuration
在 深入理解EventBus - 基本使用中,我们获取EventBus单例对象是通过EventBus.getDefault()方法。此时,EventBus对象的设置都是默认的。当然,大部分情况都是可以满足。都说小概率事件年年有。假如分发的事件,没有订阅者时,之前说过,会报异常。若不想报异常,让EventBus保持静默,不做扫描,该如何处理呢?那就不得不说EventBuilder,这个通过Builder模式创建EventBus对象了。下面先看EventBuilder常用的方法
EventBus build():基于当前EventBus配置创建EventBus
EventBus installDefaultEventBus():创建默认的EventBus对象,相当于EventBus.getDefault()。
EventBuilder addIndex(SubscriberInfoIndex index): 添加由EventBus“注释预处理器生成的索引
EventBuilder eventInheritance(boolean eventInheritance):默认情况下,EventBus认为事件类有层次结构(订户超类将被通知)
EventBuilder executorService(java.util.concurrent.ExecutorService executorService):定义一个线程池用于处理后台线程和异步线程分发事件
EventBuilder ignoreGeneratedIndex(boolean ignoreGeneratedIndex):强制使用事件反射,即使事件已被设置索引
EventBuilder logNoSubscriberMessages(boolean logNoSubscriberMessages):打印没有订阅消息,默认为true
EventBuilder logSubscriberExceptions(boolean logSubscriberExceptions):打印订阅异常,默认true
EventBuilder sendNoSubscriberEvent(boolean sendNoSubscriberEvent):设置发送的的事件在没有订阅者的情况时,EventBus是否保持静默,默认true
EventBuilder sendSubscriberExceptionEvent(boolean sendSubscriberExceptionEvent):发送分发事件的异常,默认true
EventBuilder skipMethodVerificationFor(java.lang.Class<?> clazz) 在3.0以前,接收处理事件的方法名以onEvent开头,方法名称验证避免不是以此开头。
使用此方法用以排除用户类
EventBuilder strictMethodVerification(boolean strictMethodVerification) 启用严格的方法验证(默认:false)
EventBuilder throwSubscriberException(boolean throwSubscriberException) 如果onEvent***方法出现异常,是否将此异常分发给订阅者(