第三章-高级特性4:消费端限流

2019-03-27  本文已影响0人  asfafjwefhfuer

什么是消费端的限流?


 /**
 * @param prefetchSize  消息大小限制 一般为0 不限制
 * @param prefetchCount 一次最多处理消息个数 
 *        会告诉RabbitMQ不要同时给一个消费者推送多余N个消息,即一旦有N个消息还没有ack,则该consumer将block掉。直到有消息ack
 * @param global       true/false 是否将上面设置应用于chammel。
  *                                简单说。就是上面限制是channel级别还是consumer级别。
 */
void basicQos(int prefetchSize, int prefetchCount, boolean global)


/**
 * @author yangHX
 * createTime  2019/3/27 22:02
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = RabbitMqUtil.getConnectionFactory();
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        String exchangeName = "test_qos_exchange";
        String routingKey = "qos.save";

        String msg = "Hello RabbitMQ QOS Message";
        for (int i = 0; i < 5; i++) {
            channel.basicPublish(exchangeName, routingKey, true, null, msg.getBytes());
        }
    }
}



/**
 * @author yangHX
 * createTime  2019/3/27 22:06
 */
public class Consumer {


    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = RabbitMqUtil.getConnectionFactory();
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        String exchangeName = "test_qos_exchange";
        String queueName = "test_qos_queue";
        String routingKey = "qos.#";

        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);


        //限流方式 第一件事就是将 autoAck 设置为false

        /**
         * @param prefetchSize  消息大小限制 一般为0 不限制
         * @param prefetchCount 一次最多处理消息个数
         *        会告诉RabbitMQ不要同时给一个消费者推送多余N个消息,即一旦有N个消息还没有ack,则该consumer将block掉。直到有消息ack
         * @param global       true/false 是否将上面设置应用于channel。
         *                                简单说。就是上面限制是channel级别还是consumer级别。
         */
        channel.basicQos(0, 1, false);

        channel.basicConsume(queueName, false, new MyConsumer(channel));


    }
}



/**
 * @author yangHX
 * createTime  2019/3/27 22:12
 */
public class MyConsumer extends DefaultConsumer {
    private Channel channel;


    MyConsumer(Channel channel) {
        super(channel);
        this.channel = channel;
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.err.println("-----------consume message----------");
        System.err.println("consumerTag: " + consumerTag);
        System.err.println("envelope: " + envelope);
        System.err.println("properties: " + properties);
        System.err.println("body: " + new String(body));

        //ack应答
        channel.basicAck(envelope.getDeliveryTag(), false);
    }
}


上一篇 下一篇

猜你喜欢

热点阅读