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.