简体   繁体   English

延迟处理死信队列

[英]Handling dead letter queue with delay

I want to do the following: when a message fails and falls to my dead letter queue, I want to wait 5 minutes and republishes the same message on my queue.我想执行以下操作:当消息失败并落入我的死信队列时,我想等待 5 分钟并在我的队列中重新发布相同的消息。

Today, using Spring Cloud Streams and RabbitMQ, I did the following code Based on this documentation :今天,使用 Spring Cloud Streams 和 RabbitMQ,我根据这个文档做了以下代码:

@Component
public class HandlerDlq {

    private static final Logger LOGGER = LoggerFactory.getLogger(HandlerDlq.class);
    private static final String X_RETRIES_HEADER = "x-retries";
    private static final String X_DELAY_HEADER = "x-delay";
    private static final int NUMBER_OF_RETRIES = 3;
    private static final int DELAY_MS = 300000;
    private RabbitTemplate rabbitTemplate;

    @Autowired
    public HandlerDlq(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @RabbitListener(queues = MessageInputProcessor.DLQ)
    public void rePublish(Message failedMessage) {
        Map<String, Object> headers = failedMessage.getMessageProperties().getHeaders();
        Integer  retriesHeader = (Integer) headers.get(X_RETRIES_HEADER);
        if (retriesHeader == null) {
            retriesHeader = 0;
        }
        if (retriesHeader > NUMBER_OF_RETRIES) {
            LOGGER.warn("Message {} added to failed messages queue", failedMessage);
            this.rabbitTemplate.send(MessageInputProcessor.FAILED, failedMessage);
            throw new ImmediateAcknowledgeAmqpException("Message failed after " + NUMBER_OF_RETRIES + " attempts");
        }
        retriesHeader++;
        headers.put(X_RETRIES_HEADER, retriesHeader);
        headers.put(X_DELAY_HEADER, DELAY_MS * retriesHeader);
        LOGGER.warn("Retrying message, {} attempts", retriesHeader);
        this.rabbitTemplate.send(MessageInputProcessor.DELAY_EXCHANGE, MessageInputProcessor.INPUT_DESTINATION, failedMessage);
    }

    @Bean
    public DirectExchange delayExchange() {
        DirectExchange exchange = new DirectExchange(MessageInputProcessor.DELAY_EXCHANGE);
        exchange.setDelayed(true);
        return exchange;
    }

    @Bean
    public Binding bindOriginalToDelay() {
        return BindingBuilder.bind(new Queue(MessageInputProcessor.INPUT_DESTINATION)).to(delayExchange()).with(MessageInputProcessor.INPUT_DESTINATION);
    }

    @Bean
    public Queue parkingLot() {
        return new Queue(MessageInputProcessor.FAILED);
    }
}

My MessageInputProcessor interface:我的MessageInputProcessor接口:

public interface MessageInputProcessor {

    String INPUT = "myInput";

    String INPUT_DESTINATION = "myInput.group";

    String DLQ = INPUT_DESTINATION + ".dlq"; //from application.properties file

    String FAILED = INPUT + "-failed";

    String DELAY_EXCHANGE = INPUT_DESTINATION + "-DlqReRouter";

    @Input
    SubscribableChannel storageManagerInput();

    @Input(MessageInputProcessor.FAILED)
    SubscribableChannel storageManagerFailed();
}

And my properties file:还有我的属性文件:

#dlx/dlq setup - retry dead letter 5 minutes later (300000ms later)
spring.cloud.stream.rabbit.bindings.myInput.consumer.auto-bind-dlq=true
spring.cloud.stream.rabbit.bindings.myInput.consumer.republish-to-dlq=true
spring.cloud.stream.rabbit.bindings.myInput.consumer.dlq-ttl=3000
spring.cloud.stream.rabbit.bindings.myInput.consumer.delayedExchange=true


#input
spring.cloud.stream.bindings.myInput.destination=myInput
spring.cloud.stream.bindings.myInput.group=group

With this code, I can read from dead letter queue, capture the header but I can't put it back to my queue (the line LOGGER.warn("Retrying message, {} attempts", retriesHeader); only runs once, even if I put a very slow time).使用此代码,我可以从死信队列中读取,捕获标头,但无法将其放回我的队列( LOGGER.warn("Retrying message, {} attempts", retriesHeader);仅运行一次,即使如果我把时间放得很慢)。

My guess is that the method bindOriginalToDelay is binding the exchange to a new queue, and not mine.我的猜测是方法bindOriginalToDelay将交换绑定到一个新队列,而不是我的。 However, I didn't find a way to get my queue to bind there instead of creating a new one.但是,我没有找到一种方法让我的队列绑定到那里而不是创建一个新队列。 But I'm not even sure this is the error.但我什至不确定这是错误。

I've also tried to send to MessageInputProcessor.INPUT instead of MessageInputProcessor.INPUT_DESTINATION , but it didn't work as expected.我也尝试发送到MessageInputProcessor.INPUT而不是MessageInputProcessor.INPUT_DESTINATION ,但它没有按预期工作。

Also, unfortunately, I can't update Spring framework due to dependencies on the project...另外,不幸的是,由于对项目的依赖,我无法更新 Spring 框架......

Could you help me with putting back the failed message on my queue after some time?你能帮我在一段时间后把失败的消息放回我的队列吗? I really didn't want to put a thread.sleep there...我真的不想在那里放一个thread.sleep ...

With that configuration, myInput.group is bound to the delayed (topic) exchange myInput with routing key # .使用该配置, myInput.group绑定到延迟(主题)交换myInput与路由键#

You should probably remove spring.cloud.stream.rabbit.bindings.myInput.consumer.delayedExchange=true because you don't need the main exchange to be delayed.您可能应该删除spring.cloud.stream.rabbit.bindings.myInput.consumer.delayedExchange=true因为您不需要延迟主交换。

It will also be bound to your explicit delayed exchange, with key myInput.group .它也将绑定到您的显式延迟交换,密钥为myInput.group

Everything looks correct to me;在我看来,一切都是正确的; you should see the same (single) queue bound to two exchanges:您应该看到绑定到两个交换机的相同(单个)队列:

在此处输入图片说明

The myInput.group.dlq is bound to DLX with key myInput.group myInput.group.dlq与键myInput.group绑定到DLX

You should set a longer TTL and examine the message in the DLQ to see if something stands out.您应该设置更长的 TTL 并检查 DLQ 中的消息以查看是否有突出显示。

EDIT编辑

I just copied your code with a 5 second delay and it worked fine for me (with turning off the delay on the main exchange).我刚刚复制了您的代码,延迟了 5 秒,对我来说效果很好(关闭了主交换机的延迟)。

Retrying message, 4 attempts

and

added to failed messages queue

Perhaps you thought it was not working because you have a delay on the main exchange too?也许您认为它不起作用是因为您在主交易所也有延迟?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM