rabbitmq 手动 ack
rabbitmq 手动 ack
@RabbitListener(bindings = @QueueBinding(
exchange = @Exchange(value = "ex.retry.manual"),
value = @Queue(value = "q.retry.manual", durable = "true"),
key = "manual"), ackMode = "MANUAL")
public void manualRetry(String message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException {
int i = counter.incrementAndGet();
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()) + ": " + message + " counter: " + i);
doErrorManual(2, deliveryTag, channel);
}
private void doErrorManual(int type, long deliveryTag, Channel channel) throws IOException {
if (type == 1) {
try {
if (1 == 1) {
throw new NullPointerException("异常啦");
}
} catch (NullPointerException e) {
log.error("catch error: ", e);
}
} else if (type == 2) {
try {
if (1 == 1) {
throw new NullPointerException("异常啦");
}
} catch (NullPointerException e) {
log.error("catch error: ", e);
throw e;
}
} else {
if (1 == 1) {
throw new NullPointerException("异常啦");
}
}
}
以上 这种配置 ,开启 ack 模式为 MANUAL
手动确认方式
如果消费异常,消息不会重新入队列,此时消费的状态为 Unacked , 但是,服务重启之后,消费会被重新发送
但是如果 channel.basicReject(deliveryTag, true)
, 则消息会再次入队,mq会重新发送消息,此时消息的状态是 redy