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

Commit 4d606ab

Browse files
Started making advanced usage docs.
1 parent 7ba697d commit 4d606ab

File tree

4 files changed

+89
-13
lines changed

4 files changed

+89
-13
lines changed

advanced-usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Advanced usage
2+
3+
todo
4+
5+
6+
For basic message consumption features see the [Previous page](message-consumption.md)

docs/message-consumption.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,6 @@ 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-
For message production features see the [Previous page](message-production.md)
314+
For message production features see the [Previous page](message-production.md)
315+
316+
For more information about advanced usage of the RabbitMQ client see the [Next page](advanced-usage.md)

docs/readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ These files cover all functionality of the library and if the new feature comes
44
Documentation consists of four sections. **RabbitMQ configuration section** covers ways of configuring the connection to the RabbitMQ host or multiple hosts (HA cluster).
55
**Exchange configuration section** explains the difference between consumption and production exchanges in terms of this library as well as specific nuances of configuring them.
66
**Message production section** says about features of sending messages and **message consuming section** covers ways of receiving and handling messages.
7+
Last section **Advanced usage** covers specific usage of the RabbitMQ client that can be useful for users who want to control behavior of RabbitMQ client tighter.
78

89
## Table of contents
910

1011
- [RabbitMQ configuration](rabbit-configuration.md)
1112
- [Exchange configuration](exchange-configuration.md)
1213
- [Message production](message-production.md)
13-
- [Message consumption](message-consumption.md)
14+
- [Message consumption](message-consumption.md)
15+
- [Advanced usage](advanced-usage.md)

readme.md

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public void ConfigureServices(IServiceCollection services)
2020
{
2121
var rabbitMqSection = Configuration.GetSection("RabbitMq");
2222
var exchangeSection = Configuration.GetSection("RabbitMqExchange");
23-
23+
2424
services.AddRabbitMqClient(rabbitMqSection)
2525
.AddProductionExchange("exchange.name", exchangeSection);
2626
}
2727
```
2828

29-
By calling `AddRabbitMqClient` you add `IQueueService` that provides functionality of sending messages to queues. `AddProductionExchange` configures exchange to queues bindings (presented in json configuration) that allow messages to route properly.
29+
By calling `AddRabbitMqClient` you add `IQueueService` that provides functionality of sending messages to queues. `AddProductionExchange` configures exchange to queues bindings (presented in json configuration) that allow messages to route properly.
3030
Example of `appsettings.json` is two sections below. You can also configure everything manually. For more information, see [rabbit-configuration](./docs/rabbit-configuration.md) and [exchange-configuration](./docs/exchange-configuration.md) documentation files.
3131

3232
Now you can inject an instance of `IQueueService` inside anything you want.
@@ -69,7 +69,7 @@ _queueService.Send(
6969
```
7070

7171
The mechanism of sending delayed messages described in the [documentation](./docs/message-production.md). Dive into it for more detailed information.
72-
72+
7373
### Consumer
7474

