Highlights of this issue

This issue brings many features that developers have been waiting for.

Highlight 1: Support wildcard, regular expression event message


// 支持正则表达式匹配
[EventSubscribe("(^1[3456789][0-9]{9}$)|((^[0-9]{3,4}\\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\\([0-9]{3,4}\\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$))")]
public async Task RegexHandler(EventHandlerExecutingContext context)
{
    var eventId = context.Source.EventId;
    await Task.CompletedTask;
}

test:


await _eventPublisher.PublishAsync("13800138000");
await _eventPublisher.PublishAsync("13434563233");

Highlight 2: Support local retry strategy and global custom strategy


// 支持多种异常重试配置
[EventSubscribe("test:error", NumRetries = 3)]
[EventSubscribe("test:error", NumRetries = 3, RetryTimeout = 1000)] // 重试间隔时间
[EventSubscribe("test:error", NumRetries = 3, ExceptionTypes = new[] { typeof(ArgumentException) })]    // 特定类型异常才重试
public async Task ExceptionHandler(EventHandlerExecutingContext context)
{
    var eventId = context.Source.EventId;
    await Task.CompletedTask;
}

// 全局自定义策略
public class RetryEventHandlerExecutor : IEventHandlerExecutor
{
    public async Task ExecuteAsync(EventHandlerExecutingContext context, Func<EventHandlerExecutingContext, Task> handler)
    {
        // 如果执行失败,每隔 1s 重试,最多三次
        await Retry(async () => {
            await handler(context);
        }, 3, 1000);
    }
}

Highlight 3: Support for subscribing or deleting messages at runtime


public class TestEventBusController : Controller
{
    private readonly IEventPublisher _eventPublisher;
    private readonly IEventBusFactory _eventBusFactory;
    public TestEventBusController(IEventPublisher eventPublisher, IEventBusFactory eventBusFactory)
    {
        _eventPublisher = eventPublisher;
        _eventBusFactory = eventBusFactory;
    }

    // 运行时动态添加一个订阅器
    public async Task AddSubscriber()
    {
        await _eventBusFactory.AddSubscriber("xxx", async (c) =>
        {
            Console.WriteLine("我是动态的");
            await Task.CompletedTask;
        });
    }

    // 运行时动态删除一个订阅器
    public async Task RemoveDynamic(string eventId)
    {
        await _eventBusFactory.RemoveSubscriber(eventId);
    }
}

Highlight 4: Support a variety of rich registration handler methods


// 注册 EventBus 服务
services.AddEventBus(builder =>
{
    // 注册 ToDo 事件订阅者
    builder.AddSubscriber<ToDoEventSubscriber>();

    // 通过类型注册
    builder.AddSubscriber(typeof(ToDoEventSubscriber));

    // 批量注册事件订阅者
    builder.AddSubscribers(ass1, ass2, ....);
});

license nuget dotNET China

.NET event bus that simplifies communication between projects, class libraries, threads, services, etc. with less code and better quality. ‎

Jaina.drawio

Source code analysis

characteristic

  • Simplify communication between components
    • Support event monitor
    • Support for action executors
    • Support for custom message store components
    • Support for custom policy enforcement
    • Support single consumption and multi-consumption messages
    • Support message idempotency processing
  • High cohesion, low coupling, making the code simpler
  • very fast, processing per second 30000 + information
  • small, only 10KB
  • No third-party dependencies
  • Available at Windows/Linux/MacOS Daemon deployment
  • Support distributed, cluster
  • High quality code and good unit tests

Install

Quick start

We have quite a few examples on the home page, this is the first one to get you started:

  1. Define event subscribers ToDoEventSubscriber:

// 实现 IEventSubscriber 接口
public class ToDoEventSubscriber : IEventSubscriber
{
    private readonly ILogger<ToDoEventSubscriber> _logger;
    public ToDoEventSubscriber(ILogger<ToDoEventSubscriber> logger)
    {
        _logger = logger;
    }

    [EventSubscribe("ToDo:Create")] // 支持多个
    [EventSubscribe(YourEnum.Message)]   // 支持枚举
    public async Task CreateToDo(EventHandlerExecutingContext context)
    {
        var todo = context.Source;
        _logger.LogInformation("创建一个 ToDo:{Name}", todo.Payload);
        await Task.CompletedTask;
    }

    // 支持枚举类型
    [EventSubscribe(YourEnum.Some)]
    public async Task EnumHandler(EventHandlerExecutingContext context)
    {
        var eventEnum = context.Source.EventId.ParseToEnum(); // 将事件 Id 转换成枚举对象
        await Task.CompletedTask;
    }

    // 支持正则表达式匹配
    [EventSubscribe("(^1[3456789][0-9]{9}$)|((^[0-9]{3,4}\\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\\([0-9]{3,4}\\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$))")]
    public async Task RegexHandler(EventHandlerExecutingContext context)
    {
        var eventId = context.Source.EventId;
        await Task.CompletedTask;
    }

    // 支持多种异常重试配置
    [EventSubscribe("test:error", NumRetries = 3)]
    [EventSubscribe("test:error", NumRetries = 3, RetryTimeout = 1000)] // 重试间隔时间
    [EventSubscribe("test:error", NumRetries = 3, ExceptionTypes = new[] { typeof(ArgumentException) })]    // 特定类型异常才重试
    public async Task ExceptionHandler(EventHandlerExecutingContext context)
    {
        var eventId = context.Source.EventId;
        await Task.CompletedTask;
    }
}

  1. Create a controller ToDoControllerdependency injection IEventPublisher Serve:

public class ToDoController : ControllerBase
{
    // 依赖注入事件发布者 IEventPublisher
    private readonly IEventPublisher _eventPublisher;
    public ToDoController(IEventPublisher eventPublisher)
    {
        _eventPublisher = eventPublisher;
    }

    // 发布 ToDo:Create 消息
    public async Task CreateDoTo(string name)
    {
        await _eventPublisher.PublishAsync(new ChannelEventSource("ToDo:Create", name));
        // 简化版本
        await _eventPublisher.PublishAsync("ToDo:Create", name);
    }
}

  1. exist Startup.cs register EventBus Serve:

// 注册 EventBus 服务
services.AddEventBus(builder =>
{
    // 注册 ToDo 事件订阅者
    builder.AddSubscriber<ToDoEventSubscriber>();

    // 通过类型注册
    builder.AddSubscriber(typeof(ToDoEventSubscriber));

    // 批量注册事件订阅者
    builder.AddSubscribers(ass1, ass2, ....);
});

  1. Run the project:

info: Jaina.Samples.ToDoEventSubscriber[0]
      创建一个 ToDo:Jaina

more documentation

Documentation

You can find Jaina documentation on the home page.

contribute

The main purpose of this repository is to continue developing the Jaina core, making it faster and easier to use. Jaina is developed in Gitee Publicly, we thank the community for contributing bug fixes and improvements.

license

Jaina adopts MulanPSL-2.0 Open Source License.


Copyright (c) 2020-2021 百小僧, Baiqian Co.,Ltd.
Jaina is licensed under Mulan PSL v2.
You can use this software according to the terms andconditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
            https://gitee.com/dotnetchina/Jaina/blob/master/LICENSE
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUTWARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.

#NET #Distributed #Event #Bus #Specification #Jaina #v310 #Released #News Fast Delivery

.NET Distributed Event Bus Specification Jaina v3.1.0 Released – News Fast Delivery

Leave a Comment

Your email address will not be published. Required fields are marked *