How to implement priority queue based on RabbitMQ

Source: Internet
Author: User

How to implement 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:

    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

Install RabbitMQ in CentOS 5.6

Detailed installation record of RabbitMQ client C ++

Try RabbitMQ in Python

Deployment of production instances in the RabbitMQ Cluster Environment

Use PHP + RabbitMQ in Ubuntu

Process of installing RabbitMQ on CentOS

RabbitMQ concept and Environment Construction

RabbitMQ getting started

RabbitMQ details: click here
RabbitMQ: click here

This article permanently updates the link address:

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.