Consumer end limiting policy

scenes to be used

First of all, we have Rabbitmq server unprocessed messages over ten thousand, we just opened a consumer client, there will be the following: 
a huge amount of instant messages all push over, but we have a single client can not handle so much data at the same time! 
Rabbitmq provides a qos (quality of service) function, that is in a non-automatic acknowledgment messages on the premise that if a certain number of messages 
before has not been confirmed, no new news consumption (by value based on the consumer or channel set qos) .

specific method

void BasicQos (Unit prefetchSize, ushort prefetchCount, Boolean Global); 
prefetchSize: size limit of a single message is usually set to 0, meaning that no limitation 
prefetchCount: number of messages, is generally set to a 
global: level messages directed, true: channel level, false: consumer level, usually set to false

Note: prefetchSize and global two, rabbitmq not achieved, for the time being do not do research, prefetchCount in the case of an automatic answer is not in force, must be manually sign

Creating Producer

package com.dwz.rabbitmq.qos;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        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, null, msg.getBytes());
        }

      channel.close();
      connection.close();

    }
}

Creating consumer

package com.dwz.rabbitmq.qos;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.dwz.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        
        String exchangeName = "test_qos_exchange";
        String routingkey = "qos.#";
        String queueName = "test_qos_queue";
        
        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingkey);
        //Limiting 
        channel.basicQos (0,. 1, to false ); 
        
        DefaultConsumer Consumer = new new DefaultConsumer (Channel) { 
            @Override 
            public  void handleDelivery (String consumerTag, Envelope Envelope, BasicProperties Properties, byte [] body)
                     throws IOException {
                 Super .handleDelivery (consumerTag , Envelope, Properties, body);
                 // manually sign (because only one pass over the data, so do not receiving bulk to false = Multiple) 
                channel.basicAck (envelope.getDeliveryTag (), to false ); 
            } 
        }; 
        
        //Limiting embodiment, the first thing is autoAck set to false 
        channel.basicConsume (queueName, to false , Consumer); 
    } 
}

related articles:

RabbitMQ consumer side limiting policies (X)

Guess you like

Origin www.cnblogs.com/zheaven/p/11832840.html