You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Using exact matches first for better performance
224
227
- Only applying wildcard pattern matching when no exact match is found
225
228
- Caching routing key patterns for efficient lookup
226
229
230
+
##### Unrecognized Routing Key Handler
231
+
232
+
When a message arrives on a queue with a routing key that doesn't match any of the configured consumer routing key patterns, the `MessageUnrecognizedRoutingKeyHandler` is invoked to determine how the message should be handled.
233
+
234
+
**Default Behavior:**
235
+
236
+
By default, unrecognized messages are **acknowledged (Ack)** and removed from the queue:
logger.LogWarning("Unrecognized routing key: {RoutingKey} on exchange: {Exchange}",
273
+
routingKey, transportMessage.Exchange);
274
+
275
+
// Route to DLX if it looks like a potential typo or misconfiguration
276
+
if (routingKey.StartsWith("orders."))
277
+
{
278
+
returnRabbitMqMessageConfirmOptions.Nack; // Send to DLX for investigation
279
+
}
280
+
281
+
// Ack and discard messages from other exchanges
282
+
returnRabbitMqMessageConfirmOptions.Ack;
283
+
};
284
+
});
285
+
});
286
+
```
287
+
288
+
**Available Options:**
289
+
290
+
-**`Ack` (default)**: Acknowledge and remove the message from the queue - use when unrecognized messages should be silently discarded
291
+
-**`Nack`**: Reject the message and route to Dead Letter Exchange (if configured) - use for debugging routing issues or when messages shouldn't be lost
292
+
-**`Nack | Requeue`**: Reject and requeue the message for retry - use when routing keys might be registered dynamically or during rolling deployments
293
+
294
+
**Example with Dead Letter Exchange:**
295
+
296
+
```cs
297
+
services.AddSlimMessageBus(mbb=>
298
+
{
299
+
mbb.WithProviderRabbitMQ(cfg=>
300
+
{
301
+
// Send unrecognized messages to DLX for investigation
The handler receives `BasicDeliverEventArgs` which provides access to:
318
+
319
+
-`RoutingKey`: The routing key of the unrecognized message
320
+
-`Exchange`: The exchange the message was published to
321
+
-`Body`: The message payload (ReadOnlyMemory<byte>)
322
+
-`BasicProperties`: Message properties including headers, MessageId, ContentType, etc.
323
+
324
+
This allows for sophisticated routing decisions based on message metadata.
325
+
326
+
**Common Use Cases:**
327
+
328
+
1.**Development/Debugging**: Use `Nack` to route unrecognized messages to a DLX where they can be inspected
329
+
2.**Production Monitoring**: Log unrecognized routing keys and send metrics to your monitoring system
330
+
3.**Graceful Degradation**: Use `Ack` in production to prevent queue buildup from deprecated message types
331
+
4.**Rolling Deployments**: Use `Nack | Requeue` to temporarily requeue messages during deployments when consumers might not yet be ready
332
+
333
+
**Note:** This handler only applies to messages where the routing key doesn't match any configured consumer patterns. Messages that match a routing key pattern but fail during processing are handled by the [Consumer Error Handling](#consumer-error-handling) mechanism instead.
334
+
227
335
#### Acknowledgment Mode
228
336
229
337
When a consumer processes a message from a RabbitMQ queue, it needs to acknowledge that the message was processed. RabbitMQ supports three types of acknowledgments out which two are available in SMB:
@@ -487,6 +595,7 @@ When a RabbitMQ server restarts or the connection is lost, SlimMessageBus automa
487
595
5. Resumesmessageprocessingautomatically
488
596
489
597
Thisensuresthat:
598
+
490
599
-Messagesdon't pile up in queues during temporary outages
/// Represents the method that handles a RabbitMQ message that includes an routing key that is obsolete or non relevent from the applicatin perspective
36
+
/// Provides access to the message, its properties, and a confirmation action.
37
+
/// </summary>
38
+
/// <remarks>Use this delegate to process messages received from RabbitMQ queues where the routing key is not
39
+
/// relevant or not provided. The handler is responsible for invoking the confirmation action to ensure proper message
40
+
/// acknowledgment.</remarks>
41
+
/// <param name="transportMessage">The event arguments containing the delivered RabbitMQ message and related metadata.</param>
@@ -164,6 +167,8 @@ protected override async Task<Exception> OnMessageReceived(Dictionary<string, ob
164
167
else
165
168
{
166
169
Logger.LogDebug("Exchange {Exchange} - Queue {Queue}: No message processor found for routing key {RoutingKey}",transportMessage.Exchange,Path,transportMessage.RoutingKey);
0 commit comments