Delayed message in RabbitMQ

后端 未结 8 1251
别跟我提以往
别跟我提以往 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:43

    Thanks to Norman's answer, I could implement it in Node.js.

    Everything is pretty clear from the code.

    var ch = channel;
    ch.assertExchange("my_intermediate_exchange", 'fanout', {durable: false});
    ch.assertExchange("my_final_delayed_exchange", 'fanout', {durable: false});
    
    // setup intermediate queue which will never be listened.
    // all messages are TTLed so when they are "dead", they come to another exchange
    ch.assertQueue("my_intermediate_queue", {
          deadLetterExchange: "my_final_delayed_exchange",
          messageTtl: 5000, // 5sec
    }, function (err, q) {
          ch.bindQueue(q.queue, "my_intermediate_exchange", '');
    });
    
    ch.assertQueue("my_final_delayed_queue", {}, function (err, q) {
          ch.bindQueue(q.queue, "my_final_delayed_exchange", '');
    
          ch.consume(q.queue, function (msg) {
              console.log("delayed - [x] %s", msg.content.toString());
          }, {noAck: true});
    });
    

提交回复
热议问题