Database-based message queues, currently supported databases.:
- MongoDB
- SqlServer
- Sqlite
Features:
- Supports retries
- Supports multiple queues
- Supports concurrent consumption
- Support opentelemetry tracing
Initialize:
serviceCollection.AddLightMQ(it =>
{
// it.UseSqlServer("Data Source=.;Initial Catalog=Test;User ID=sa;Password=Abc12345;");
it.UseMongoDB("mongodb://localhost:27017","Test");
});
Add a consumer:
public class Test2Consumer:IMessageConsumer
{
public ConsumerOptions GetOptions()
{
return new ConsumerOptions()
{
ParallelNum = 1,
Topic = "test"
};
}
public async Task<bool> ConsumeAsync(string message, CancellationToken cancellationToken)
{
Console.WriteLine("consume message:"+message);
await Task.Delay(2_000,cancellationToken);
return true;
}
}
Register the consumer:
builder.Services.AddScoped<TestConsumer>();
public class ConsumerOptions
{
/// <summary>
/// Topic
/// </summary>
public string Topic { get; set; }
/// <summary>
/// Enable Random Queue
/// </summary>
public bool EnableRandomQueue {get;set;}
/// <summary>
/// Poll Interval
/// </summary>
public TimeSpan PollInterval { get; set; }=TimeSpan.FromSeconds(2);
/// <summary>
/// Retry Count (not including the first execution)
/// </summary>
public int RetryCount { get; set; } = 0;
/// <summary>
/// Retry Interval
/// </summary>
public TimeSpan RetryInterval { get; set; }=TimeSpan.FromSeconds(5);
/// <summary>
/// Concurrent Number
/// </summary>
public int ParallelNum { get; set; }
}
More examples can be found under the Sample category.