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

Commit 85c3560

Browse files
author
Anton Vorontsov
committed
Changelog and documentation about batch message handlers.
1 parent c930436 commit 85c3560

File tree

4 files changed

+19
-47
lines changed

4 files changed

+19
-47
lines changed

docs/changelog.md

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

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

5+
## [4.3.0] - Will be drafted
6+
7+
### Changed
8+
9+
- **Breaking!** `BatchMessageHandler` has been removed, `BaseBatchMessageHandler` is now one and only base class for handling messages in batches. `HandleMessages` method of `BaseBatchMessageHandler` gets a collection of messages as `BasicDeliverEventArgs` instead of bytes.
10+
511
## [4.2.0] - 2020-10-01
612

713
### Added

docs/message-consumption.md

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ The message handling process organized as follows:
314314

315315
### Batch message handlers
316316

317-
There are also a feature that you can use in case of necessity of handling messages in batches.
318-
First of all you have to create a class that inherits a `BatchMessageHandler` class.
317+
There is a feature that you can use in case of necessity of handling messages in batches.
318+
First of all you have to create a class that inherits `BaseBatchMessageHandler`.
319319
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. You can also set a `MessageHandlingPeriod` property value and the method `HandleMessage` will be executed repeatedly so messages in unfilled batches could be processed too, but keep in mind that this property is optional.
320320
Be aware that batch message handlers **do not declare queues**, so if it does not exist an exception will be thrown. Either declare manually or using RabbitMqClient configuration features.
321321

322322
```c#
323-
public class CustomBatchMessageHandler : BatchMessageHandler
323+
public class CustomBatchMessageHandler : BaseBatchMessageHandler
324324
{
325325
readonly ILogger<CustomBatchMessageHandler> _logger;
326326

@@ -339,61 +339,26 @@ public class CustomBatchMessageHandler : BatchMessageHandler
339339

340340
public override TimeSpan? MessageHandlingPeriod { get; set; } = TimeSpan.FromMilliseconds(500);
341341

342-
public override Task HandleMessages(IEnumerable<string> messages, CancellationToken cancellationToken)
343-
{
344-
_logger.LogInformation("Handling a batch of messages.");
345-
foreach (var message in messages)
346-
{
347-
_logger.LogInformation(message);
348-
}
349-
return Task.CompletedTask;
350-
}
351-
}
352-
```
353-
354-
If you want to get raw messages as `ReadOnlyMemory<byte>` you can inherit base message handler class.
355-
356-
```c#
357-
public class CustomBatchMessageHandler : BaseBatchMessageHandler
358-
{
359-
readonly ILogger<CustomBatchMessageHandler> _logger;
360-
361-
public CustomBatchMessageHandler(
362-
IRabbitMqConnectionFactory rabbitMqConnectionFactory,
363-
IEnumerable<BatchConsumerConnectionOptions> batchConsumerConnectionOptions,
364-
ILogger<CustomBatchMessageHandler> logger)
365-
: base(rabbitMqConnectionFactory, batchConsumerConnectionOptions, logger)
366-
{
367-
_logger = logger;
368-
}
369-
370-
public override ushort PrefetchCount { get; set; } = 3;
371-
372-
public override string QueueName { get; set; } = "queue.name";
373-
374-
public override Task HandleMessages(IEnumerable<ReadOnlyMemory<byte>> messages, CancellationToken cancellationToken)
342+
public override Task HandleMessages(IEnumerable<BasicDeliverEventArgs> messages, CancellationToken cancellationToken)
375343
{
376344
_logger.LogInformation("Handling a batch of messages.");
377345
foreach (var message in messages)
378346
{
379-
var stringifiedMessage = Encoding.UTF8.GetString(message.ToArray());
380-
_logger.LogInformation(stringifiedMessage);
347+
_logger.LogInformation(message.GetMessage());
381348
}
382349
return Task.CompletedTask;
383350
}
384351
}
385352
```
386353

387354
After all you have to register that batch message handler via DI.
355+
388356
```c#
389357
services.AddBatchMessageHandler<CustomBatchMessageHandler>(Configuration.GetSection("RabbitMq"));
390358
```
391359

392360
The message handler will create a separate connection and use it for reading messages.
393-
When the message collection is full to the size of `PrefetchCount` they are passed to the `HandleMessage` method.
394-
Both `BaseBatchMessageHandler` and `BatchMessageHandler` implement `IDisposable` interface, so you can use it for release of resources.
395-
396-
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.
361+
When the message collection is full to the size of `PrefetchCount` it will be passed to the `HandleMessage` method.
397362

398363
For message production features see the [Previous page](message-production.md)
399364

readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ For more information about `appsettings.json` and manual configuration features,
240240
## Batch message handlers
241241

242242
There are also a feature that you can use in case of necessity of handling messages in batches.
243-
First of all you have to create a class that inherits a `BatchMessageHandler` class.
243+
First of all you have to create a class that inherits a `BaseBatchMessageHandler` class.
244244
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.
245245

246246
```c#
247-
public class CustomBatchMessageHandler : BatchMessageHandler
247+
public class CustomBatchMessageHandler : BaseBatchMessageHandler
248248
{
249249
readonly ILogger<CustomBatchMessageHandler> _logger;
250250

@@ -263,19 +263,20 @@ public class CustomBatchMessageHandler : BatchMessageHandler
263263

264264
public override TimeSpan? MessageHandlingPeriod { get; set; } = TimeSpan.FromMilliseconds(500);
265265

266-
public override Task HandleMessages(IEnumerable<string> messages, CancellationToken cancellationToken)
266+
public override Task HandleMessages(IEnumerable<BasicDeliverEventArgs> messages, CancellationToken cancellationToken)
267267
{
268268
_logger.LogInformation("Handling a batch of messages.");
269269
foreach (var message in messages)
270270
{
271-
_logger.LogInformation(message);
271+
_logger.LogInformation(message.GetMessage());
272272
}
273273
return Task.CompletedTask;
274274
}
275275
}
276276
```
277277

278278
After all you have to register that batch message handler via DI.
279+
279280
```c#
280281
services.AddBatchMessageHandler<CustomBatchMessageHandler>(Configuration.GetSection("RabbitMq"));
281282
```

src/RabbitMQ.Client.Core.DependencyInjection/RabbitMQ.Client.Core.DependencyInjection.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<LangVersion>latest</LangVersion>
6-
<Version>4.2.0</Version>
6+
<Version>4.3.0</Version>
77
<PackageTags>RabbitMQ</PackageTags>
88
<RepositoryUrl>https://github.com/AntonyVorontsov/RabbitMQ.Client.Core.DependencyInjection</RepositoryUrl>
99
<RepositoryType>GIT</RepositoryType>

0 commit comments

Comments
 (0)