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:
-
- Custom Attributes must be added to the queue.
- Set custom attributes when sending messages to rabbitmq
Internal static idictionary <string, Object> queuearguments {get {idictionary <string, Object> arguments = new dictionary <string, Object> (); arguments ["X-max-priority"] = 10; // defines the queue priority as 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; // here, convert the enumeration inherited from byte to bytechannel. 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 implement priority queue based on rabbitmq