超时死信消息:超时的消息
有两种超时消息:
消息所在的队列设置了超时时间
消息本身设置了超时时间

消息所在的队列设置了超时时间:所有在此队列的消息超过了设定时间都会成为死信

设置队列超时时间:

  1. @Bean
  2. public Queue ttlQueue(){
  3. return QueueBuilder.durable("ttl.queue") // 指定队列名称,并持久化
  4. .ttl(10000) // 设置队列的超时时间,10秒
  5. .deadLetterExchange("dl.ttl.exchange") // 指定死信交换机
  6. .build();
  7. }

消息本身设置超时时间:

  1. @Test
  2. public void testSendTTLMessage() throws InterruptedException {
  3. // 1.消息体
  4. // String msg = "超时消息...";
  5. Message msg = MessageBuilder
  6. .withBody("hello, ttl message".getBytes(StandardCharsets.UTF_8))
  7. .setExpiration("5000")
  8. .build();
  9. // 2.发送消息
  10. rabbitTemplate.convertAndSend("ttl.direct","ttl", msg);
  11. log.info("发送消息成功...");
  12. }
  13. }