假如:RabbitMQ服务器上有上万条未消费的消息,我们随便打开一个消费者客户端,会出现如下情况:
巨量的消息推送过来,单个客户端无法同时处理这么多数据。导致服务器挂掉。
RabbitMQ 提供了一种qos(服务质量保证)功能,即咋非自动确认消息的前提下,如果一定数目的消息(通过基于consume或者channel设置qos的值)未被确认前,不进行新的消费。( 不能设置自动签收 autoAck为false )
关键方法:
void BasicQos(unit prefetchSize, ushort prefetchCont, bool global): 在消费端体现
- prefetchSize:消息限制,为0不限制
- prefetchCont:一次消费多少条消息。会告诉mq不要给一个消费者同时推送多多于n条消息, 即一旦有n个消息还没有ack,则该consumer将block掉,直到有消息ack。
- global:是否将上面的设置应用于channel。true在channel上限制, false在consumer限制。
prefetchCont 和 global 为channel时 rabbitmq没有实现。 自动签收上边两个不生效。
生产者代码:
package com.example.rabbitmq.limit;
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_qos_exchange";
String routingKey = "qos.save";
//5, 发送消息
String msg = "hello rabbitmq send retuen message!";
for (int i = 0; i < 5; i++) {
channel.basicPublish(exchangeName, routingKey, true,null, msg.getBytes());
}
// channel.close();
// connection.close();
}
}
消费者代码:
package com.example.rabbitmq.limit;
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_qos_exchange";
String routingKey = "qos.#";
String queueName = "test_qos_queue";
//声明一个交换机
channel.exchangeDeclare(exchangeName, "topic", true);
//声明一个队列
channel.queueDeclare(queueName, true, false, false, null);
//建立关系
channel.queueBind(queueName, exchangeName, routingKey);
//5,创建消费者
//限流方式, autoAck 必须为fale
//三个参数 prefetchSize, prefetchCount, global
channel.basicQos(0, 1, false);
channel.basicConsume(queueName, false, new MyConsumer(channel));
}
}
package com.example.rabbitmq.limit;
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("consumerTag = " + consumerTag);
System.out.println("envelope = " + envelope);
System.out.println("basicProperties = " + basicProperties);
System.out.println("body = " + new String(body));
//手动签收
channel.basicAck(envelope.getDeliveryTag(), false);
}
}