消费端的ACK与NACK
- ack:成功处理
- nack:处理失败
消费端因为业务异常,我们可以进行日志的记录,然后进行补偿。
由于服务器宕机等严重问题,那我们就需要手工ACK,保障消费端消费成功。
消费端重回队列:
对没有处理成功的消息,会把消息重新递给Broker。
一般不使用重回队列。
生产者代码:
package com.example.rabbitmq.limit.ack;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* 生产者
*/
public class producer {
public static void main(String[] args) throws IOException, TimeoutException {
//1,创建工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//2, 获取连接
Connection connection = connectionFactory.newConnection();
//3, 通过connection创建一个channel
Channel channel = connection.createChannel();
String exchangeName = "test_ack_exchange";
String routingKey = "ack.save";
//5, 发送消息
for (int i = 0; i < 10; i++) {
Map<String, Object> headers = new HashMap<>();
headers.put("num", i);
String msg = "hello rabbitmq send retuen message! - " + i;
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.deliveryMode(2)
.contentEncoding("utf-8")
.headers(headers)
.build();
channel.basicPublish(exchangeName, routingKey, true,properties, msg.getBytes());
}
// channel.close();
// connection.close();
}
}
消费者代码:
package com.example.rabbitmq.limit.ack;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* 消费者
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
//1,创建工厂
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//2,获取连接
Connection connection = connectionFactory.newConnection();
//3,通过connection创建一个channel
Channel channel = connection.createChannel();
//4,exchange, routingkey, queue, 并将queue和交换机绑定
//设置交换机
String exchangeName = "test_ack_exchange";
String routingKey = "ack.#";
String queueName = "test_ack_queue";
//声明一个交换机
channel.exchangeDeclare(exchangeName, "topic", true, false, null);
//声明一个队列
channel.queueDeclare(queueName, true, false, false, null);
//建立关系
channel.queueBind(queueName, exchangeName, routingKey);
//5,创建消费者
//限流方式, autoAck 必须为fale
//prefetchSize, prefetchCount, global
channel.basicQos(0, 3, false);
channel.basicConsume(queueName, false, new MyConsumer(channel));
}
}
自定义消费者类:
package com.example.rabbitmq.limit.ack;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
/**
* 自定义消费者
*/
public class MyConsumer extends DefaultConsumer {
private Channel channel;
public MyConsumer(Channel channel) {
super(channel);
this.channel = channel;
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] body) throws IOException {
System.out.println("----- consumer message -----");
System.out.println("body = " + new String(body));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//channel.basicAck(envelope.getDeliveryTag(), false);
if((Integer)basicProperties.getHeaders().get("num") == 3){
//multiple 是否批量, requeue 是否重回队列
channel.basicNack(envelope.getDeliveryTag(), false,true);
}else{
//手动签收
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
}