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