【设计】springboot rabbitmq重试达到失败次数 进入死信队列

代码示例:

@Slf4j
@Service
@RabbitListener(queues = {"${spring.events.[event.product.product_price_changed].queue}"}, concurrency = "1", containerFactory = "rabbitListenerContainerFactory")
public class ProductPriceChangeEventHandlerBak {

    @Async
    @RabbitHandler
    @Retryable(include = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 5000, multiplier = 2, maxDelay = 60000))
    public void onProductPriceChange(ProductPriceChangeEvent event, Channel channel, Message message) throws Exception {
        log.info("handle event start: {}", event);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        TimeUnit.SECONDS.sleep(5);
        if (event.getPriceNew().equals(new BigDecimal("808.88"))) {
            throw new CommonException(ErrorCode.INTERNAL_ERROR);
        }
        channel.basicAck(deliveryTag, false);

        log.info("handle event complete: success");
    }

    @Recover
    public void onProductPriceChange(Exception ex, ProductPriceChangeEvent event, Channel channel, Message message) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicNack(deliveryTag, false, false);
        log.error("handle event complete: exception");
    }
}

Original: https://www.cnblogs.com/junneyang/p/16392497.html
Author: junneyang
Title: 【设计】springboot rabbitmq重试达到失败次数 进入死信队列

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/539756/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球