qos其实是一种优化版的批量确认,不需要自己写业务代码,由rabbitmq进行封装,提供给我们使用
确认是为了删除消息,如果队列设置成持久化,删除消息时,即要删除内存中的消息,也要删除磁盘中的消息,效率会比非持久化的低
1.创建生产者QosProducer
package com.rabbit.fisher.qos;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
*发送21条消息,其中第21条表示本批次消息的结束
*/
public class QosProducer
{
public final static String EXCHANGE_NAME="direct_logs";
public static void main( String[] args ) throws IOException, TimeoutException {
//创建链接、连接到MQ
ConnectionFactory connectionFactory = new ConnectionFactory();
//设置连接工厂的连接地址(默认端口5672)
connectionFactory.setHost("192.168.42.111");
//设置虚拟主机
connectionFactory.setVirtualHost("fisher");
//设置用户名
connectionFactory.setUsername("fisher");
//设置密码
connectionFactory.setPassword("123456");
//创建连接
Connection connection = connectionFactory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//在信道中设置交换器
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
//在消费者中申明队列
//发送21条消息
for (int i = 0; i < 21; i++) {
String msg = "Hello World" + (i+1);
if (i == 20) {
msg = "stop";
}
//发布消息(设置交换器、路由键、参数、消息内容)
channel.basicPublish(EXCHANGE_NAME, "qos", null, msg.getBytes());
System.out.println("Send:" +"qos" + ";" + msg);
}
//关闭信道
channel.close();
//关闭连接
connection.close();
}
}
2.创建qos消费者QosConsumer,使用channel.basicAck(envelope.getDeliveryTag(), false);方法进行手动确认,false表示单条确认,使用 channel.basicQos(15, true);方法进行qos预取,true表示15条的限制是加在信道上,false表示15条的限制是加在消费者上
package com.rabbit.fisher.qos;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
*qos预取单条确认
*/
public class QosConsumer
{
public static void main( String[] args ) throws IOException, TimeoutException {
//创建链接、连接到MQ
ConnectionFactory connectionFactory = new ConnectionFactory();
//设置连接工厂的连接地址(默认端口5672)
connectionFactory.setHost("192.168.42.111");
//设置虚拟主机
connectionFactory.setVirtualHost("fisher");
//设置用户名
connectionFactory.setUsername("fisher");
//设置密码
connectionFactory.setPassword("123456");
//创建连接
Connection connection = connectionFactory.newConnection();
//创建信道
final Channel channel = connection.createChannel();
//在信道中设置交换器(这里选择直接交换器direct)
channel.exchangeDeclare(QosProducer.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
//申明队列
String queueName = "focuserror";
channel.queueDeclare(queueName, false, false, false, null);
//绑定:将队列与交换器通过路由键绑定
String routeKey = "qos";
channel.queueBind(queueName, QosProducer.EXCHANGE_NAME, routeKey);
System.out.println("waiting for message ......");
//申明一个消费者
final Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("Receive[" + envelope.getRoutingKey() + "]" + message);
//单条确认(false),批量确认(true)
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
//qos预取(先取出15条,然后再取出6),预取15条之后,再根据自己的需要,决定是单次确认,还是批量确认,此处使用单次确认
channel.basicQos(15, true);
//因为已经设置单条确认,所以此处用false,表示不需要自动确认
channel.basicConsume(queueName, false, consumer);
}
}
3.先启动消费者
4.再启动生产者
Send:qos;Hello World1
Send:qos;Hello World2
Send:qos;Hello World3
Send:qos;Hello World4
Send:qos;Hello World5
Send:qos;Hello World6
Send:qos;Hello World7
Send:qos;Hello World8
Send:qos;Hello World9
Send:qos;Hello World10
Send:qos;Hello World11
Send:qos;Hello World12
Send:qos;Hello World13
Send:qos;Hello World14
Send:qos;Hello World15
Send:qos;Hello World16
Send:qos;Hello World17
Send:qos;Hello World18
Send:qos;Hello World19
Send:qos;Hello World20
Send:qos;stop
5.查看消费者打印
waiting for message ......
Receive[qos]Hello World1
Receive[qos]Hello World2
Receive[qos]Hello World3
Receive[qos]Hello World4
Receive[qos]Hello World5
Receive[qos]Hello World6
Receive[qos]Hello World7
Receive[qos]Hello World8
Receive[qos]Hello World9
Receive[qos]Hello World10
Receive[qos]Hello World11
Receive[qos]Hello World12
Receive[qos]Hello World13
Receive[qos]Hello World14
Receive[qos]Hello World15
Receive[qos]Hello World16
Receive[qos]Hello World17
Receive[qos]Hello World18
Receive[qos]Hello World19
Receive[qos]Hello World20
Receive[qos]stop
6.重启消费者,消息已确认,队列中不存在消息
如果注释掉手动确认方法channel.basicAck(envelope.getDeliveryTag(), false);则消息不会被清除,依然存在与队列中,会发生消息重复消费的情况
7.创建批量消费者QosBatchConsumer,每5条确认一次,channel.basicAck(envelope.getDeliveryTag(), true);true表示批量
package com.rabbit.fisher.qos;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
*qos预取批量确认
*/
public class QosBatchConsumer
{
public static void main( String[] args ) throws IOException, TimeoutException {
//创建链接、连接到MQ
ConnectionFactory connectionFactory = new ConnectionFactory();
//设置连接工厂的连接地址(默认端口5672)
connectionFactory.setHost("192.168.42.111");
//设置虚拟主机
connectionFactory.setVirtualHost("fisher");
//设置用户名
connectionFactory.setUsername("fisher");
//设置密码
connectionFactory.setPassword("123456");
//创建连接
Connection connection = connectionFactory.newConnection();
//创建信道
final Channel channel = connection.createChannel();
//在信道中设置交换器(这里选择直接交换器direct)
channel.exchangeDeclare(QosProducer.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
//申明队列
String queueName = channel.queueDeclare().getQueue();
//绑定:将队列与交换器通过路由键绑定
String routeKey = "qos";
channel.queueBind(queueName, QosProducer.EXCHANGE_NAME, routeKey);
System.out.println("waiting for message ......");
//申明一个消费者
final Consumer consumer = new DefaultConsumer(channel){
//计数,第多少条
private int messageCount =0;
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("批量确认Receive[" + envelope.getRoutingKey() + "]" + message);
messageCount ++;
//批量确认5条一批
if (messageCount % 5 == 0) {
channel.basicAck(envelope.getDeliveryTag(), true);
System.out.println("批量消费者批量确认,第"+messageCount+"条");
}
if (message.equals("stop")) {
channel.basicAck(envelope.getDeliveryTag(), true);
System.out.println("批量消费者最后一批消息确认,第"+messageCount+"条");
}
}
};
//因为已经设置手动确认,所以此处用false,表示不需要自动确认
channel.basicConsume(queueName, false, consumer);
}
}
8.先启动消费者
9.再启动生产者
10.查看消费者打印
waiting for message ......
批量确认Receive[qos]Hello World1
批量确认Receive[qos]Hello World2
批量确认Receive[qos]Hello World3
批量确认Receive[qos]Hello World4
批量确认Receive[qos]Hello World5
批量消费者批量确认,第5条
批量确认Receive[qos]Hello World6
批量确认Receive[qos]Hello World7
批量确认Receive[qos]Hello World8
批量确认Receive[qos]Hello World9
批量确认Receive[qos]Hello World10
批量消费者批量确认,第10条
批量确认Receive[qos]Hello World11
批量确认Receive[qos]Hello World12
批量确认Receive[qos]Hello World13
批量确认Receive[qos]Hello World14
批量确认Receive[qos]Hello World15
批量消费者批量确认,第15条
批量确认Receive[qos]Hello World16
批量确认Receive[qos]Hello World17
批量确认Receive[qos]Hello World18
批量确认Receive[qos]Hello World19
批量确认Receive[qos]Hello World20
批量消费者批量确认,第20条
批量确认Receive[qos]stop
批量消费者最后一批消息确认,第21条