How to retract a message in RabbitMQ?

后端 未结 4 1324
旧巷少年郎
旧巷少年郎 2021-02-04 05:26

I\'ve got something like a job queue over RabbitMQ and, upon a request to cancel a job, I\'d like to retract the tasks that have not yet started processing (their messages have

4条回答
  •  执笔经年
    2021-02-04 06:06

    You need to subscribe to all the queues to which messages have been routed, and consume them with ack.

    For instance if you publish to a topic exchange with "test" as the routing key, and there are 3 persistent queues which subscribe to "test" you would need to consume those three queues. It might be better to add another queue which your consumer processes would also listen too, and tell them to ignore those messages.

    An alternative, since you are using RabbitMQ, is to write a custom exchange plugin that will accept some out of band instruction to clear all queues. For instance you might have that exchange read a special message header that tells it to clear all queues to which this message is destined. This does require writing Erlang code, but there are 4 different exchange types implemented so you would only need to copy the most similar one and write the code for the new bahaviours. If you only use custom headers for this, then the body of the message can be a normal message for the consumers.

    To sum up:

    1) the publisher needs to consume the messages itself 2) the publisher can send a special message in a special queue to tell consumers to ignore the message 3) the publisher can send a special message to a custom exchange that will clear any existing messages from the queues before sending this special message to consumers.

提交回复
热议问题