Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.

Commit 38c8390

Browse files
Merge pull request #27 from AntonyVorontsov/feature/queue-service-separation
WIP: IQueueService separation.
2 parents 30bdd77 + d0d1a2b commit 38c8390

27 files changed

+1019
-341
lines changed

RabbitMQ.Client.Core.DependencyInjection.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{9D0289B2
4242
EndProject
4343
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RabbitMQ.Client.Core.DependencyInjection.Tests", "tests\RabbitMQ.Client.Core.DependencyInjection.Tests\RabbitMQ.Client.Core.DependencyInjection.Tests.csproj", "{85907F19-00B0-4BA6-9B7C-0452A174903D}"
4444
EndProject
45+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.AdvancedConfiguration", "examples\Examples.AdvancedConfiguration\Examples.AdvancedConfiguration.csproj", "{0C713913-8D54-49E7-AADD-45497216E2EF}"
46+
EndProject
4547
Global
4648
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4749
Debug|Any CPU = Debug|Any CPU
@@ -68,6 +70,10 @@ Global
6870
{85907F19-00B0-4BA6-9B7C-0452A174903D}.Debug|Any CPU.Build.0 = Debug|Any CPU
6971
{85907F19-00B0-4BA6-9B7C-0452A174903D}.Release|Any CPU.ActiveCfg = Release|Any CPU
7072
{85907F19-00B0-4BA6-9B7C-0452A174903D}.Release|Any CPU.Build.0 = Release|Any CPU
73+
{0C713913-8D54-49E7-AADD-45497216E2EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
74+
{0C713913-8D54-49E7-AADD-45497216E2EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
75+
{0C713913-8D54-49E7-AADD-45497216E2EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
76+
{0C713913-8D54-49E7-AADD-45497216E2EF}.Release|Any CPU.Build.0 = Release|Any CPU
7177
EndGlobalSection
7278
GlobalSection(SolutionProperties) = preSolution
7379
HideSolutionNode = FALSE
@@ -79,6 +85,7 @@ Global
7985
{1F81E848-7781-448F-BBBB-6A1E509B76CC} = {04EFD8F7-5120-4072-9728-64203F6F4873}
8086
{93D59B0E-856C-4260-B50E-57FE5C0F5073} = {BC4CDBDE-4AEE-44B3-B00B-380EB1AC5E62}
8187
{85907F19-00B0-4BA6-9B7C-0452A174903D} = {9D0289B2-C566-46CC-A53A-471BCBA0F277}
88+
{0C713913-8D54-49E7-AADD-45497216E2EF} = {04EFD8F7-5120-4072-9728-64203F6F4873}
8289
EndGlobalSection
8390
GlobalSection(ExtensibilityGlobals) = postSolution
8491
SolutionGuid = {0EBBD182-65B2-47F9-ABBE-64B5B8C9652F}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Logging;
4+
using RabbitMQ.Client.Core.DependencyInjection;
5+
6+
namespace Examples.AdvancedConfiguration.Controllers
7+
{
8+
[ApiController]
9+
[Route("api/example")]
10+
public class ExampleController : ControllerBase
11+
{
12+
readonly ILogger<ExampleController> _logger;
13+
readonly IProducingService _producingService;
14+
15+
public ExampleController(
16+
IProducingService producingService,
17+
ILogger<ExampleController> logger)
18+
{
19+
_producingService = producingService;
20+
_logger = logger;
21+
}
22+
23+
[HttpGet]
24+
public async Task<IActionResult> Get()
25+
{
26+
_logger.LogInformation($"Sending messages with {typeof(IProducingService)}.");
27+
var message = new { message = "text" };
28+
await _producingService.SendAsync(message, "consumption.exchange", "routing.key");
29+
return Ok(message);
30+
}
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\RabbitMQ.Client.Core.DependencyInjection\RabbitMQ.Client.Core.DependencyInjection.csproj" />
9+
</ItemGroup>
10+
11+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using RabbitMQ.Client.Core.DependencyInjection;
3+
4+
namespace Examples.AdvancedConfiguration.MessageHandlers
5+
{
6+
public class CustomAsyncMessageHandler : IAsyncMessageHandler
7+
{
8+
public async Task Handle(string message, string routingKey)
9+
{
10+
// The message handler does not do anything.
11+
// It is just an example.
12+
await Task.CompletedTask;
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using RabbitMQ.Client.Core.DependencyInjection;
3+
4+
namespace Examples.AdvancedConfiguration.MessageHandlers
5+
{
6+
public class CustomAsyncNonCyclicMessageHandler : IAsyncNonCyclicMessageHandler
7+
{
8+
public async Task Handle(string message, string routingKey, IQueueService queueService)
9+
{
10+
// The message handler does not do anything.
11+
// It is just an example.
12+
await Task.CompletedTask;
13+
}
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using RabbitMQ.Client.Core.DependencyInjection;
2+
3+
namespace Examples.AdvancedConfiguration.MessageHandlers
4+
{
5+
public class CustomMessageHandler : IMessageHandler
6+
{
7+
public void Handle(string message, string routingKey)
8+
{
9+
// The message handler does not do anything.
10+
// It is just an example.
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using RabbitMQ.Client.Core.DependencyInjection;
2+
3+
namespace Examples.AdvancedConfiguration.MessageHandlers
4+
{
5+
public class CustomNonCyclicMessageHandler : INonCyclicMessageHandler
6+
{
7+
public void Handle(string message, string routingKey, IQueueService queueService)
8+
{
9+
// The message handler does not do anything.
10+
// It is just an example.
11+
}
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace Examples.AdvancedConfiguration
5+
{
6+
public static class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
Host.CreateDefaultBuilder(args)
11+
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
12+
.Build()
13+
.Run();
14+
}
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Hosting;
4+
using RabbitMQ.Client.Core.DependencyInjection;
5+
6+
namespace Examples.AdvancedConfiguration.Services
7+
{
8+
public class ConsumingHostedService : IHostedService
9+
{
10+
readonly IConsumingService _consumingService;
11+
12+
public ConsumingHostedService(IConsumingService consumingService)
13+
{
14+
_consumingService = consumingService;
15+
}
16+
17+
public Task StartAsync(CancellationToken cancellationToken)
18+
{
19+
_consumingService.StartConsuming();
20+
return Task.CompletedTask;
21+
}
22+
23+
public Task StopAsync(CancellationToken cancellationToken)
24+
{
25+
return Task.CompletedTask;
26+
}
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Examples.AdvancedConfiguration.MessageHandlers;
2+
using Examples.AdvancedConfiguration.Services;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using RabbitMQ.Client.Core.DependencyInjection;
8+
9+
namespace Examples.AdvancedConfiguration
10+
{
11+
public class Startup
12+
{
13+
private IConfiguration Configuration { get; }
14+
15+
public Startup(IConfiguration configuration)
16+
{
17+
Configuration = configuration;
18+
}
19+
20+
public void ConfigureServices(IServiceCollection services)
21+
{
22+
services.AddControllers();
23+
24+
// Configurations are the same (same user) and same password, but the only difference - names of connections.
25+
var rabbitMqConsumerSection = Configuration.GetSection("RabbitMqConsumer");
26+
var rabbitMqProducerSection = Configuration.GetSection("RabbitMqProducer");
27+
28+
var producingExchangeSection = Configuration.GetSection("ProducingExchange");
29+
var consumingExchangeSection = Configuration.GetSection("ConsumingExchange");
30+
31+
// There is an example of configuring different message handlers with different parameters.
32+
// You can set connection of routing keys or specify the exact exchange that will be listened by giver routing keys (or route patterns) by message handlers.
33+
// You can also register singleton or transient RabbitMQ clients (IConsumer and IProducer) and message handlers.
34+
// There are a lot of different extension methods that is better take a closer look to.
35+
services.AddRabbitMqConsumingClientSingleton(rabbitMqConsumerSection)
36+
.AddRabbitMqProducingClientSingleton(rabbitMqProducerSection)
37+
.AddProductionExchange("exchange.to.send.messages.only", producingExchangeSection)
38+
.AddConsumptionExchange("consumption.exchange", consumingExchangeSection)
39+
.AddMessageHandlerTransient<CustomMessageHandler>("routing.key")
40+
.AddAsyncMessageHandlerTransient<CustomAsyncMessageHandler>(new[] { "routing.key", "another.routing.key" })
41+
.AddNonCyclicMessageHandlerSingleton<CustomNonCyclicMessageHandler>("*.*", "consumption.exchange")
42+
.AddAsyncNonCyclicMessageHandlerSingleton<CustomAsyncNonCyclicMessageHandler>("#", "consumption.exchange");
43+
44+
services.AddHostedService<ConsumingHostedService>();
45+
}
46+
47+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
48+
{
49+
app.UseRouting();
50+
app.UseEndpoints(endpoints => endpoints.MapControllers());
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)