How to implement priority queue and RabbitMQ priority queue based on rabbitmq
Overview
For various reasons, up to now, RabbitMQ has not implemented a priority queue, but only implemented priority processing for Consumer.
However, for various reasons, Priority Queues are required at the application layer, so the requirement is: how to add priority queue features to RabbitMQ.
After querying the information, I learned that although RabbitMQ does not officially support this feature, the Community already has the relevant priority queue plug-in, and this plug-in is listed on the official website of RabbitMQ.
Address: http://www.rabbitmq.com/community-plugins.html
Plugin installation
Do not download the link in this url immediately. First download the corresponding plug-in from another location based on the version of rabbitmq you want to update. For example:
The plug-in directories of the two major versions will be listed (select the corresponding directory to download, otherwise an error will be reported ...):
How to install the plug-in?
Enter the rabbitmq installation directory, enter the plugins directory, copy the ez File above to the plugins directory, and then run the command to enable this plug-in.
In centos, the default path is/usr/lib/rabbitmq/lib/rabbitmq_server-3.3.4/plugins (the version number may change)
In windows, the default path is C: \ Program Files \ RabbitMQ Server \ rabbitmq_server-3.3.4 \ plugins (the version may change)
Copy the ez File and run the following command to list the plug-ins:
Find the priority queue plug-in named rabbitmq_priority_queue.
Run: rabbitmq-plugins enable rabbitmq_priority_queue
OK. Restart the rabbitmq-server service.
In this way, the server configuration is complete.
C # changes required by the code end
Let's take a look at the compilation of the client Class Library:
First, we need to define priority enumeration to inherit from byte, because the C # client priority of RabbitMQ is transmitted using byte:
First define three levels of priority: low, medium, and high (in fact, many levels can be defined, just to simplify, so only three levels are defined)
There are two changes:
internal static IDictionary <string, object> QueueArguments
{
get
{
IDictionary <string, object> arguments = new Dictionary <string, object> ();
arguments ["x-max-priority"] = 10; // Define the queue priority to 10 levels
return arguments;
}
}
channel.QueueDeclare ("queueName", true, false, false, QueueArguments); // QueueArguments is the dictionary defined above
var headers = channel.CreateBasicProperties ();
headers.Priority = (byte) msg.Priority; // Convert the enumeration inherited from byte to byte here
channel.BasicPublish ("exchange", "route", headers, SerializerUtility.Serialize2Bytes (msg));
Other notes
In the rabbitmq-server instance with the priority queue plug-in installed, all Durable queues must use the above method to set the x-max-priority attribute. Otherwise, the rabbitmq-server Service will crash
How to sort messages in the system message queue by priority? --- VC
Hello,
Windows is a protection mechanism. User Programs can only accept messages, but cannot sort priorities,
If you want to arrange the priority, adjust the thread, or change the message with a higher priority,
Thank you,
What is the difference between a priority queue and a queue?
A priority queue is another queue different from a first-in-first-out queue. Each time an element is retrieved from the queue, it has the highest priority.
Class Definition of priority queue
# Include <assert. h>
# Include <iostream. h>
$ Include <stdlib. h>
Const int maxPQSize = 50; // default number of elements
Template <class Type> class PQueue {
Public:
PQueue ();
~ PQueue () {delete [] pqelements ;}
Void PQInsert (const Type & item );
Type PQRemove ();
Void makeEmpty () {count = 0 ;}
Int IsEmpty () const
{Return count = 0 ;}
Int IsFull () const
{Return count = maxPQSize ;}
Int Length () const {return count ;}
Private:
Type * pqelements; // stores the Array
Int count; // queue element count
}
A priority queue is a set of 0 or more elements. Each element has a priority or value. The operations performed on the priority queue are as follows: 1) Search; 2) Insert a new element; 3) delete. in min priorityq u e, the search operation is used to search for the element with the smallest priority, and the delete operation is used to delete the element. For max priority queue ), the search operation is used to search for the element with the highest priority, and the delete operation is used to delete the element. the elements in the priority queue can have the same priority. The search and delete operations can be performed based on any priority.
The description of the abstract data type of the maximum priority queue is shown in ADT 9-1. The description of the abstract data type of the minimum priority queue is similar to that of the maximum priority queue.
ADT maximum priority queue abstract data type description abstract data type
M a x P r I o r I t y Q u e {
A Finite Element Set of instances. Each element has a priority.
Operation
Create (): Create an empty priority queue
Size (): returns the number of elements in the queue.
Max (): returns the element with the highest priority.
I n s e rt (x): insert x into the queue
DeleteMax (x): deletes an element with maximum priority from the queue and returns the element to x
}
Priority queue insertion and deletion of elements are complex O (lgn), so it is very fast.
An ordered linear table is used to describe the elements in ascending order and linked lists in descending order. The deletion time of the two descriptive methods is (1 ), the time required for the insert operation is (n ).
Example:
Assume that we charge for the machine service. Each user pays the same cost for using the machine each time,
The service time required by the user is different. In order to get the maximum profit, assuming that the user machine will not be idle, we can
The user waiting to use this machine is organized into a minimum priority queue. Priority is the service time required by the user. When a new
When a user needs to use a machine, add his/her requests to the priority queue. Once the machine is available, the minimum service time is required.
(That is, users with the highest priority) provide services.
If each user needs the same time, but the user is willing to pay different fees, you can use the payment as the priority,
I. ...... the remaining full text>