From 0abe394d53f4fb15029e485fc16e1fce73991850 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 28 Jun 2021 11:15:51 +0800 Subject: [PATCH] Added exception handling section to event bus document --- docs/en/Distributed-Event-Bus.md | 53 +++++++++++++++++++++++++- docs/en/Local-Event-Bus.md | 48 ++++++++++++++++++++++- docs/zh-Hans/Distributed-Event-Bus.md | 55 ++++++++++++++++++++++++++- docs/zh-Hans/Local-Event-Bus.md | 48 ++++++++++++++++++++++- 4 files changed, 199 insertions(+), 5 deletions(-) diff --git a/docs/en/Distributed-Event-Bus.md b/docs/en/Distributed-Event-Bus.md index 02216c7ac8..05ef16a59e 100644 --- a/docs/en/Distributed-Event-Bus.md +++ b/docs/en/Distributed-Event-Bus.md @@ -4,7 +4,7 @@ Distributed Event bus system allows to **publish** and **subscribe** to events t ## Providers -Distributed event bus system provides an **abstraction** that can be implemented by any vendor/provider. There are two providers implemented out of the box: +Distributed event bus system provides an **abstraction** that can be implemented by any vendor/provider. There are four providers implemented out of the box: * `LocalDistributedEventBus` is the default implementation that implements the distributed event bus to work as in-process. Yes! The **default implementation works just like the [local event bus](Local-Event-Bus.md)**, if you don't configure a real distributed provider. * `RabbitMqDistributedEventBus` implements the distributed event bus with the [RabbitMQ](https://www.rabbitmq.com/). See the [RabbitMQ integration document](Distributed-Event-Bus-RabbitMQ-Integration.md) to learn how to configure it. @@ -302,3 +302,54 @@ namespace AbpDemo ```` This example uses the `AutoMap` attribute of the AutoMapper to configure the mapping. You could create a profile class instead. Please refer to the AutoMapper document for more options. + +## Exception Handling + +ABP provides exception handling and retries when an exception occurs, it will move to the dead letter queue after the retry fails. + +Enable exception handling: + +```csharp +public override void PreConfigureServices(ServiceConfigurationContext context) +{ + PreConfigure(options => + { + options.EnabledErrorHandle = true; + options.UseRetryStrategy(); + }); +} +``` + +* `EnabledErrorHandle` is used to enable exception handing. +* `UseRetryStrategy` is used to enable retry. + +When an exception occurs, it will retry every three seconds up to the maximum number of retries(default is 3) and move to dead letter queue, you can change the number of retries, retry interval and dead letter queue name: + +```csharp +PreConfigure(options => +{ + options.DeadLetterName = "dead_queue"; + options.UseRetryStrategy(retryStrategyOptions => + { + retryStrategyOptions.IntervalMillisecond = 0; + retryStrategyOptions.MaxRetryAttempts = 1; + }); +}); +``` + +### Error Handle Selector + +By default all event types will be exception handling, you can use `ErrorHandleSelector` of `AbpEventBusOptions` to change it: + +```csharp +PreConfigure(options => +{ + options.ErrorHandleSelector = type => type == typeof(MyExceptionHandleEventData); +}); +``` + +`options.ErrorHandleSelector` actually a list of type predicate. You can write a lambda expression to define your filter. + +### Customize Exception Handling + +ABP defines the `IEventErrorHandler` interface and implemented by the provider, you can replace it via [dependency injection](Dependency-Injection.md) \ No newline at end of file diff --git a/docs/en/Local-Event-Bus.md b/docs/en/Local-Event-Bus.md index d937a1caff..82c839a996 100644 --- a/docs/en/Local-Event-Bus.md +++ b/docs/en/Local-Event-Bus.md @@ -158,7 +158,53 @@ If you perform **database operations** and use the [repositories](Repositories.m > The handler class must be registered to the dependency injection (DI). The sample above uses the `ITransientDependency` to accomplish it. See the [DI document](Dependency-Injection.md) for more options. -## Transaction & Exception Behavior +## Exception Handling + +ABP provides exception handling and retries when an exception occurs. + +Enable exception handling: + +```csharp +public override void PreConfigureServices(ServiceConfigurationContext context) +{ + PreConfigure(options => + { + options.UseRetryStrategy(); + }); +} +``` + +When an exception occurs, it will retry every three seconds up to the maximum number of retries(default is 3) and throw the original exception, you can change the number of retries and the retry interval: + +```csharp +PreConfigure(options => +{ + options.UseRetryStrategy(retryStrategyOptions => + { + retryStrategyOptions.IntervalMillisecond = 0; + retryStrategyOptions.MaxRetryAttempts = 1; + }); +}); +``` + +### Error Handle Selector + +By default all event types will be exception handling, you can use `ErrorHandleSelector` of `AbpEventBusOptions` to change it: + +```csharp +PreConfigure(options => +{ + options.ErrorHandleSelector = type => type == typeof(MyExceptionHandleEventData); +}); +``` + +`options.ErrorHandleSelector` actually a list of type predicate. You can write a lambda expression to define your filter. + +### Customize Exception Handling + +ABP defines the `IEventErrorHandler` interface and implemented by `LocalEventErrorHandler`, you can replace it via [dependency injection](Dependency-Injection.md) + +### Transaction & Exception Behavior When an event published, subscribed event handlers are immediately executed. So; diff --git a/docs/zh-Hans/Distributed-Event-Bus.md b/docs/zh-Hans/Distributed-Event-Bus.md index 4b4203911b..bd086c3d83 100644 --- a/docs/zh-Hans/Distributed-Event-Bus.md +++ b/docs/zh-Hans/Distributed-Event-Bus.md @@ -4,7 +4,7 @@ ## 提供程序 -分布式事件总线系统提供了一个可以被任何提供程序实现的**抽象**. 有两种开箱即用的提供程序: +分布式事件总线系统提供了一个可以被任何提供程序实现的**抽象**. 有四种开箱即用的提供程序: * `LocalDistributedEventBus` 是默认实现,实现作为进程内工作的分布式事件总线. 是的!如果没有配置真正的分布式提供程序,**默认实现的工作方式与[本地事件总线](Local-Event-Bus.md)一样**. * `RabbitMqDistributedEventBus` 通过[RabbitMQ](https://www.rabbitmq.com/)实现分布式事件总线. 请参阅[RabbitMQ集成文档](Distributed-Event-Bus-RabbitMQ-Integration.md)了解如何配置它. @@ -299,4 +299,55 @@ namespace AbpDemo } ```` -此示例使用AutoMapper的 `AutoMap` 属性配置的映射. 你可以创建一个配置文件类代替. 请参阅AutoMapper文档了解更多选项. \ No newline at end of file +此示例使用AutoMapper的 `AutoMap` 属性配置的映射. 你可以创建一个配置文件类代替. 请参阅AutoMapper文档了解更多选项. + +## 异常处理 + +ABP提供了异常处理, 它会进行重试并且重试失败后移动到死信队列. + +启用异常处理: + +```csharp +public override void PreConfigureServices(ServiceConfigurationContext context) +{ + PreConfigure(options => + { + options.EnabledErrorHandle = true; + options.UseRetryStrategy(); + }); +} +``` + +* `EnabledErrorHandle` 用于启用异常处理. +* `UseRetryStrategy` 用于启用重试. + +当一个异常抛出,它会每3秒重试一次直到最大重试次数(默认是3)并且移动到错误队列, 你可以更改重试次数,重试间隔和死信队列名称: + +```csharp +PreConfigure(options => +{ + options.DeadLetterName = "dead_queue"; + options.UseRetryStrategy(retryStrategyOptions => + { + retryStrategyOptions.IntervalMillisecond = 0; + retryStrategyOptions.MaxRetryAttempts = 1; + }); +}); +``` + +### 错误处理选择器 + +默认所有的事件类型都会被处理, 你可以使用 `AbpEventBusOptions` 的 `ErrorHandleSelector` 来更改它: + +```csharp +PreConfigure(options => +{ + options.ErrorHandleSelector = type => type == typeof(MyExceptionHandleEventData); +}); +``` + +`options.ErrorHandleSelector` 实际上是一个类型类型谓词列表. 你可以编写lambda来定义你的过滤. + +### 自定义异常处理 + +ABP定义了 `IEventErrorHandler` 接口并且由提供程序实现, 你可以通过[依赖注入](Dependency-Injection.md)替换它. \ No newline at end of file diff --git a/docs/zh-Hans/Local-Event-Bus.md b/docs/zh-Hans/Local-Event-Bus.md index 3a955e90d7..3a2944d32b 100644 --- a/docs/zh-Hans/Local-Event-Bus.md +++ b/docs/zh-Hans/Local-Event-Bus.md @@ -158,7 +158,53 @@ namespace AbpDemo 如果您执行**数据库操作**并在事件处理程序中使用[仓储](Repositories.md),那么您可能需要创建一个[工作单元](Unit-Of-Work.md),因为一些存储库方法需要在**活动的工作单元**中工作. 确保处理方法设置为 `virtual`,并为该方法添加一个 `[UnitOfWork]` attribute. 或者手动使用 `IUnitOfWorkManager` 创建一个工作单元范围. -## 事务和异常行为 +## 异常处理 + +ABP提供了异常处理, 当异常发现时它会进行重试. + +启用异常处理: + +```csharp +public override void PreConfigureServices(ServiceConfigurationContext context) +{ + PreConfigure(options => + { + options.UseRetryStrategy(); + }); +} +``` + +当一个异常抛出,它会每3秒重试一次直到最大重试次数(默认是3)并且移动抛出原始异常, 你可以更改重试次数和重试间隔和: + +```csharp +PreConfigure(options => +{ + options.UseRetryStrategy(retryStrategyOptions => + { + retryStrategyOptions.IntervalMillisecond = 0; + retryStrategyOptions.MaxRetryAttempts = 1; + }); +}); +``` + +### 错误处理选择器 + +默认所有的事件类型都会被处理, 你可以使用 `AbpEventBusOptions` 的 `ErrorHandleSelector` 来更改它: + +```csharp +PreConfigure(options => +{ + options.ErrorHandleSelector = type => type == typeof(MyExceptionHandleEventData); +}); +``` + +`options.ErrorHandleSelector` 实际上是一个类型类型谓词列表. 你可以编写lambda来定义你的过滤. + +### 自定义异常处理 + +ABP定义了 `IEventErrorHandler` 接口并且由 `LocalEventErrorHandler` 实现, 你可以通过[依赖注入](Dependency-Injection.md)替换它. + +### 事务和异常行为 当一个事件发布,订阅的事件处理程序将立即执行.所以;