How to implement priority queue based on rabbitmq

Source: Internet
Author: User
Tags 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:

    1. Custom Attributes must be added to the queue.
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.