-
Notifications
You must be signed in to change notification settings - Fork 1
EventBus
Arcturus EventBus is a standardized eventbus implementation, used for common distributed systems.
Arcturus provide different clients used to either produce or consume events.
When producing events, use the event publisher from the IEventBusFactory, example:
var publisher = eventBusFactory.CreatePublisher("task_queue");
var eventMessage = new MessageEvent { Message = "Hello World" };
await publisher.Publish(eventMessage, cancellationToken);You can consume events using either a IProcessor or a ISubscriber.
var processor = eventBusFactory.CreateProcessor();
processor.OnProcessAsync += async (eventMessage, cancellationToken) =>
{
// handle eventMessage
await Task.CompletedTask; // Simulate async processing
};
Task.Run(() => processor.WaitForEvents());Events are strongly typed classes implementing the IEventMessage interface. Example:
public sealed class MessageEvent : IEventMessage
{
public required string Message { get; set; }
}When using the event processor (implementations of IProcessor), events can be auto-wired using the IEventMessageHandler interface. Example:
public sealed class MessageEventEventHandler(
ILogger<MessageEventEventHandler> logger)
: IEventMessageHandler<MessageEvent>
{
private readonly ILogger<MessageEventEventHandler> _logger = logger;
public Task Handle(MessageEvent@event, CancellationToken cancellationToken = default)
{
// ... code
return Task.CompletedTask;
}
}Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component:
- Chooses whether to pass the request to the next component in the pipeline.
- Can perform work before and after the next component in the pipeline.
Request delegates are used to build the request pipeline. The request delegates handle each eventbus request.
Arcturus support implementation of middleware as a pipeline when implementing the IEventMessageHandler approach.
Using
IProcessor.OnProcessAsyncdoes not call the middleware framework.
Middleware follow this syntax:
public class SampleMiddlware
{
public async Task InvoiceAsync(EventContext context)
{
}
}You can inject services injected as singleton in the constructor of the middleware, whilst scoped and transient services must be injected within the InvokeAsync method.
Implements Arcturus.EventBus using RabbitMQ as queue and intermediate.
dotnet add package Arcturus.EventBus.RabbitMQusing Arcturus.EventBus.RabbitMQ;
...
// console sample
var builder = Host.CreateApplicationBuilder();
builder.Services.AddRabbitMQEventBus(o => {
o.ApplicationId = "sampleApplication";
o.ClientName = Guid.NewGuid().ToString()[..10];
o.HostName = "localhost";
});
var host = builder.Build();
...Implements Arcturus.EventBus using SQLite as queue and intermediate.
dotnet add package Arcturus.EventBus.SQLiteusing Arcturus.EventBus.SQLite;
...
// console sample
var builder = Host.CreateApplicationBuilder();
builder.Services.AddSqliteEventBus(o => {
o.ConnectionString = "Data Source=:memory:;";
});
var host = builder.Build();
...Implements Arcturus.EventBus using Azure ServiceBus as queue and intermediate.
dotnet add package Arcturus.EventBus.AzureServiceBusImplements Arcturus.EventBus using Azure Storage as queue and intermediate.
dotnet add package Arcturus.EventBus.AzureStorageQueueusing Arcturus.EventBus.AzureStorageQueue;
...
// console sample
var builder = Host.CreateApplicationBuilder();
builder.Services.AddAzureStorageQueueEventBus(o => {
o.ApplicationId = "sampleApplication";
o.ConnectionString = @"DefaultEndpointsProtocol=https;...EndpointSuffix=core.windows.net";
o.MessageProcessing.MessageIntervalMilliseconds = 1500;
});
var host = builder.Build();
...