Mick
Mick

Reputation: 31919

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:

This would be great, but :

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

Upvotes: 6

Views: 7605

Answers (1)

Brian Zell
Brian Zell

Reputation: 1166

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.

Upvotes: 5

Related Questions