RabbitMQ types of fanout in exchange

Source: Internet
Author: User
Tags rabbitmq

Fanout Multicast

In the previous use of direct direct-connect switches, the Routingkey was used to decide which queue to push messages to.

Fanout, in turn, pushes the received message into all the queues it binds to.

Analysis of the business, what scenarios need it? A user registered the site of the user, generally we need to send text messages and e-mail notification, is it necessary to in the same consumer to do both of these things? This is not a single responsibility, but the message sent is the same, but in a different way. Do you want to use both types of routingkey to send them once? This is obviously not what we want. So Fanout appeared.

Fanout type of exchange pushes messages into all of the queues, so you don't need to specify Routingkey, and it's useless to specify them!

The following code looks

There are two kinds of needs, texting and sending emails

//Create a new channel to return using(varChannel =rabbitmqhelper.getconnection (). Createmodel ()) {//Publish a message        varmsg = Encoding.UTF8.GetBytes ($"Two dog son"); //You do not need to specify Routingkey, and it is useless to specify it. Because the switch is fanout typeChannel. Basicpublish ("Fanoutexchange", Routingkey:string. Empty, Basicproperties:NULL, body:msg); Console.Write ("Publish successfully! ");} Console.readkey ();

Here's the code for the consumer section.

BOOLFlag =true; stringPattern ="";  while(flag) {Console.WriteLine ("Please select Ccnsumer Mode 1 (send SMS)/2 (email)"); Pattern=Console.ReadLine (); if(Pattern = ="1"|| Pattern = ="2") Flag=false; ElseConsole.Write ("please make the right choice"); }            using(varChannel =rabbitmqhelper.getconnection (). Createmodel ()) {//declaring switch fanout modeChannel. Exchangedeclare ("Fanoutexchange", Exchangetype.fanout,true,false,NULL); //queues that are used according to claims                varQueueName = Pattern = ="1"?"SMS":"Emai"; Channel. Queuedeclare (QueueName,true,false,false,NULL); //to bindChannel. Queuebind (QueueName,"Fanoutexchange",string. Empty,NULL); //Create Consumbers                varConsumer =NewEventingbasicconsumer (channel); Consumer. Received+ = (sender, e) = =                {                    varmsg =Encoding.UTF8.GetString (e.body); varAction = (Pattern = ="1"?"Send SMS":"send e-mail"); Console.WriteLine ($"to {msg}{action}");                }; //for consumptionChannel. Basicconsume (QueueName,true, consumer);            Console.readkey (); }

Below the program to run, different consumer for different consumption

RabbitMQ types of fanout in exchange

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.