DEV Community

Cover image for Automated dead-letter queue handling for EasyNetQ [RabbitMQ]
Matt Ghafouri
Matt Ghafouri

Posted on

Automated dead-letter queue handling for EasyNetQ [RabbitMQ]

📃What is EasyNetQ?
EasyNetQ is designed to make publishing and subscribing with RabbitMQ as easy as possible. of course, you can always use the RabbitMQ client to do this, but it brings lots of complexity of maintenance cumbersome to your application.
Digging into this library is out of the scope of this article, you can read more about this incredibly simple library Here.


📃What is the dead-letter queue?

Dead-letter rabbitmq and easyNetQ
Messages from a queue can be "dead-lettered"; that is, republished to an exchange when any of the following events occur:

  • The message is negatively acknowledged by a consumer using basic.reject or basic.nack with requeue parameter set to false.

  • The message expires due to per-message TTL

  • The message is dropped because its queue exceeded a length limit.


🔔EasyNetQ weakness!!
Honestly. it's not a weakness, EasyNetQ tries to keep the implementation as simple as possible, because of this principle you cannot find so many features which already exist in the RabbitMQ client in this library.

But one of the biggest issues that a developer perhaps faces during the implementation of RabbitMQ functionality is dead-letter management.

EasyNetQ does not support automatic dead-letter definition. It means in case any of the conditions are mentioned, the failed message will be moved to the default error queue of RabbitMQ.


❓What's wrong with one Error queue?
Good question! dealing with different types of failed messages and monitoring the failed messages for different queues is not possible in this case. It means there should be a different dead-letter queue for each discrete queue.


🌱Solution

EasyDeadLetter
Here, I have implemented a Nuget package (EasyDeadLetter) for this purpose, which can be easily implemented with the minimum changes in any project.

All you need to do is follow the four steps :
1- First of all, Decorate your class object with QeueuAttribute

2- The second step is to define your dead-letter queue with the same QueueAttribute and also inherit the dead-letter object from the Main object class.

3- Now, it's time to decorate your main queue object with the EasyDeadLetter attribute and set the type of dead-letter queue.

4- In the final step, you need to register EasyDeadLetterStrategy as the default error handler (IConsumerErrorStrategy).

That's all. from now on any failed message will be moved to the related dead-letter queue.


📑Resources
NuGet Package

GitHub logo MattGhafouri / EasyDeadLetter

RabbitMQ automatic DeadLetter handler for dotnet | EasyNetQ

🌱Easy Dead-Letter 🌱

📕Handling dead-letter queues in EasyNetQ (RabbitMQ)

Messages from a queue can be "dead-lettered"; that is, republished to an exchange when any of the following events occur:

  • The message is negatively acknowledged by a consumer using basic.reject or basic.nack with requeue parameter set to false.
  • The message expires due to per-message TTL;
  • The message is dropped because its queue exceeded a length limit

🔔 Problem

If you are using RabbitMQ client for dotnet there is no problem, you can define dead-letter queues during a queue declaration, But using RabbitMQ client brings lots of implementation complexity to the application, because of that so many developers prefer to use the EasyNetQ library which is a wrapper on RabbitMQ client(Amazing and simple) There is no implementation for dead-letter queues in EasyNetQ to keep the library easy to use, It means in case of any exception in the message consumers,…

🤞If you want to support me, please share this article with your community.

Follow me on Linkedin

Top comments (3)

Collapse
 
luizcarlosfaria profile image
Luiz Carlos Faria

Ack the original message and calling BasicPublish is a bad choice to do that.

It's wrong, it's weird.

RabbitMQ has a native and safe Deadletter mechanism. Use it!

By the way, leastwise use same IBasicProperties from original message, your code causes data lost, ignoring things like headers, and other properties like Type, ReplyTo, AppId, UserId, CorrelationId, Priority....

Collapse
 
mattqafouri profile image
Matt Ghafouri

Thanks for the detailed comment.
When you use EasyNetQ, you have to follow the queue creation convention, and there is no dead-letter in this library.
But, about the basic properties, I will check it again. Thanks again.

Collapse
 
mattqafouri profile image
Matt Ghafouri

Thanks for the feedback, I cloned the BasicProperties from the original message.