. NET medium and large project development prerequisite (12) -- using MQ message queue

Related downloads:

RabbitMQ message component
RabbitMQ message component

 

RabbitMQ has long been popular as a mainstream message queuing tool. Compared with other MQ tools, RabbitMQ supports more languages and more complete functions.

 

This article provides the simplest way to use RabbitMQ on the market. You can almost master the use of RabbitMQ by calling the following three methods:

(1) SendMessage to send a message

(2) GetMessage to get a message

(3) UseMessage, using one message (continuous use)

 

In order to call the above three methods, you first need to reference developer sharp. In the project RabbitMQ. DLL and rabbitmq Client. DLL two components, and in app config/Web. Add the following configuration to config:

  <appSettings>
    <add key="RabbitMQConnectionString" value="hostName=135.208.12.236,port=5672,userName=sa,password=aevin.gang" />
  </appSettings>

Note: in the above configuration, the IP address hostName, port, userName and password of the server where the RabbitMQ application is located are set respectively (please change the corresponding values of these four items to the corresponding values of your own RabbitMQ)

 

Next, we give an example using the above SendMessage, GetMessage and UseMessage methods. The functions of this example are described as follows:

  • First, five messages were sent to the queue named "aa" on the RabbitMQ server,
  • Then get it from the "aa" queue on the RabbitMQ server and print the first message,
  • Finally, get the remaining four messages from the "aa" queue on the RabbitMQ server and write them to the queue named FJ Txt file.

The code is as follows:

using DeveloperSharp.RabbitMQ;
--------------------------

        static void Main(string[] args)
        {
            //Send 5 messages (using SendMessage)
            RabbitMQHelper.SendMessage("aa", "World 1, Hello!");
            RabbitMQHelper.SendMessage("aa", "World 2, Hello!");
            RabbitMQHelper.SendMessage("aa", "World 3, Hello!");
            RabbitMQHelper.SendMessage("aa", "World 4, Hello!");
            RabbitMQHelper.SendMessage("aa", "World 5, Hello!");

            //Get 1 message (use) GetMessage)
            string OneMessage = RabbitMQHelper.GetMessage("aa").Message;
            Console.WriteLine(OneMessage);

            //towards fj.txt Write 4 messages in this text file (using UseMessage)
            RabbitMQHelper.UseMessage("aa", t => 
            {
                System.IO.File.AppendAllText("D:/fj.txt", t.Message);
                return true;
            });
        }

The operation results are as follows:

[console display]: World 1, Hello!

[displayed in fj.txt file]: World 2, Hello! World 3, Hello! World 4, Hello! World 5, Hello!

 

 

Detailed function description of the three methods (auxiliary reference):

(1)Send a message
void SendMessage(string QueueName, string Message, Dictionary<string, object> Header = null)

(2)Get a message
RabbitMQMessage GetMessage(string QueueName)

(3)Use one message (continuous use)
void UseMessage(string QueueName, Func<RabbitMQMessage, bool?> Use)
Additional notes:
    (I)Use The return value is true Indicates that the current message has been effectively processed and will be deleted by the server. Then the program automatically enters the use of the next message.
         if Use The return value is false It means that the current message has not been effectively processed but will still be deleted by the server. Then the program automatically enters the use of the next message.
         if Use The return value is null The message representing the current message will be re queued by the server to other available instances for further processing. Then the program automatically enters the use of the next message.
         if Use If an unhandled exception occurs internally, the program will stop.
    (II)RabbitMQMessage The object definition is as follows:
          public class RabbitMQMessage
          {
             public string Message;
             public IDictionary<string, object> Header;
             public string Id; //Automatically generated here Id have UUID Characteristics of.
          }
    (III)RabbitMQMessage.Header In the dictionary, the actual value is string Type object Value, which needs byte conversion to become a string.
           For example: Encoding.UTF8.GetString((byte[])p.Header["Content"])

A final note: once the three methods are called, a "long link" will be established with the RabbitMQ server. To end the long link, kill the process in the task manager.

 

The use of message services is of great use in decoupling distributed systems, realizing publish / subscribe, improving system performance, etc. I believe this article will expand your thinking and cognition and give you more flexible ideas + association space in relevant technical solutions!

 

[note]: the download examples given at the beginning of the article have been successfully run. However, download examples often contain only "core template" content. Some auxiliary contents need to be created / set by yourself (such as database creation, link string setting, file configuration, path setting, parameter setting, etc.).
Please do an example experiment on the basis of understanding the content of the article. In case of abnormal error, please read + understand this article carefully again.
If you do encounter difficulties, please scan the QR code at the end of the text on wechat and contact technical support!

Keywords: C# RabbitMQ .NET message queue

Added by Hieroglyphics on Wed, 26 Jan 2022 14:46:04 +0200