Flow
Flow

Reputation: 402

RabbitMq: Replace duplicated messages

I'm using RabbitMq for submitting data for registered web hooks.

base info: If a contact is created in the system, the message is put in the queue and the consumer is sending later the hook data to the registered url.

To my question: Its possible, that a contact is updated in 5 seconds twice and that both messages are still in the queue. But I'd like, if the second message is queued, that the first message will be removed.

I know that i can't delete the message manually. But is it somehow possible to set an id on the message and if two messages with the same id are in the same queue, that the first one is automatically removed/replaced? That only one request is sent to the url. I know you can set a message id on the message self. But I have nothing found to replace the old one.

My PHP Code (simplified):

    $connection = new AMQPConnection('localhost', 5672, 'test', 'test');
    $channel = $connection->channel();
    $channel->queue_declare(self::QUEUE_NAME, false, true, false, false);

    $data = array(
        'model' => get_class($subject),
        'id' => $subject->getId(),
        'event' => $event->getName()
    );
    $messageProperties = array(
        'message_id' => get_class($subject) . '-' . $subject->getId()
    );
    $channel->basic_publish(new AMQPMessage(json_encode($data), $messageProperties), '', self::QUEUE_NAME);

    $channel->close();
    $connection->close();

Btw i'm using the the php amqplib https://github.com/videlalvaro/php-amqplib.

Thanks for the help Flo

Upvotes: 3

Views: 1476

Answers (2)

Paul Mooney
Paul Mooney

Reputation: 1606

You could potentially tag each message with a unique message ID. A consuming application should keep an optimised list of inbound message IDs that it has already processed (in a thread-safe HashMap (Java), or Dictionary (.NET) implementation.

Should a message arrive, that has already been processed (the message ID is present in the stored list of handled message IDs), it will be ignored (or a polite “please wait” style response should be issued), preserving idempotency.

Upvotes: 0

old_sound
old_sound

Reputation: 2313

RabbitMQ doesn't delete/filter messages that way. You have to do that at the app level, probably using something like a bloom filter.

Upvotes: 1

Related Questions