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

Commit 7759acc

Browse files
author
Anton Vorontsov
committed
Finished documentation about message consumption.
1 parent 85c3560 commit 7759acc

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

docs/changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

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

5-
## [4.3.0] - Will be drafted
5+
## [4.3.0] - 2020-10-03
6+
7+
### Added
8+
9+
- `BasicDeliverEventArgs` extensions for parsing messages.
610

711
### Changed
812

13+
- **Breaking!** `IMessageHandler`, `IAsyncMessageHandler`, `INonCyclicMessageHandler` and `IAsyncNonCyclicMessageHandler` get messages in `Handle` methods as `BasicDeliverEventArgs` instead of string values.
914
- **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.
1015

1116
## [4.2.0] - 2020-10-01

docs/message-consumption.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ The second step is to define classes that will take responsibility of handling r
109109

110110
### Synchronous message handlers
111111

112-
`IMessageHandler` consists of one method `Handle` that gets a message in a string format. You can deserialize that message (if it is a json message) or handle its raw value.
112+
`IMessageHandler` consists of one method `Handle` that gets a message. You can deserialize that message with `BasicDeliverEventArgs` extensions (described below).
113113
Thus, a message handler will look like this.
114114

115115
```c#
116116
public class CustomMessageHandler : IMessageHandler
117117
{
118-
public void Handle(string message, string routingKey)
118+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
119119
{
120120
// Do whatever you want.
121-
var messageObject = JsonConvert.DeserializeObject<YourClass>(message);
121+
var messageObject = eventArgs.GetPayload<YourClass>();
122122
}
123123
}
124124
```
@@ -134,9 +134,9 @@ public class CustomMessageHandler : IMessageHandler
134134
_logger = logger;
135135
}
136136

137-
public void Handle(string message, string routingKey)
137+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
138138
{
139-
_logger.LogInformation($"I got a message {message} by routing key {routingKey}");
139+
_logger.LogInformation($"I got a message {eventArgs.GetMessage()} by routing key {matchingRoute}");
140140
}
141141
}
142142
```
@@ -153,10 +153,10 @@ public class CustomNonCyclicMessageHandler : INonCyclicMessageHandler
153153
_logger = logger;
154154
}
155155

156-
public void Handle(string message, string routingKey, IQueueService queueService)
156+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute, IQueueService queueService)
157157
{
158158
_logger.LogInformation("Got a message. I will send it back to another queue.");
159-
var response = new { Message = message };
159+
var response = new { Message = eventArgs.GetMessage() };
160160
queueService.Send(response, "exchange.name", "routing.key");
161161
}
162162
}
@@ -171,7 +171,7 @@ public class CustomNonCyclicMessageHandler : INonCyclicMessageHandler
171171
```c#
172172
public class CustomAsyncMessageHandler : IAsyncMessageHandler
173173
{
174-
public async Task Handle(string message, string routingKey)
174+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
175175
{
176176
// Do whatever you want asynchronously!
177177
}
@@ -191,10 +191,10 @@ public class CustomAsyncNonCyclicMessageHandler : IAsyncNonCyclicMessageHandler
191191
_logger = logger;
192192
}
193193

194-
public async Task Handle(string message, string routingKey, IQueueService queueService)
194+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute, IQueueService queueService)
195195
{
196196
_logger.LogInformation("You can do something async, e.g. send message back.");
197-
var response = new { Message = message };
197+
var response = new { Message = eventArgs.GetMessage() };
198198
await queueService.SendAsync(response, "exchange.name", "routing.key");
199199
}
200200
}
@@ -306,7 +306,7 @@ services.AddRabbitMqClient(clientConfiguration)
306306
The message handling process organized as follows:
307307

