Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify message sent by rabbitMQ inside consumer

Note: Using RabbitMq via RabbitMQBundle in Symfony2.

My producer sends a message like this :

$message = array(
    'class' => get_class($receiver),
    'id' => $receiver->getId(),
    'stepNumber' => 1,
    'errorCount' => 0
);

The consumer retrieves the $receiver from the database and sends him an email.

public function execute(AMQPMessage $msg)
{
    //Step1 - retrieve user from db

    //Step2 - send email

    //Step3 - update stuff in database
}

To keep track of the errors, I want to handle the exceptions at each step. If there is an exception thrown at step 3, I want to modify stepNumber to 3, increase the errorCount by 1 in $msg, and finally requeue the $msg by returning false.

This has the following advantages:

  • When the consumer will process the message again, it will not send the email again.
  • When the errorCount > 5, I just discard the message.. return false.

This would be great, but :

Is there a way to modify the $msg before it is requeued by RabbitMQ?

like image 667
Mick Avatar asked Feb 20 '13 07:02

Mick


1 Answers

As this answer points out, RabbitMQ doesn't let you change messages after publishing them. When you return false, RabbitMQ just puts the original message back in the queue for processing.

It is possible to get the same effect by republishing the message with the changes you want, then consuming the original message by returning true. You will probably want to republish the message using the default (nameless) exchange so that you can send it directly to the queue that you got the original message from.

like image 75
Brian Zell Avatar answered Oct 25 '22 19:10

Brian Zell