深入理解EventBus - ThreadMode、Sticky Event等

        在 深入理解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***方法出现异常,是否将此异常分发给订阅者(
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
abp-vnex eventbus是一个用于在ABP框架中进行事件通信的模块。要使用abp-vnex eventbus,您需要按照以下步骤进行安装和配置: 1. 首先,您需要安装abp-vnex eventbus模块。可以通过运行以下命令来安装: ```shell npm install abp-vnex-eventbus --save ``` 2. 安装完成后,您需要在您的应用程序的模块中导入abp-vnex eventbus模块。在您的模块文件中,添加以下代码: ```typescript import { AbpVnexEventBusModule } from 'abp-vnex-eventbus'; @NgModule({ imports: [ AbpVnexEventBusModule ] }) export class YourModule { } ``` 3. 现在,您可以在您的组件或服务中使用abp-vnex eventbus来发送和接收事件。首先,您需要导入`AbpVnexEventBusService`: ```typescript import { AbpVnexEventBusService } from 'abp-vnex-eventbus'; ``` 4. 在您的组件或服务中,您可以使用`AbpVnexEventBusService`的`emit`方法来发送事件。例如,发送一个名为`myEvent`的事件: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } sendEvent() { this.eventBus.emit('myEvent', { data: 'Hello World' }); } ``` 5. 要接收事件,您可以使用`AbpVnexEventBusService`的`on`方法。在您的组件或服务中,添加以下代码: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } ngOnInit() { this.eventBus.on('myEvent').subscribe((eventData) => { console.log(eventData.data); // 输出:Hello World }); } ``` 这样,您就可以使用abp-vnex eventbus模块来进行事件通信了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值