【问题标题】:How do I send a message directly to a parking lot queue, prevent requeue and exit the program flow?如何将消息直接发送到停车场队列,防止重新排队并退出程序流程?
【发布时间】:2020-02-14 22:47:46
【问题描述】:

我目前有 4 个队列:

  • 测试队列
  • 测试队列短期死信
  • 测试队列长期死信
  • 测试队列停车场

当邮件进入test-queue 时,我会检查邮件的格式是否正确。如果不是我想将消息直接发送到停车场队列

我不能使用AmqpRejectAndDontRequeue(),因为它会自动将消息发送到配置的DLQ(test-queue-short-term-dead-letter)。

RabbitTemplate.convertAndSend() 与另一个例外(例如BadRequestException)一起使用不起作用。该消息按预期进入停车场队列,但相同的消息将保留在test-queue

在程序继续执行时,单独使用RabbitTemplate.convertAndSend() 将不起作用。

所有队列都绑定到一个直接交换,每个队列都有唯一的路由键。 test-queue 配置有以下参数:

  • x-dead-letter-exchange: ""
  • x-dead-letter-routing-key: <shortTermDeadLetterKey>

接收者:

  @RabbitListener(queues = "test-queue")
  public void receiveMessage(byte[] person) {
    String personString = new String(person);

    if (!personString.matches(desiredRegex)) {
      rabbitTemplate.convertAndSend("test-exchange", "test-queue-parking-lot",
          "invalid person");
      log.info("Invalid person");
    }
    ...some other code which I dont want to run as the message has arrived in the incorrect format
}

【问题讨论】:

    标签: java rabbitmq message-queue spring-amqp


    【解决方案1】:

    通过手动确认消息并从方法返回解决了问题。

      @RabbitListener(queues = "test-queue")
      public void receiveMessage(byte[] person, Channel channel,
      @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception) {
        String personString = new String(person);
    
        if (!personString.matches(desiredRegex)) {
          rabbitTemplate.convertAndSend("test-exchange", "test-queue-parking-lot",
              "invalid person");
          log.info("Invalid person");
          channel.basicAck(tag, false);
          return;
        }
        ...some other code which I dont want to run as the message has arrived in the incorrect format
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 2012-02-12
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 2011-07-31
      • 2018-02-18
      • 2012-05-20
      • 2016-04-16
      相关资源
      最近更新 更多