一文搞懂RabbitMQ的ack与nack
JavaEdge
共 1825字,需浏览 4分钟
·
2021-04-12 23:48
点击上方“JavaEdge”,关注公众号
connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare("队列名", true, false, false, null);
//第二个参数设为true为自动应答,false为手动ack
channel.basicConsume("队列名", true, new DefaultConsumer(channel){
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try {
Thread.sleep(10000);
System.out.println(new String(body, "UTF-8"));
//模拟异常
int i = 1/0;
//手动ack
//channel.basicAck(envelope.getDeliveryTag(), false);
} catch (Exception e) {
//重新放入队列
//channel.basicNack(envelope.getDeliveryTag(), false, true);
//抛弃此条消息
//channel.basicNack(envelope.getDeliveryTag(), false, false);
e.printStackTrace();
}finally {
}
}
});
ack机制为自动
手动ack应答(channel.basicAck方法)
这条消息重新放回队列,重新消费
抛弃此条消息
目前交流群已有 800+人,旨在促进技术交流,可关注公众号添加笔者微信邀请进群
喜欢文章,点个“在看、点赞、分享”素质三连支持一下~
评论