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

Commit 49b1672

Browse files
author
Anton Vorontsov
committed
Added documentation and removed sequential execution for tests.
1 parent e82631f commit 49b1672

File tree

6 files changed

+14
-8
lines changed

6 files changed

+14
-8
lines changed

docs/advanced-usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Advanced usage
22

3-
`IQueueService` is an interface that implements two other interfaces - `IConsumingService` and `IProducingService`. By default a RabbitMQ Client is registered as `IQueueService` without a logical separation at producing and consuming code. Thus, you can inject only a `IQueueService` instance, and `IConsumingService` or `IProducingService` won't be available. This is not a real deal until you want to control the way a RabbitMQ Client connects to the server.
3+
`IQueueService` is an interface that implements two other interfaces - `IConsumingService` and `IProducingService`. By default, a RabbitMQ Client is registered as `IQueueService` without a logical separation at producing and consuming code. Thus, you can inject only a `IQueueService` instance, and `IConsumingService` or `IProducingService` won't be available. This is not a real deal until you want to control the way a RabbitMQ Client connects to the server.
44

5-
An instance of `IQueueService` opens two connections to the RabbitMQ server, one is for message production and the other one is for message consumption. Normally a RabbitMQ Client is added in a singleton mode, so both connections stay opened while application is running. It is also noticeable that a RabbitMQ Client uses the same credentials for both connections. If you add `IQueueService` in a transient mode (via the `AddRabbitMqClientTransient` extension method) both connections will be opened each time `IQueueService` is being injected somewhere else. This behavior does not fit everybody, so you can change it a little.
5+
An instance of `IQueueService` opens two connections to the RabbitMQ server, one is for message production, and the other one is for message consumption. Normally a RabbitMQ Client is added in a singleton mode, so both connections stay opened while application is running. It is also noticeable that a RabbitMQ Client uses the same credentials for both connections. If you add `IQueueService` in the transient mode (via the `AddRabbitMqClientTransient` extension method) both connections will be opened each time `IQueueService` is being injected somewhere else. This behavior does not fit everybody, so you can change it a little.
66

77
You are allowed to register a RabbitMQ Client as an implementation of two interfaces that have been mentioned before - `IConsumingService` and `IProducingService`. Each interface defines its own connection and its own collection of methods, obviously, for message production and message consumption. You can also use different credentials for different connections, and there is an option `ClientProvidedName` which allows you to create a "named" connection (which will be easier to find in the RabbitMQ management UI). There is also a possibility of registering `IConsumingService` and `IProducingService` in different lifetime modes, in case you want your consumption connection to be persist (singleton `IConsumingService`) and open a connection each time you want to send a message (a transient `IProducingService`). This situation will be covered in code examples below.
88

docs/message-consumption.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ The message handling process organized as follows:
316316

317317
There are also a feature that you can use in case of necessity of handling messages in batches.
318318
First of all you have to create a class that inherits a `BatchMessageHandler` class.
319-
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.
320-
You have to be aware that batch message handlers do not declare queues, so if it does not exists an exception will be thrown. Either declare manually or using RabbitMqClient configuration features.
319+
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.
320+
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#
323323
public class CustomBatchMessageHandler : BatchMessageHandler
@@ -337,6 +337,8 @@ public class CustomBatchMessageHandler : BatchMessageHandler
337337

338338
public override string QueueName { get; set; } = "another.queue.name";
339339

340+
public override TimeSpan? MessageHandlingPeriod { get; set; } = TimeSpan.FromMilliseconds(500);
341+
340342
public override Task HandleMessages(IEnumerable<string> messages, CancellationToken cancellationToken)
341343
{
342344
_logger.LogInformation("Handling a batch of messages.");

examples/Examples.BatchMessageHandler/AnotherCustomBatchMessageHandler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Threading;
34
using System.Threading.Tasks;
@@ -25,6 +26,9 @@ public AnotherCustomBatchMessageHandler(
2526
// You have to be aware that BaseBatchMessageHandler does not declare the specified queue. So if it does not exists an exception will be thrown.
2627
public override string QueueName { get; set; } = "another.queue.name";
2728

29+
// This thing will fire message handling if there are not enough messages, but timeout is already off.
30+
public override TimeSpan? MessageHandlingPeriod { get; set; } = TimeSpan.FromMilliseconds(500);
31+
2832
public override Task HandleMessages(IEnumerable<string> messages, CancellationToken cancellationToken)
2933
{
3034
_logger.LogInformation("Handling a batch of messages.");

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ public class CustomBatchMessageHandler : BatchMessageHandler
259259

260260
public override ushort PrefetchCount { get; set; } = 50;
261261

262-
public override string QueueName { get; set; } = "another.queue.name";
262+
public override string QueueName { get; set; } = "queue.name";
263+
264+
public override TimeSpan? MessageHandlingPeriod { get; set; } = TimeSpan.FromMilliseconds(500);
263265

264266
public override Task HandleMessages(IEnumerable<string> messages, CancellationToken cancellationToken)
265267
{
@@ -279,7 +281,7 @@ services.AddBatchMessageHandler<CustomBatchMessageHandler>(Configuration.GetSect
279281
```
280282

281283
The message handler will create a separate connection and use it for reading messages.
282-
When the message collection is full to the size of `PrefetchCount` they are passed to the `HandleMessage` method. For more information, see the [message-consuming](./docs/message-consumption.md) documentation file.
284+
When the message collection is full to the size of `PrefetchCount` they are passed to the `HandleMessage` method. 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. For more information, see the [message-consuming](./docs/message-consumption.md) documentation file.
283285

284286
## Advanced usage and nuances
285287

tests/RabbitMQ.Client.Core.DependencyInjection.Tests/UnitTests/BaseBatchMessageHandlerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
namespace RabbitMQ.Client.Core.DependencyInjection.Tests.UnitTests
1616
{
17-
[Collection("Sequential tests")]
1817
public class BaseBatchMessageHandlerTests
1918
{
2019
readonly TimeSpan _globalTestsTimeout = TimeSpan.FromSeconds(60);

tests/RabbitMQ.Client.Core.DependencyInjection.Tests/UnitTests/BatchMessageHandlerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
namespace RabbitMQ.Client.Core.DependencyInjection.Tests.UnitTests
1616
{
17-
[Collection("Sequential tests")]
1817
public class BatchMessageHandlerTests
1918
{
2019
readonly TimeSpan _globalTestsTimeout = TimeSpan.FromSeconds(60);

0 commit comments

Comments
 (0)