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

Commit 83b21f4

Browse files
author
Anton Vorontsov
committed
Merge branch 'master' into feature/stop-consuming
2 parents 5ca6036 + 8feb43c commit 83b21f4

File tree

83 files changed

+1341
-171
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1341
-171
lines changed

.github/workflows/dotnetcore.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ on: [push]
44

55
jobs:
66
workflow:
7-
87
runs-on: ubuntu-latest
98
steps:
109
- uses: actions/checkout@v2

RabbitMQ.Client.Core.DependencyInjection.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RabbitMQ.Client.Core.Depend
4545
EndProject
4646
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples.AdvancedConfiguration", "examples\Examples.AdvancedConfiguration\Examples.AdvancedConfiguration.csproj", "{0C713913-8D54-49E7-AADD-45497216E2EF}"
4747
EndProject
48+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples.BatchMessageHandler", "examples\Examples.BatchMessageHandler\Examples.BatchMessageHandler.csproj", "{BC9BE326-AEFE-42F2-B592-80126188514D}"
49+
EndProject
4850
Global
4951
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5052
Debug|Any CPU = Debug|Any CPU
@@ -75,6 +77,10 @@ Global
7577
{0C713913-8D54-49E7-AADD-45497216E2EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
7678
{0C713913-8D54-49E7-AADD-45497216E2EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
7779
{0C713913-8D54-49E7-AADD-45497216E2EF}.Release|Any CPU.Build.0 = Release|Any CPU
80+
{BC9BE326-AEFE-42F2-B592-80126188514D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
81+
{BC9BE326-AEFE-42F2-B592-80126188514D}.Debug|Any CPU.Build.0 = Debug|Any CPU
82+
{BC9BE326-AEFE-42F2-B592-80126188514D}.Release|Any CPU.ActiveCfg = Release|Any CPU
83+
{BC9BE326-AEFE-42F2-B592-80126188514D}.Release|Any CPU.Build.0 = Release|Any CPU
7884
EndGlobalSection
7985
GlobalSection(SolutionProperties) = preSolution
8086
HideSolutionNode = FALSE
@@ -87,6 +93,7 @@ Global
8793
{93D59B0E-856C-4260-B50E-57FE5C0F5073} = {BC4CDBDE-4AEE-44B3-B00B-380EB1AC5E62}
8894
{85907F19-00B0-4BA6-9B7C-0452A174903D} = {9D0289B2-C566-46CC-A53A-471BCBA0F277}
8995
{0C713913-8D54-49E7-AADD-45497216E2EF} = {04EFD8F7-5120-4072-9728-64203F6F4873}
96+
{BC9BE326-AEFE-42F2-B592-80126188514D} = {04EFD8F7-5120-4072-9728-64203F6F4873}
9097
EndGlobalSection
9198
GlobalSection(ExtensibilityGlobals) = postSolution
9299
SolutionGuid = {0EBBD182-65B2-47F9-ABBE-64B5B8C9652F}

docs/changelog.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
All notable changes to this library will be documented in this file.
44

5+
## [4.0.0] - 2020-05-05
6+
7+
### Added
8+
9+
- `BaseBatchMessageHandler` and `BatchMessageHandler` for handling messages in batches via prefetch count feature.
10+
- Example of basic usages of batch message handlers.
11+
12+
### Updated
13+
14+
- Updated RabbitMQ.Client to the newest version 6.0.0. Made some changes according to the breaking changes that come with the newest version of RabbitMQ.Client.
15+
- Moved message handlers to the different namespace `RabbitMQ.Client.Core.DependencyInjection.MessageHandlers`.
16+
- Moved internal DI extensions to the different namespace `RabbitMQ.Client.Core.DependencyInjection.InternalExtensions`.
17+
18+
## [3.2.1] - 2020-03-29
19+
20+
### Fixed
21+
22+
- Fixed message acknowledgement when both `IConsumingService` and `IProducingService` in use.
23+
24+
### Updated
25+
26+
- Updated Microsoft libraries to the newer version (3.1.3).
27+
528
## [3.2.0] - 2020-02-14
629

730
### Added

docs/message-consumption.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,86 @@ The message handling process is organized as follows:
311311
- If any exception occurs `IMessageHandlingService` acknowledges the message anyway and checks if the message has to be re-send. If exchange option `RequeueFailedMessages` is set `true` then `IMessageHandlingService` adds a header `"requeued"` to the message and sends it again with delay in 60 seconds. Mechanism of sending delayed messages covered in the message production [documentation](message-production.md).
312312
- If any exception occurs within handling the message that has been already re-sent that message will not be re-send again (re-send happens only once).
313313

314+
### Batch message handlers
315+
316+
There are also a feature that you can use in case of necessity of handling messages in batches.
317+
First of all you have to create a class that inherits a `BatchMessageHandler` class.
318+
You have to set up values for `QueueName` and `PrefetchCount` properties. These values are responsible for the queue that will be read by the message handler, and the size of batches of messages.
319+
320+
```c#
321+
public class CustomBatchMessageHandler : BatchMessageHandler
322+
{
323+
readonly ILogger<CustomBatchMessageHandler> _logger;
324+
325+
public CustomBatchMessageHandler(
326+
IRabbitMqConnectionFactory rabbitMqConnectionFactory,
327+
IEnumerable<BatchConsumerConnectionOptions> batchConsumerConnectionOptions,
328+
ILogger<CustomBatchMessageHandler> logger)
329+
: base(rabbitMqConnectionFactory, batchConsumerConnectionOptions, logger)
330+
{
331+
_logger = logger;
332+
}
333+
334+
public override ushort PrefetchCount { get; set; } = 50;
335+
336+
public override string QueueName { get; set; } = "another.queue.name";
337+
338+
public override Task HandleMessages(IEnumerable<string> messages, CancellationToken cancellationToken)
339+
{
340+
_logger.LogInformation("Handling a batch of messages.");
341+
foreach (var message in messages)
342+
{
343+
_logger.LogInformation(message);
344+
}
345+
return Task.CompletedTask;
346+
}
347+
}
348+
```
349+
350+
If you want to get raw messages as `ReadOnlyMemory<byte>` you can inherit base message handler class.
351+
352+
```c#
353+
public class CustomBatchMessageHandler : BaseBatchMessageHandler
354+
{
355+
readonly ILogger<CustomBatchMessageHandler> _logger;
356+
357+
public CustomBatchMessageHandler(
358+
IRabbitMqConnectionFactory rabbitMqConnectionFactory,
359+
IEnumerable<BatchConsumerConnectionOptions> batchConsumerConnectionOptions,
360+
ILogger<CustomBatchMessageHandler> logger)
361+
: base(rabbitMqConnectionFactory, batchConsumerConnectionOptions, logger)
362+
{
363+
_logger = logger;
364+
}
365+
366+
public override ushort PrefetchCount { get; set; } = 3;
367+
368+
public override string QueueName { get; set; } = "queue.name";
369+
370+
public override Task HandleMessages(IEnumerable<ReadOnlyMemory<byte>> messages, CancellationToken cancellationToken)
371+
{
372+
_logger.LogInformation("Handling a batch of messages.");
373+
foreach (var message in messages)
374+
{
375+
var stringifiedMessage = Encoding.UTF8.GetString(message.ToArray());
376+
_logger.LogInformation(stringifiedMessage);
377+
}
378+
return Task.CompletedTask;
379+
}
380+
}
381+
```
382+
383+
After all you have to register that batch message handler via DI.
384+
```c#
385+
services.AddBatchMessageHandler<CustomBatchMessageHandler>(Configuration.GetSection("RabbitMq"));
386+
```
387+
388+
The message handler will create a separate connection and use it for reading messages.
389+
When the message collection is full to the size of `PrefetchCount` they are passed to the `HandleMessage` method.
390+
Both `BaseBatchMessageHandler` and `BatchMessageHandler` implement `IDisposable` interface, so you can use it for release of resources.
391+
392+
Use this method of getting messages only when you sure that the number of messages that pass through this queue is really huge. Otherwise, messages could stack in the temporary collection of messages waiting to get in full.
393+
314394
For message production features see the [Previous page](message-production.md)
315395

316396
For more information about advanced usage of the RabbitMQ client see the [Next page](advanced-usage.md)

examples/Examples.AdvancedConfiguration/Controllers/ExampleController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Threading.Tasks;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.Extensions.Logging;
4-
using RabbitMQ.Client.Core.DependencyInjection;
4+
using RabbitMQ.Client.Core.DependencyInjection.Services;
55

66
namespace Examples.AdvancedConfiguration.Controllers
77
{

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomAsyncMessageHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Threading.Tasks;
2-
using RabbitMQ.Client.Core.DependencyInjection;
2+
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
33

44
namespace Examples.AdvancedConfiguration.MessageHandlers
55
{

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomAsyncNonCyclicMessageHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Threading.Tasks;
2-
using RabbitMQ.Client.Core.DependencyInjection;
2+
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
3+
using RabbitMQ.Client.Core.DependencyInjection.Services;
34

45
namespace Examples.AdvancedConfiguration.MessageHandlers
56
{

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomMessageHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using RabbitMQ.Client.Core.DependencyInjection;
1+
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
22

33
namespace Examples.AdvancedConfiguration.MessageHandlers
44
{

examples/Examples.AdvancedConfiguration/MessageHandlers/CustomNonCyclicMessageHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using RabbitMQ.Client.Core.DependencyInjection;
1+
using RabbitMQ.Client.Core.DependencyInjection.MessageHandlers;
2+
using RabbitMQ.Client.Core.DependencyInjection.Services;
23

34
namespace Examples.AdvancedConfiguration.MessageHandlers
45
{

examples/Examples.AdvancedConfiguration/Services/ConsumingHostedService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Threading;
22
using System.Threading.Tasks;
33
using Microsoft.Extensions.Hosting;
4-
using RabbitMQ.Client.Core.DependencyInjection;
4+
using RabbitMQ.Client.Core.DependencyInjection.Services;
55

66
namespace Examples.AdvancedConfiguration.Services
77
{

0 commit comments

Comments
 (0)