Delayed message in RabbitMQ

后端 未结 8 1229
别跟我提以往
别跟我提以往 2020-12-08 02:31

Is it possible to send message via RabbitMQ with some delay? For example I want to expire client session after 30 minutes, and I send a message which will be processed after

相关标签:
8条回答
  • 2020-12-08 02:54

    With the release of RabbitMQ v2.8, scheduled delivery is now available but as an indirect feature: http://www.javacodegeeks.com/2012/04/rabbitmq-scheduled-message-delivery.html

    0 讨论(0)
  • 2020-12-08 02:55

    There are two approaches you can try:

    Old Approach: Set the TTL(time to live) header in each message/queue(policy) and then introduce a DLQ to handle it. once the ttl expired your messages will move from DLQ to main queue so that your listener can process it.

    Latest Approach: Recently RabbitMQ came up with RabbitMQ Delayed Message Plugin , using which you can achieve the same and this plugin support available since RabbitMQ-3.5.8.

    You can declare an exchange with the type x-delayed-message and then publish messages with the custom header x-delay expressing in milliseconds a delay time for the message. The message will be delivered to the respective queues after x-delay milliseconds

    byte[] messageBodyBytes = "delayed payload".getBytes("UTF-8");
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put("x-delay", 5000);
    AMQP.BasicProperties.Builder props = new 
    AMQP.BasicProperties.Builder().headers(headers);
    channel.basicPublish("my-exchange", "", props.build(), messageBodyBytes);
    

    More here: git

    0 讨论(0)
提交回复
热议问题