RABBITMQ Study Notes (Fri)--topic

Source: Internet
Author: User
Tags bind rabbitmq
Receivetopicone matching rule 1//Declaration exchanger and Queue Channel.exchangedeclare (exchange_name, "topic");

String queuename = Channel.queuedeclare (). Getqueue (); 

Routing keyword Rule string[] routingkeys = new string[]{"*.*.rabbit", "lazy.#"};  

 Bind routing keyword for (String bindingkey:routingkeys) {channel.queuebind (QueueName, Exchange_name, Bindingkey);

} receivetopictwo matching rule 2//Declaration exchanger and Queue Channel.exchangedeclare (exchange_name, "topic");

String queuename = Channel.queuedeclare (). Getqueue ();  

Routing keyword Rule string[] routingkeys = new string[]{"*.orange.*"};  

Bind routing keyword for (String bindingkey:routingkeys) {channel.queuebind (QueueName, Exchange_name, Bindingkey);  

} topicsend Send Channel.exchangedeclare (exchange_name, "topic");

                                                            Message to be sent string[] Routingkeys = new string[]{"Quick.orange.rabbit",   

                    "Lazy.orange.elephant", "Quick.orange.fox",                                        "Lazy.brown.fox",   

                                                            "Quick.brown.fox", "Quick.orange.male.rabbit",  

"Lazy.orange.male.rabbit"};  

for (String Bindingkey:routingkeys) {channel.queuebind (QueueName, Exchange_name, Bindingkey);   }

With three keywords to bind, the Q1 binding keyword is ". Orange.", the Q2 binding keyword is "... Rabbit "and" lazy.# ", and then analyze what happens:

Q1 will receive all this color-related messages from Orange.

Q2 will receive all messages about rabbit this animal and all the speed of the lazy animal

Analysis:

The producer sends a "quick.orange.rabbit" message, and two queues will receive

Producers send "Lazy.orange.elephant", and both queues will receive.

Producers send "Quick.orange.fox", then only Q1 will receive.

Producers send "Lazy.brown.fox", then only Q2 will be able to receive it.

Producers send "Quick.brown.fox", then this message will be discarded and no one can receive it.

Producers send "Quick.orange.male.rabbit", the message will be discarded, no one can receive.

The producer sends "Lazy.orange.male.rabbit", which is sent to the Q2 queue by matching the Q2 "lazy.#" rule.

Attention

The switch is in match mode:

If the consumer's routing keyword only uses "#" to match the message, in the match "topic" mode, it becomes a distribution "fanout" mode, receiving all messages.

If there is no "#" or "*" in the customer's routing keyword, it becomes direct-connect "direct" mode to work.

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.