RabbitMQ Topic Exchange

Source: Internet
Author: User

Topic Exchange

Topic is different from each of the previous types (PS: nonsense each is different). Topic solved our other needs. For example, there is a company that does information that collects the dynamics of various technology companies and forwards them the first time. Small Series A is responsible for Microsoft company, small part B is responsible for the Google Company, manual to search articles and see if the title match and then forwarding is very inefficient, may be small made up to lazy, write a program to the big Web site to crawl data, for the title contains Microsoft's hand to small A, for the title contains Google to small series B.

Yes, topic is similar to the regular fuzzy matching routingkey, for key requirements is determined and not fully determined.

* represents 0 to more characters

. Represents a character

Embed this into the code, first look at the producer part of the code, now release is no longer write dead, can be entered in the console program.

varFlag =true;  while(flag) {Console.WriteLine ("Please enter the message key|msg to be published. or press CTRL + C to exit"); varmsg =Console.ReadLine (); //Create a new channel to return                using(varChannel =rabbitmqhelper.getconnection (). Createmodel ()) {varmsgs = Msg. Split ('|'); //Publish a message                    varBODY = Encoding.UTF8.GetBytes (msgs[1]); Channel. Basicpublish ("Topicexchange", routingkey:msgs[0], Basicproperties:NULL, Body:body); Console.Write ("Publish successfully! "); }            }

Then turn your attention to the consumer side, you can specify the routing rules. Declares an topic type of exchange for binding. And then to spend

BOOLFlag =true; varKey ="";  while(flag) {Console.WriteLine ("Please enter a route regular. Represents a character * representing 0 to more characters"); Key=Console.ReadLine (); if(string. Isnullorwhitespace (key)) {Console.Write ("Please enter a route"); Continue; }                ElseFlag=false; }            using(varChannel =rabbitmqhelper.getconnection (). Createmodel ()) {//queues that are used according to claims                varQueueName = key +"Queue"; //declaring switch headers modeChannel. Exchangedeclare ("Topicexchange", Exchangetype.topic,true,false); Channel. Queuedeclare (QueueName,true,false,false,NULL); //to bindChannel. Queuebind (QueueName,"Topicexchange", Key,NULL); //Create Consumbers                varConsumer =NewEventingbasicconsumer (channel); Consumer. Received+ = (sender, e) = =                {                    varmsg =Encoding.UTF8.GetString (e.body); Console.WriteLine ($"{e.routingkey}:{msg}");                }; //for consumptionChannel. Basicconsume (QueueName,true, consumer);            Console.readkey (); }

Now, let's run the program and specify the route of * Microsoft * with * Google *

And then publish a few matching messages.

RabbitMQ Topic 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.