Home Backend Development C#.Net Tutorial Detailed explanation of graphic code for RabbitMQ application in C#

Detailed explanation of graphic code for RabbitMQ application in C#

Jul 27, 2017 pm 04:10 PM
.net rabbitmq

rabbitmq is not described as and how to install it. Baidu knows it at once, just pay more attention in terms of configuration.

Without further ado, let’s start with a simple example code

Sender:


            ConnectionFactory factory = new ConnectionFactory { HostName = "hostname", UserName = "root", Password = "root001", VirtualHost = "hostserver" }; 
                       using (IConnection conn = factory.CreateConnection())
            {                using (IModel im = conn.CreateModel())
                {
                    im.ExchangeDeclare("rabbitmq_route", ExchangeType.Direct);
                    im.QueueDeclare("rabbitmq_query", false, false, false, null);
                    im.QueueBind("rabbitmq_query", "rabbitmq_route", ExchangeType.Direct, null);                    
                    for (int i = 0; i < 1000; i++)
                    {                        byte[] message = Encoding.UTF8.GetBytes("Hello Lv");
                        im.BasicPublish("rabbitmq_route", ExchangeType.Direct, null, message);
                        Console.WriteLine("send:" + i);
                    }
                }
            }
Copy after login

Receiver:


#
            ConnectionFactory factory = new ConnectionFactory { HostName = "hostname", UserName = "root", Password = "root001", VirtualHost = "hostserver" }; 
                       using (IConnection conn = factory.CreateConnection())
            {                using (IModel im = conn.CreateModel())
                {                    while (true)
                    {
                        BasicGetResult res = im.BasicGet("rabbitmq_query", true);                        
                        if (res != null)
                        {
                            Console.WriteLine("receiver:"+UTF8Encoding.UTF8.GetString(res.Body));
                        }
                    }
                }
            }
Copy after login

Send a thousand sending one at one time, the sending process is fast, and it is relatively slow when receiving.

                                                                                                                                                                                                   Just end it.

                                                                                                           It can be seen that when two receivers are running at the same time, RabbitMQ will Distribute each message sequentially. When each confirmation is received, the message will be deleted, and then the next one will be distributed to the next recipient, mainly because of RabbitMQ's

cyclic distribution

mechanism.

. When multiple receivers, due to the reasons of cycle distribution, the news is almost two receiving end.

So how to distribute the same message to multiple receivers.

Modify the sending terminal code:

##

            ConnectionFactory factory = new ConnectionFactory { HostName = "hostname", UserName = "root", Password = "root001", VirtualHost = "host" }; 
                       using (IConnection conn = factory.CreateConnection())
            {                using (IModel im = conn.CreateModel())
                {
                    im.ExchangeDeclare("rabbitmq_route_Fanout", ExchangeType.Fanout);// 路由                    
                    int i = 0;                    
                    while (true)
                    {
                        Thread.Sleep(1000);
                        ++i;                        
                        byte[] message = Encoding.UTF8.GetBytes(i.ToString());
                        im.BasicPublish("rabbitmq_route_Fanout", "", null, message);
                        Console.WriteLine("send:" + i.ToString());
                    }
                }
            }
Copy after login
Compared with the above -mentioned method, you will find that there are two sections of code behind the code annotation. After this method, there is no need to specify the queue name. The purpose of pausing for one second is to facilitate viewing the results and avoid refreshing too quickly.

Let's take a look at the receiving end code:

# When a new receiving end connection (consumer), you need to declare a new queue, Comment on code 1. When RabbitMQ declares a queue, it will automatically generate one if you do not specify a name, which is good.

When there are two receivers, the running results are in line with expectations.

                                                                                               What are the disadvantages? You will know after running it yourself.

                 

The above is the detailed content of Detailed explanation of graphic code for RabbitMQ application in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build a reliable messaging app with React and RabbitMQ How to build a reliable messaging app with React and RabbitMQ Sep 28, 2023 pm 08:24 PM

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

How to use RabbitMQ to implement distributed message processing in PHP How to use RabbitMQ to implement distributed message processing in PHP Jul 18, 2023 am 11:00 AM

How to use RabbitMQ to implement distributed message processing in PHP Introduction: In large-scale application development, distributed systems have become a common requirement. Distributed message processing is a pattern that improves the efficiency and reliability of the system by distributing tasks to multiple processing nodes. RabbitMQ is an open source, reliable message queuing system that uses the AMQP protocol to implement message delivery and processing. In this article we will cover how to use RabbitMQ in PHP for distribution

How SpringBoot integrates RabbitMQ to implement delay queue How SpringBoot integrates RabbitMQ to implement delay queue May 16, 2023 pm 08:31 PM

How to ensure that messages are not lost. The rabbitmq message delivery path producer->switch->queue->consumer is generally divided into three stages. 1. The producer ensures the reliability of message delivery. 2.MQ internal messages are not lost. 3. Consumer consumption is successful. What is message delivery reliability? Simply put, it means that messages are 100% sent to the message queue. We can turn on confirmCallback. After the producer delivers the message, mq will give the producer an ack. Based on the ack, the producer can confirm whether the message is sent to mq. Turn on confirmCallback and modify the configuration file #NONE: disable the release confirmation mode, which is the default value , CORRELATED:

Using RabbitMQ in Go: A Complete Guide Using RabbitMQ in Go: A Complete Guide Jun 19, 2023 am 08:10 AM

As modern applications increase in complexity, messaging has become a powerful tool. In this area, RabbitMQ has become a very popular message broker that can be used to deliver messages between different applications. In this article, we will explore how to use RabbitMQ in Go language. This guide will cover the following: Introduction to RabbitMQ RabbitMQ Installation RabbitMQ Basic Concepts Getting Started with RabbitMQ in Go RabbitMQ and Go

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

Solution for real-time data synchronization between Golang and RabbitMQ Solution for real-time data synchronization between Golang and RabbitMQ Sep 27, 2023 pm 10:41 PM

Introduction to the solution for real-time data synchronization between Golang and RabbitMQ: In today's era, with the popularity of the Internet and the explosive growth of data volume, real-time data synchronization has become more and more important. In order to solve the problems of asynchronous data transmission and data synchronization, many companies have begun to use message queues to achieve real-time synchronization of data. This article will introduce a real-time data synchronization solution based on Golang and RabbitMQ, and provide specific code examples. 1. What is RabbitMQ? Rabbi

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

Application practice of go-zero and RabbitMQ Application practice of go-zero and RabbitMQ Jun 23, 2023 pm 12:54 PM

Now more and more companies are beginning to adopt the microservice architecture model, and in this architecture, message queues have become an important communication method, among which RabbitMQ is widely used. In the Go language, go-zero is a framework that has emerged in recent years. It provides many practical tools and methods to allow developers to use message queues more easily. Below we will introduce go-zero based on practical applications. And the usage and application practice of RabbitMQ. 1.RabbitMQ OverviewRabbit

See all articles