308308
- `IQueueMessage` receives a message and delegates it to `IMessageHandlingService`.
309-
- `IMessageHandlingService` gets a message (as a byte array) and decodes it to the UTF8 string. It also checks if there are any message handlers in a combined collection of `IMessageHandler`, `IAsyncMessageHandler`, `INonCyclicMessageHandler` and `IAsyncNonCyclicMessageHandler` instances and forwards a message to them.
309+
- `IMessageHandlingService` gets a message and checks if there are any message handlers in a combined collection of `IMessageHandler`, `IAsyncMessageHandler`, `INonCyclicMessageHandler` and `IAsyncNonCyclicMessageHandler` instances and forwards a message to them.
310310
- All subscribed message handlers (`IMessageHandler`, `IAsyncMessageHandler`, `INonCyclicMessageHandler`, `IAsyncNonCyclicMessageHandler`) process the given message in a given or a default order.
311311
- `IMessageHandlingService` acknowledges the message by its `DeliveryTag`.
312312
- 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 `"re-queue-attempts"` to the message and sends it again with delay in value of `RequeueTimeoutMilliseconds` (default is 200 milliseconds). The number of attempts is configurable and re-delivery will be made that many times as the value of `RequeueAttempts` property. Mechanism of sending delayed messages covered in the message production [documentation](message-production.md).
@@ -360,6 +360,31 @@ services.AddBatchMessageHandler<CustomBatchMessageHandler>(Configuration.GetSect
360360
The message handler will create a separate connection and use it for reading messages.
361361
When the message collection is full to the size of `PrefetchCount` it will be passed to the `HandleMessage` method.
362362

363+
### Parsing extensions
364+
365+
There are some simple extensions for `BasicDeliverEventArgs` class that helps to parse messages. You have to use `RabbitMQ.Client.Core.DependencyInjection` namespace to enable those extensions.
366+
There is an example of using those extensions inside a `Handle` method of `IMessageHandler`.
367+
368+
```c#
369+
public class CustomMessageHandler : IMessageHandler
370+
{
371+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
372+
{
373+
// You can get string message.
374+
var stringifiedMessage = eventArgs.GetMessage();
375+
376+
// Or object payload.
377+
var payload = eventArgs.GetPayload<YourClass>();
378+
379+
// Or anonymous object by another example object.
380+
var anonymousObject = new { message = string.Empty, number = 0 };
381+
var anonymousPayload = eventArgs.GetAnonymousPayload(anonymousObject);
382+
}
383+
}
384+
```
385+
386+
You can also pass `JsonSerializerSettings` to `GetPayload` or `GetAnonymousPayload` methods as well as collection of `JsonConverter` in case you use custom serialization.
387+
363388
For message production features see the [Previous page](message-production.md)
364389

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

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public class CustomMessageHandler : IMessageHandler
133133
_logger = logger;
134134
}
135135

136-
public void Handle(string message, string routingKey)
136+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
137137
{
138138
// Do whatever you want with the message.
139139
_logger.LogInformation("Hello world");
@@ -152,7 +152,7 @@ public class CustomAsyncMessageHandler : IAsyncMessageHandler
152152
_logger = logger;
153153
}
154154

155-
public async Task Handle(string message, string routingKey)
155+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute)
156156
{
157157
// await something.
158158
}
@@ -172,7 +172,7 @@ public class MyNonCyclicMessageHandler : INonCyclicMessageHandler
172172
_logger = logger;
173173
}
174174

175-
public void Handle(string message, string routingKey, IQueueService queueService)
175+
public void Handle(BasicDeliverEventArgs eventArgs, string matchingRoute, IQueueService queueService)
176176
{
177177
// Send anything you want using IQueueService instance.
178178
var anotherMessage = new MyMessage { Foo = "Bar" };
@@ -193,7 +193,7 @@ public class MyAsyncNonCyclicMessageHandler : IAsyncNonCyclicMessageHandler
193193
_logger = logger;
194194
}
195195

196-
public async Task Handle(string message, string routingKey, IQueueService queueService)
196+
public async Task Handle(BasicDeliverEventArgs eventArgs, string matchingRoute, IQueueService queueService)
197197
{
198198
// Do async stuff.
199199
var anotherMessage = new MyMessage { Foo = "Bar" };

0 commit comments

Comments
 (0)