mirror of https://github.com/abpframework/abp.git
16 changed files with 258 additions and 56 deletions
@ -0,0 +1,82 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus.RabbitMq |
||||
|
{ |
||||
|
public class RabbitMqEventErrorHandler : EventErrorHandlerBase, ISingletonDependency |
||||
|
{ |
||||
|
public const string HeadersKey = "headers"; |
||||
|
public const string RetryIndexKey = "retryIndex"; |
||||
|
|
||||
|
protected RabbitMqDistributedEventBus EventBus { get; } |
||||
|
|
||||
|
public RabbitMqEventErrorHandler( |
||||
|
IOptions<AbpEventBusOptions> options, |
||||
|
RabbitMqDistributedEventBus eventBus) |
||||
|
: base(options) |
||||
|
{ |
||||
|
EventBus = eventBus; |
||||
|
} |
||||
|
|
||||
|
protected override async Task Retry(EventExecutionErrorContext context) |
||||
|
{ |
||||
|
if (Options.RetryStrategyOptions.IntervalMillisecond > 0) |
||||
|
{ |
||||
|
await Task.Delay(Options.RetryStrategyOptions.IntervalMillisecond); |
||||
|
} |
||||
|
|
||||
|
var headers = context.GetProperty<Dictionary<string, object>>(HeadersKey) ?? |
||||
|
new Dictionary<string, object>(); |
||||
|
|
||||
|
var index = 1; |
||||
|
if (headers.ContainsKey(RetryIndexKey)) |
||||
|
{ |
||||
|
index = (int) headers[RetryIndexKey]; |
||||
|
headers[RetryIndexKey] = ++index; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
headers[RetryIndexKey] = index; |
||||
|
} |
||||
|
|
||||
|
headers["exceptions"] = context.Exceptions; |
||||
|
|
||||
|
await EventBus.PublishAsync(context.EventType, context.EventData, headers); |
||||
|
} |
||||
|
|
||||
|
protected override Task MoveToDeadLetter(EventExecutionErrorContext context) |
||||
|
{ |
||||
|
if (context.Exceptions.Count == 1) |
||||
|
{ |
||||
|
context.Exceptions[0].ReThrow(); |
||||
|
} |
||||
|
|
||||
|
throw new AggregateException( |
||||
|
"More than one error has occurred while triggering the event: " + context.EventType, |
||||
|
context.Exceptions); |
||||
|
} |
||||
|
|
||||
|
protected override bool ShouldRetry(EventExecutionErrorContext context) |
||||
|
{ |
||||
|
if (!base.ShouldRetry(context)) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
var headers = context.GetProperty<Dictionary<string, object>>(HeadersKey); |
||||
|
|
||||
|
if (headers == null || !headers.ContainsKey(RetryIndexKey)) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
var index = (int) headers[RetryIndexKey]; |
||||
|
|
||||
|
return Options.RetryStrategyOptions.MaxRetryAttempts < index; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Microsoft.Extensions.Options; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus.Rebus |
||||
|
{ |
||||
|
public class AbpEventBusRebusOptionsSetup : IConfigureOptions<AbpEventBusOptions> |
||||
|
{ |
||||
|
public void Configure(AbpEventBusOptions options) |
||||
|
{ |
||||
|
throw new System.NotImplementedException(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.EventBus.Rebus |
||||
|
{ |
||||
|
public class RebusEventErrorHandler : EventErrorHandlerBase, ISingletonDependency |
||||
|
{ |
||||
|
public RebusEventErrorHandler( |
||||
|
IOptions<AbpEventBusOptions> options) |
||||
|
: base(options) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
protected override Task Retry(EventExecutionErrorContext context) |
||||
|
{ |
||||
|
Throw(context); |
||||
|
|
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
protected override Task MoveToDeadLetter(EventExecutionErrorContext context) |
||||
|
{ |
||||
|
Throw(context); |
||||
|
|
||||
|
return Task.CompletedTask; |
||||
|
} |
||||
|
|
||||
|
private void Throw(EventExecutionErrorContext context) |
||||
|
{ |
||||
|
// Rebus will automatic retries and error handling: https://github.com/rebus-org/Rebus/wiki/Automatic-retries-and-error-handling
|
||||
|
|
||||
|
if (context.Exceptions.Count == 1) |
||||
|
{ |
||||
|
context.Exceptions[0].ReThrow(); |
||||
|
} |
||||
|
|
||||
|
throw new AggregateException( |
||||
|
"More than one error has occurred while triggering the event: " + context.EventType, |
||||
|
context.Exceptions); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue