This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19634|Reply: 1

[Plugin Library] Message queue sending and receiving message demo

[Copy link]
Posted on 5/30/2019 10:58:30 PM | | | |



preface


In some places, message queues are used to ensure the accuracy of information processing. For example, in high concurrency situations, websites perform data processing. Today this article records a simple demo of RabbitMQ. Before entering this article, the RbMQ environment must be installed in advance (because I installed the local RbMQ environment before, and I am afraid that uninstalling will cause some problems that the registry is not clean, so I will not record the installation of the environment here).



Environment & Tools



>Windows 10 system
>RabbitMQ Environmental Services
>VS 2017



Text



1: Create two new console applications, namely "RabbitMQClient" and "RabbitMQServer".





2: Refer to the "RabbitMQ.Client" dll

3: In the RabbitMQClient project, Program.cs, paste the following code



4: In the RabbitMQServer project, Program.cs, paste the following code



5: F5 to start the RabbitMQServer project, in the console, enter the information, press enter.



6: Do not close the RabbitMQServer project console, right-click on the RabbitMQClient project - > "Debug" - > "Start a new instance"






You can enter information in the RabbitMQServer console on the right to display it on the left. The RabbitMQServer project produces messages. The RabbitMQClient project processes (consumes) messages. This is the producer and consumer in the message queue. The code also involves routing and channels.

On the message producer's side, drop the information to be processed into the message queue. After receiving the message, the consumer can process this data accordingly.

If the environment is installed, the local default port address should be http://localhost:15672/#/queues. You can directly access the guest account, or you can configure the account and corresponding permissions by tapping the command line.



The information you just entered in the RabbitMQServer console actually exists in the message queue first, you can close the RabbitMQClient window first, and then enter a few pieces of information in the window, observe the message queue, and you can see that there are three pieces of data in the queue.



When our RabbitMQClient window starts, the message will automatically disappear from the message queue after receiving it normally. This is the confirmation mechanism of the message queue, once the information we need is accurately received, the consumer will return a state to the message queue to destroy the message. If the consumer does not receive the message, or if there is an exception and does not return this status value, the message will remain in the queue until the message is consumed normally, which ensures the accuracy of the message.



Epilogue




Unfortunately, there are very few places where I use message queues in my projects. Without an in-depth understanding, only the basics will be used.

There is a hole in the above demo, and the connection of RbMQ in RabbitMQClient has a layer of using. Because it is an asynchronous thread, this will cause the program to run and recycle the RbMQ Connection before it finishes receiving the message, and an error will be reported. The way to deal with it is: remove using and release it manually.













Previous:"npm ERR! Error: EPERM: operation not permitted"
Next:Introduction to nine conventional vision software
Posted on 5/31/2019 9:12:21 AM |
It is best to upload the demo source code of the article to:https://down.itsvse.com/
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com