7575
After making the message production possible let's make the consumption possible too! Imagine that a consumer is a simple console application.
@@ -79,27 +79,27 @@ class Program
7979
{
8080
const string ExchangeName = "exchange.name";
8181
public static IConfiguration Configuration { get; set; }
82-
82+
8383
static void Main()
8484
{
8585
var builder = new ConfigurationBuilder()
8686
.SetBasePath(Directory.GetCurrentDirectory())
8787
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
8888
Configuration = builder.Build();
89-
89+
9090
var serviceCollection = new ServiceCollection();
9191
ConfigureServices(serviceCollection);
92-
92+
9393
var serviceProvider = serviceCollection.BuildServiceProvider();
9494
var queueService = serviceProvider.GetRequiredService<IQueueService>();
9595
queueService.StartConsuming();
9696
}
97-
97+
9898
static void ConfigureServices(IServiceCollection services)
9999
{
100100
var rabbitMqSection = Configuration.GetSection("RabbitMq");
101101
var exchangeSection = Configuration.GetSection("RabbitMqExchange");
102-
102+
103103
services.AddRabbitMqClient(rabbitMqSection)
104104
.AddConsumptionExchange("exchange.name", exchangeSection)
105105
.AddMessageHandlerSingleton<CustomMessageHandler>("routing.key");
@@ -132,20 +132,82 @@ public class CustomMessageHandler : IMessageHandler
132132
{
133133
_logger = logger;
134134
}
135-
135+
136136
public void Handle(string message, string routingKey)
137137
{
138138
// Do whatever you want with the message.
139139
_logger.LogInformation("Hello world");
140140
}
141141
}
142142
```
143-
There are async and non-cyclic message handler types, which allow you to do additional stuff. For more information, see the [message-consuming](./docs/message-consumption.md) documentation file.
143+
144+
If you want to use an async magic then implement your custom `IAsyncMessageHandler`.
145+
146+
```c#
147+
public class CustomAsyncMessageHandler : IAsyncMessageHandler
148+
{
149+
readonly ILogger<CustomAsyncMessageHandler> _logger;
150+
public CustomAsyncMessageHandler(ILogger<CustomAsyncMessageHandler> logger)
151+
{
152+
_logger = logger;
153+
}
154+
155+
public async Task Handle(string message, string routingKey)
156+
{
157+
// await something.
158+
}
159+
}
160+
```
161+
162+
If you want to send messages from the `Handle` method inside your message handler then use `INonCyclicMessageHandler`.
163+
You can't inject `IQueueService` inside any message handlers, otherwise you will get a cyclic dependency exception. But `INonCyclicMessageHandler` allows you to avoid this as it accepts an instance of `IQueueService` as a parameter of the method `Handle`.
164+
165+
```c#
166+
public class MyNonCyclicMessageHandler : INonCyclicMessageHandler
167+
{
168+
// Inject anything you want except IQueueService.
169+
readonly ILogger<MyNonCyclicMessageHandler> _logger;
170+
public MyNonCyclicMessageHandler(ILogger<MyNonCyclicMessageHandler> logger)
171+
{
172+
_logger = logger;
173+
}
174+
175+
public void Handle(string message, string routingKey, IQueueService queueService)
176+
{
177+
// Send anything you want using IQueueService instance.
178+
var anotherMessage = new MyMessage { Foo = "Bar" };
179+
queueService.Send(anotherMessage, "exchange.name", "routing.key");
180+
}
181+
}
182+
```
183+
184+
`INonCyclicMessageHandler` has its asynchrony analogue IAsyncNonCyclicMessageHandler`.
185+
186+
```c#
187+
public class MyAsyncNonCyclicMessageHandler : IAsyncNonCyclicMessageHandler
188+
{
189+
// Inject anything you want except IQueueService.
190+
readonly ILogger<MyAsyncNonCyclicMessageHandler> _logger;
191+
public MyNonCyclicMessageHandler(ILogger<MyAsyncNonCyclicMessageHandler> logger)
192+
{
193+
_logger = logger;
194+
}
195+
196+
public async Task Handle(string message, string routingKey, IQueueService queueService)
197+
{
198+
// Do async stuff.
199+
var anotherMessage = new MyMessage { Foo = "Bar" };
200+
await queueService.SendAsync(anotherMessage, "exchange.name", "routing.key");
201+
}
202+
}
203+
```
204+
205+
You can register any of those message handlers the same way you registered `IMessageHandler` before. There are similar extension methods for each type of message handlers. For more information, see the [message-consuming](./docs/message-consumption.md) documentation file.
144206

145207
You can also find example projects in the repository inside the [examples](./examples) directory.
146208

147209
### Configuration
148-
210+
149211
In both cases for producing and consuming messages the configuration file is the same. `appsettings.json` consists of those sections: (1) settings to connect to the RabbitMQ server and (2) sections that configure exchanges and queue bindings. You can have multiple exchanges and one configuration section per each exchange.
150212
Exchange sections define how to bind queues and exchanges with each other using specified routing keys (or patterns). You allowed to bind a queue to an exchange with more than one routing key, but if there are no routing keys in the queue section, then that queue will be bound to the exchange with its name.
151213

@@ -175,6 +237,10 @@ Exchange sections define how to bind queues and exchanges with each other using
175237

176238
For more information about `appsettings.json` and manual configuration features, see [rabbit-configuration](./docs/rabbit-configuration.md) and [exchange-configuration](./docs/exchange-configuration.md) documentation files.
177239

240+
## Important nuances and advanced usage
241+
242+
RabbitMQ client implemented in this library (class which implements `IQueueService`) opens two connections to the RabbitMQ server. One connection is used for message production and the other one is for message consumption. This behavior embedded since 3.2.0 version of the library.
243+
178244
## Changelog
179245

180246
All notable changes are being tracked in the [changelog](./docs/changelog.md) file.

0 commit comments

Comments
 (0)