3

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

4

2 回答 2

1

RabbitMQ 不会以这种方式删除/过滤消息。您必须在应用程序级别执行此操作,可能使用诸如布隆过滤器之类的东西。

于 2015-01-06T16:43:16.787 回答
0

您可能会使用唯一的消息 ID 标记每条消息。消费应用程序应保留已处理的入站消息 ID 的优化列表(在线程安全HashMap( Java ) 或Dictionary( .NET ) 实现中)。

如果消息到达,并且已经被处理(消息 ID 存在于已处理消息 ID 的存储列表中),它将被忽略(或者应该发出礼貌的“请稍候”样式的响应),保持幂等性。

于 2015-07-23T16:47:06.130 回答