【设计】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");
    }
}

 

posted @ 2022-06-20 10:41  junneyang  阅读(344)  评论(0编辑  收藏  举报