【问题标题】:How to requeue a message to the Back of a Rabbit MQ Queue via Spring如何通过 Spring 将消息重新排队到 Rabbit MQ 队列的后端
【发布时间】:2021-03-03 08:18:13
【问题描述】:

我正在编写一个 SpringBoot RabbitMQ 消费者,我需要偶尔将一条消息重新排队到队列的 BACK

我认为这就是否定确认的工作原理,但是 basicReject(deliveryTag, true) 只是将消息放回队列中尽可能接近其原始位置的位置,在我的一次一个的情况下,它正好回到队列的前面。

我的第一个想法是在某个时间间隔 (similar to the approach mentioned in this answer) 使用死信队列反馈到消息队列,但如果有某种方法可以简单地重新排队到初始队列

我的下面的结构只是简单地消耗了消息并且无法将它重新添加到队列中。

如果没有 DLQ,如何做到这一点?

@ServiceActivator(inputChannel = "amqpInputChannel")
    public void handle(@Payload String message,
                       @Header(AmqpHeaders.CHANNEL) Channel channel,
                       @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag){

    try{

        methodThatThrowsRequeueError();
        methodThatThrowsMoveToErrorQueueError();

    } catch (RequeueError re) {

        channel.basicAck(deliveryTag, false);
        sendMessageToBackOfQueue(message);
        return;

    } catch (MoveToErrorQueueError me) {
        //Structured the same as sendMessageToBackOfQueue, works fine
        moveMessageToErrorQueue(message);
    }
    
    channel.basicAck(deliveryTag, false);
}

private void sendMessageToBackOfQueue(String message) {
        try {
            rabbitTemplate.convertAndSend(
                exchangeName,
                routingKeyRequeueMessage,
                message,
                message -> {
                    message.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
                    return message;
                }
            );
        } catch (AmqpException amqpEx) {
            //error handling which is not triggered...
        }
    }

【问题讨论】:

    标签: java spring-boot rabbitmq


    【解决方案1】:

    TL;DR:我没有发现没有任何中介将消息从侦听服务转发回原始队列的方法。

    有几个选项围绕死信队列/死信交换,但我们发现的非 DLQ/DLX 解决方案是定时交换,如果您愿意,可以使用伪 DLX。本质上:

    消息进入 MessageExchange (MsgX),它传播到服务队列 (SvcQ)。 服务 (Svc) 从 SvcQ 获取消息。

    一旦你确定消息应该发送到SvcQ的后面,Svc应该:

    1. 向 SvcQ 发送确认。
    2. 将消息发送到另一个交易所,即我们的定时伪 DLX
    3. psuedo-DLX 可以配置为在某个时间间隔内向 (BACK OF!!) SvcQ 发布消息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2016-12-13
      • 2013-10-14
      相关资源
      最近更新 更多