A detailed description of the graphics code for RABBITMQ applications in C #

Source: Internet
Author: User
Tags rabbitmq
RABBITMQ is what and how to install will not repeat, Baidu a bit to know, but in the configuration to pay more attention.

Not much to say, first directly to a simple example code

Send side:


 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 < i++) {byte[] message = Encoding.UTF8.GetBytes ("Hel                        Lo Lv "); Im.                        Basicpublish ("Rabbitmq_route", exchangetype.direct, NULL, message);                    Console.WriteLine ("Send:" + i); }                }            }

Receiving end:


            ConnectionFactory factory = new ConnectionFactory {HostName = "HostName", UserName = "root", Password = "root001", Virtua Lhost = "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));}}}}            

Send the end of a one-time send 1000, send the process quickly, receive relatively slower.

The demo is limited to one receive, the same amount of transmission, the number of receivers will be what happens, add a new receiver, directly copy the demo in the receiving side.

Attach the result of the operation:

As you can see, RabbitMQ distributes each message sequentially when it runs at the same time as two receivers. When each acknowledgment is received, the message is deleted and then distributed to the next recipient, mainly because of the RABBITMQ circular distribution mechanism.

To put it briefly, the message is almost two receive-side pairs at multiple receivers, because of the reason for cyclic distribution.

So how do you distribute the same message to multiple receive ends.

To modify the send-side 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);//route 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 ()); }                }            }

Compared to the previous method, you will find that there are two less code behind the code comment, and you do not need to specify the queue name after you set the Fanout mode. Stop for a second to make it easier to see the results, lest the refresh be too fast.

Take a look at the receive-side code:


            ConnectionFactory factory = new ConnectionFactory {HostName = "HostName", UserName = "root", Password = "root                        001 ", VirtualHost =" Host "}; using (iconnection conn = factory. CreateConnection ()) {using (IModel IM = conn. Createmodel ()) {im.                                        Exchangedeclare ("Rabbitmq_route_fanout", exchangetype.fanout); var queueok = im. Queuedeclare ();//1 im. Queuebind (Queueok.queuename, "Rabbitmq_route_fanout", "" ");//2 var consumer = new Qu Eueingbasicconsumer (IM);//3 im.                    Basicconsume (Queueok.queuename, true, consumer);//4 while (true) {var _result = (Basicdelivereventargs) consumer. Queue.dequeue ();//5 var body = _result.                                            Body; var message = Encoding.UTF8.GetSTring (body);                    Console.WriteLine ("Received:{0}", message); }                }

When a new receiving end is connected (consumer), it is necessary to declare a new queue, comment 1 code, RABBITMQ in the declaration queue, if you do not specify a name will automatically generate a, this is good.

The result is running at two receivers, as expected.

As to what is wrong with the way of broadcasting, run it in person.

Related Article

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.