理解消息的确认机制
- 消息的确认,值指生产者投递消息后,如果Broker收到消息,则会给我们生产者一个应答
- 生产者接收应答,用来确定这条消息是否正常发送到Broker,这种方式也是消息可靠性投递的核心保障
确认机制流程图
Confirm 确认消息实现
- 第一步:在channel上开启确认模式:channel.confirmSelect()
- 第二步:在channel上添加监听:addConfirmListener,监听成功和失败的返回结果,根据具体的消息进行重新发送、或记录日志等后续处理!
代码编写
生产端代码
package club.yunqiang.rabbitmqapi.confirm;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author zyq
* @date 2019/12/7
*/
public class Producer {
public static void main(String[] args) throws IOException, TimeoutException {
// 1、创建ConnectionFactory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
// 2、获取 Connection
Connection connection = connectionFactory.newConnection();
// 3、通过 Connection 获取 Channel
Channel channel = connection.createChannel();
// 4、指定消息的确认模式
channel.confirmSelect();
// 5、投递消息
String exchangeName = "test_confirm_exchange";
String routingKey = "confirm.save";
String msg = "Hello RabbitMQ send confirm message!";
channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
// 6、添加确认监听
channel.addConfirmListener(new ConfirmListener() {
@Override
public void handleAck(long deliveryTag, boolean multiple) throws IOException {
System.out.println("----- ack! -----");
}
@Override
public void handleNack(long deliveryTag, boolean multiple) throws IOException {
System.out.println("----- no ack! -----");
}
});
}
}
消费端代码
package club.yunqiang.rabbitmqapi.confirm;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
/**
* @author zyq
* @date 2019/12/7
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
// 1、创建ConnectionFactory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
// 2、获取 Connection
Connection connection = connectionFactory.newConnection();
// 3、通过 Connection 获取 Channel
Channel channel = connection.createChannel();
// 4、声明 exchange
String exchangeName = "test_confirm_exchange";
String routingKey = "confirm.#";
channel.exchangeDeclare(exchangeName, "topic", true);
// 5、声明 queue
String queueName = "test_confirm_queue";
channel.queueDeclare(queueName, true, false, false, null);
// 6、绑定 exchange 和 queue
channel.queueBind(queueName, exchangeName, routingKey);
// 7、创建消费者
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body, StandardCharsets.UTF_8);
System.out.println(String.format("消费端%s", msg));
}
};
channel.basicConsume(queueName, true, defaultConsumer);
}
}
注意:本文归作者所有,未经作者允许,不得转载