RabbitMQ usage scenario exercise: priority queue (10)

  • priority queue
     The priority queue will take effect only when there are insufficient consumers and cannot consume in time.

     RabbitMQ 3.5 has been integrated with rabbitmq_priority_queue
quote


Verification method: The trigger is a timely consumption scenario . Common scenarios are used in combination with QoS.
1. You can send a message first, and then consume it.
2. Enable manual response and set QoS. If it is 1, in the presence of a consumer, all consumers except the first message are consumed according to priority (the first message is consumed in time)
3. On the basis of method 2, consumers can be added continuously, Also conforms to the precedence call rule

  • Points to Note

Set priority level for messages :
//set the message priority randomly
Builder properties=new BasicProperties.Builder();
int priority=new Random().nextInt(10);
properties.priority(priority);//Recommended 0~255, it seems that it is no problem to exceed
channel.basicPublish(exchange_name, "", properties.build(), SerializationUtils.serialize(_mes));

Create a priority level for the queue :
//Set the priority of the queue, the priority of the message is greater than the priority of the queue, whichever is smaller (for example: the priority of the queue is 5, the priority of the message is 8, and the actual priority of the message is 5)
Map<String, Object> args=new HashMap<String, Object>();
args.put("x-max-priority", 10);//The queue priority can only be declared once and cannot be changed (involving hard disk and memory storage methods)
channel.queueDeclare(queueName, false, false, false, args);


The priority of the queue and message must be set to take effect. The smaller value shall prevail. The

priority of the queue can only be declared once and cannot be changed (involving the hard disk and memory storage method).

The priority queue has costs in memory, hard disk and cpu Consumption, it is not recommended to create a large number of priority levels (number, type of level, large level, confusion in understanding, difficulty in understanding English...)

  • Priority queue test message sending class

package com.demo.mq.rabbitmq.example10;

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

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * Priority queue test message sending class
 * @author sheungxin
 *
 */
public class PrioritySend {
	private static String exchange_name="priority_direct";
	
	public static void prioritySend(Serializable mes) throws IOException, TimeoutException{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare(exchange_name, BuiltinExchangeType.DIRECT);
		// send 10 messages
		for(int i=0;i<10;i++){
			//set the message priority randomly
			Builder properties=new BasicProperties.Builder();
			int priority=new Random().nextInt(10);
			properties.priority(priority);//Recommended 0~255, it seems that it is no problem to exceed
			String _mes=mes.toString()+i;
			channel.basicPublish(exchange_name, "", properties.build(), SerializationUtils.serialize(_mes));
			System.out.println(priority+" "+_mes);
		}
		channel.close();
		conn.close();
	}
	
	public static void main(String[] args) throws IOException, TimeoutException {
		prioritySend("priority send:hello world!");
	}

}

  • Priority queue test message receiving class

package com.demo.mq.rabbitmq.example10;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import org.apache.commons.lang3.SerializationUtils;
import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class PriorityRecv {
	private static String exchange_name="priority_direct";
	private static String queueName="priority_queue";
	
	/**
	 * The priority queue will take effect only when there are insufficient consumers and cannot consume in time
	 * Ways of identifying:
	 * 1. Messages can be sent first, and then consumed
	 * 2. Enable manual answering and set QoS. If it is 1, in the presence of a consumer, except the first message is consumed according to priority (the first message is consumed in time)
	 * 3. Consumers can be continuously added on the basis of method 2, and it also conforms to the priority calling rule
	 *Note points:
	 * 1. The priority must be set on the queue and message to take effect, whichever is smaller;
	 * 2. The queue priority can only be declared once and cannot be changed (involving hard disk and memory storage methods)
	 * 3. The priority queue will consume costs in memory, hard disk, and cpu. It is not recommended to create a large number of priority levels (number, type of level, large level, confusion, difficulty in understanding English...)
	 * @throws IOException
	 * @throws TimeoutException
	 */
	public static void priorityRecv() throws IOException, TimeoutException{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare(exchange_name, BuiltinExchangeType.DIRECT);
		//Set the priority of the queue, the priority of the message is greater than the priority of the queue, whichever is smaller (for example: the priority of the queue is 5, the priority of the message is 8, and the actual priority of the message is 5)
		Map<String, Object> args=new HashMap<String, Object>();
		args.put("x-max-priority", 10);//The queue priority can only be declared once and cannot be changed (involving hard disk and memory storage methods)
		channel.queueDeclare(queueName, false, false, false, args);
		channel.queueBind(queueName, exchange_name, "");
		channel.basicQos(1);//Need to enable manual answer mode, otherwise invalid
		channel.basicConsume(queueName, false, new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException{
				String mes=SerializationUtils.deserialize(body);
				System.out.println(properties.getPriority()+":priority Received :'"+mes+"' done");	
				channel.basicAck(envelope.getDeliveryTag(), false);
			}
		});
	}
	
	public static void main(String[] args) throws IOException, TimeoutException {
		priorityRecv();
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326827537&siteId=291194637