RabbitMq有两种消息确认模式:
1)自动确认模式:当消息被发送之后就认为成功传输,这种方式有更高的吞吐量,但降低了消息传输和消费端处理的安全性。
2)手动确认模式:通过API,Channel#basicAck 和 Channel#basicNack分别进行确认和非确认
boolean autoAck = false;
channel.basicConsume(queueName, autoAck, "a-consumer-tag",
new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
throws IOException
{
long deliveryTag = envelope.getDeliveryTag();
// positively acknowledge a single delivery, the message will
// be discarded
channel.basicAck(deliveryTag, false);
}
});