-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Added possibility to process messages for each consumer group independently #1034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9362f12
Clarifying the behavior of Subscription Group
54b556b
Improved logging informations
dario-l 6527476
Added DispatcherPerGroup implementation
dario-l 71df712
Added Sample.RabbitMQ.SqlServer.DispatcherPerGroup
dario-l cf4705b
Renaming receivers in sample
dario-l cd0b171
Updated documentation
dario-l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
samples/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/Controllers/HomeController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using DotNetCore.CAP; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Data.SqlClient; | ||
| using Sample.RabbitMQ.SqlServer.DispatcherPerGroup.Messages; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup.Controllers | ||
| { | ||
| public class HomeController : Controller | ||
| { | ||
| private readonly ICapPublisher _capPublisher; | ||
|
|
||
| public HomeController(ICapPublisher capPublisher) | ||
| { | ||
| _capPublisher = capPublisher; | ||
| } | ||
|
|
||
| public async Task<IActionResult> Index() | ||
| { | ||
| await using (var connection = new SqlConnection("Server=(local);Database=CAP-Test;Trusted_Connection=True;")) | ||
| { | ||
| using var transaction = connection.BeginTransaction(_capPublisher); | ||
| // This is where you would do other work that is going to persist data to your database | ||
|
|
||
| var message = TestMessage.Create($"This is message text created at {DateTime.Now:O}."); | ||
|
|
||
| await _capPublisher.PublishAsync(typeof(TestMessage).FullName, message); | ||
| transaction.Commit(); | ||
| } | ||
|
|
||
| return Content("ok"); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
samples/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/Messages/TestMessage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup.Messages | ||
| { | ||
| public class TestMessage | ||
| { | ||
| public static TestMessage Create(string text) => new() | ||
| { | ||
| Text = text | ||
| }; | ||
|
|
||
| public string Text { get; private init; } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
samples/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/Messages/VeryFastProcessingReceiver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Logging; | ||
| using Sample.RabbitMQ.SqlServer.DispatcherPerGroup.TypedConsumers; | ||
|
|
||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup.Messages | ||
| { | ||
| [QueueHandlerTopic("fasttopic")] | ||
| public class VeryFastProcessingReceiver : QueueHandler | ||
| { | ||
| private readonly ILogger<VeryFastProcessingReceiver> _logger; | ||
|
|
||
| public VeryFastProcessingReceiver(ILogger<VeryFastProcessingReceiver> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public async Task Handle(TestMessage value) | ||
| { | ||
| _logger.LogInformation($"Starting FAST processing handler {DateTime.Now:O}: {value.Text}"); | ||
| await Task.Delay(50); | ||
| _logger.LogInformation($"Ending FAST processing handler {DateTime.Now:O}: {value.Text}"); | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
samples/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/Messages/XSlowProcessingReceiver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Logging; | ||
| using Sample.RabbitMQ.SqlServer.DispatcherPerGroup.TypedConsumers; | ||
|
|
||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup.Messages | ||
| { | ||
| [QueueHandlerTopic("slowtopic")] | ||
| public class XSlowProcessingReceiver : QueueHandler | ||
| { | ||
| private readonly ILogger<XSlowProcessingReceiver> _logger; | ||
|
|
||
| public XSlowProcessingReceiver(ILogger<XSlowProcessingReceiver> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public async Task Handle(TestMessage value) | ||
| { | ||
| _logger.LogInformation($"Starting SLOW processing handler {DateTime.Now:O}: {value.Text}"); | ||
| await Task.Delay(10000); | ||
| _logger.LogInformation($"Ending SLOW processing handler {DateTime.Now:O}: {value.Text}"); | ||
| } | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
samples/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Serilog; | ||
| using Serilog.Events; | ||
| using System; | ||
|
|
||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup | ||
| { | ||
| public class Program | ||
| { | ||
| public static int Main(string[] args) | ||
| { | ||
| Log.Logger = new LoggerConfiguration() | ||
| .MinimumLevel.Debug() | ||
| .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) | ||
| .Enrich.FromLogContext() | ||
| .WriteTo.Debug() | ||
| #if DEBUG | ||
| .WriteTo.Seq("http://localhost:5341") | ||
| #endif | ||
| .CreateLogger(); | ||
|
|
||
| try | ||
| { | ||
| Log.Information("Starting host..."); | ||
| CreateHostBuilder(args).Build().Run(); | ||
| return 0; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.Fatal(ex.InnerException ?? ex, "Host terminated unexpectedly"); | ||
| return 1; | ||
| } | ||
| finally | ||
| { | ||
| Log.CloseAndFlush(); | ||
| } | ||
| } | ||
|
|
||
| public static IHostBuilder CreateHostBuilder(string[] args) => | ||
| Host.CreateDefaultBuilder(args) | ||
| .ConfigureAppConfiguration((context, builder) => | ||
| { | ||
| builder | ||
| .AddJsonFile("appsettings.json") | ||
| .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true); | ||
| }) | ||
| .UseSerilog((context, configuration) => | ||
| { | ||
| configuration.ReadFrom.Configuration(context.Configuration); | ||
| }, true, true) | ||
| .ConfigureWebHostDefaults(webBuilder => | ||
| { | ||
| webBuilder.UseStartup<Startup>(); | ||
| }); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
...RabbitMQ.SqlServer.DispatcherPerGroup/Sample.RabbitMQ.SqlServer.DispatcherPerGroup.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" /> | ||
|
|
||
| <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" /> | ||
| <PackageReference Include="Serilog.Sinks.Seq" Version="5.0.1" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\DotNetCore.CAP.Dashboard\DotNetCore.CAP.Dashboard.csproj" /> | ||
| <ProjectReference Include="..\..\src\DotNetCore.CAP.RabbitMQ\DotNetCore.CAP.RabbitMQ.csproj" /> | ||
| <ProjectReference Include="..\..\src\DotNetCore.CAP.SqlServer\DotNetCore.CAP.SqlServer.csproj" /> | ||
| <ProjectReference Include="..\..\src\DotNetCore.CAP\DotNetCore.CAP.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
52 changes: 52 additions & 0 deletions
52
samples/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/Startup.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using DotNetCore.CAP; | ||
| using DotNetCore.CAP.Internal; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Sample.RabbitMQ.SqlServer.DispatcherPerGroup.TypedConsumers; | ||
| using Serilog; | ||
|
|
||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup | ||
| { | ||
| public class Startup | ||
| { | ||
| // This method gets called by the runtime. Use this method to add services to the container. | ||
| // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 | ||
| public void ConfigureServices(IServiceCollection services) | ||
| { | ||
| services.AddLogging(x => x.AddSerilog()); | ||
|
|
||
| services | ||
| .AddSingleton<IConsumerServiceSelector, TypedConsumerServiceSelector>() | ||
| .AddQueueHandlers(typeof(Startup).Assembly); | ||
|
|
||
| services.AddCap(options => | ||
| { | ||
| options.UseSqlServer("Server=(local);Database=CAP-Test;Trusted_Connection=True;"); | ||
| options.UseRabbitMQ("localhost"); | ||
| options.UseDashboard(); | ||
| options.GroupNamePrefix = "th"; | ||
| options.ConsumerThreadCount = 1; | ||
|
|
||
| options.UseDispatchingPerGroup = true; | ||
| }); | ||
|
|
||
| services.AddControllersWithViews(); | ||
| } | ||
|
|
||
| // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
| public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
| { | ||
| app.UseDeveloperExceptionPage(); | ||
| app.UseSerilogRequestLogging(); | ||
| app.UseCapDashboard(); | ||
| app.UseRouting(); | ||
| app.UseEndpoints(endpoints => | ||
| { | ||
| endpoints.MapControllerRoute( | ||
| name: "default", | ||
| pattern: "{controller=Home}/{action=Index}/{id?}"); | ||
| }); | ||
| } | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
samples/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/TypedConsumers/QueueHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup.TypedConsumers | ||
| { | ||
| public abstract class QueueHandler { } | ||
| } |
15 changes: 15 additions & 0 deletions
15
...Sample.RabbitMQ.SqlServer.DispatcherPerGroup/TypedConsumers/QueueHandlerTopicAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using System; | ||
|
|
||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup.TypedConsumers | ||
| { | ||
| [AttributeUsage(AttributeTargets.Class)] | ||
| public class QueueHandlerTopicAttribute : Attribute | ||
| { | ||
| public string Topic { get; } | ||
|
|
||
| public QueueHandlerTopicAttribute(string topic) | ||
| { | ||
| Topic = topic; | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
...es/Sample.RabbitMQ.SqlServer.DispatcherPerGroup/TypedConsumers/QueueHandlersExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using System; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
|
|
||
| namespace Sample.RabbitMQ.SqlServer.DispatcherPerGroup.TypedConsumers | ||
| { | ||
| internal static class QueueHandlersExtensions | ||
| { | ||
| private static readonly Type queueHandlerType = typeof(QueueHandler); | ||
|
|
||
| public static IServiceCollection AddQueueHandlers(this IServiceCollection services, params Assembly[] assemblies) | ||
| { | ||
| assemblies ??= new[] { Assembly.GetEntryAssembly() }; | ||
|
|
||
| foreach (var type in assemblies.Distinct().SelectMany(x => x.GetTypes().Where(FilterHandlers))) | ||
| { | ||
| services.AddTransient(queueHandlerType, type); | ||
| } | ||
|
|
||
| return services; | ||
| } | ||
|
|
||
| private static bool FilterHandlers(Type t) | ||
| { | ||
| var topic = t.GetCustomAttribute<QueueHandlerTopicAttribute>(); | ||
|
|
||
| return queueHandlerType.IsAssignableFrom(t) && topic != null && t.IsClass && !t.IsAbstract; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.