RABBITMQ--The realization of communication service in C #

Source: Internet
Author: User
Tags rabbitmq

MQ is all called the message queue, and Message Queuing (MQ) is an application-to-application communication method. Applications communicate by reading and writing messages to and from the queue (data for the application), without requiring a dedicated connection to link them.

Messaging refers to the technology that is used to communicate between programs by sending data in a message, rather than by directly invoking each other, and directly invoking techniques such as remote procedure calls (for example, thrift mentioned in the previous article). Queuing refers to an application communicating through a queue. The use of queues removes the requirement that both the receiving and sending applications execute concurrently.

Come, we begin to enter the door of inquiry.

The first step:

Download the relevant stuff, of course.

Because RABBITMQ is implemented by Erlang, you need to install Erlang first.

Download Erlang and install, currently the latest version is 21.0.1.

Download Rabbitmq-server and install, the latest version is now 3.7.7.

Step Two:

Create two of the following assemblies with VisualStudio (console program), Consumerclient (consumer), producerclient (production side).

Step Three:

Both assemblies use NuGet to add easynetq references, and EASYNETQ is a component RABBITMQ the official package that makes it easier to use RABBITMQ. Of course easynetq will depend on rabbitmq.client, so you add EASYNETQ references and also add rabbitmq.client.

Fourth Step:

Write specific code:

The Program.cs part of the Consumerclient side code is:

        /// <summary>        ///Main function/// </summary>        /// <param name= "args" ></param>        Static voidMain (string[] args) {IBus bus= Rabbithutch.createbus (string. Format ("host={0}","127.0.0.1:5672")); Iadvancedbus Advancedbus=Bus.            Advanced; Iexchange ExChange= Advancedbus.exchangedeclare ("FANOUT_MQ","fanout"); Iqueue Queue=Advancedbus.queuedeclare (); Advancedbus.bind (ExChange, queue,string.            Empty); Advancedbus.consume (Queue, registration= = {Registration. add<string> (message, info) = {Console.WriteLine ("received message ' {0} '", message. Body); });            });        Console.ReadLine (); }

The Program.cs part of the Producerclient side code is:

        /// <summary>        ///Main function/// </summary>        /// <param name= "args" ></param>        Static voidMain (string[] args) {            Try{IBus bus= Rabbithutch.createbus (string. Format ("host={0}","127.0.0.1:5672")); Iadvancedbus Advancedbus=Bus.                Advanced; Iexchange ExChange= Advancedbus.exchangedeclare ("FANOUT_MQ","fanout"); NewAction (() =                {                     for(inti =0; I < +; i++) {Thread.Sleep ( +); stringMessage ="$$$$$$ A relatively long message type $$$$$$"+i; Advancedbus.publish (ExChange,string. Empty,false,Newmessage<string>(message)); Console.WriteLine ("message "{0}" was sent", message); }                }). BeginInvoke (NULL,NULL); Iexchange ExChange2= Advancedbus.exchangedeclare ("FANOUT_MQ","fanout"); NewAction (() =                {                     for(inti =0; I < -; i++) {Thread.Sleep ( -); stringMessage ="##### #二号短消息类型"+i; Advancedbus.publish (ExChange2,string. Empty,false,Newmessage<string>(message)); Console.WriteLine ("message "{0}" was sent", message); }                }). BeginInvoke (NULL,NULL); }            Catch(Exception ex) {Console.WriteLine (ex).            Message);        } console.readline (); }

Fifth Step:

Execute the program, first execute consumerclient, then execute producerclient, test, two processes through this message queue to achieve the purpose of communication, done!

RABBITMQ--implementation of communication services in C #

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.