RABBITMQ three types of exchange

Source: Internet
Author: User

Direct Exchange – Handles routing keys. A queue needs to be bound to the switch, requiring the message to exactly match a specific routing key. This is a complete match. If a queue is bound to the switch requiring the routing key "Dog", only the message labeled "Dog" is forwarded, the Dog.puppy is not forwarded, the Dog.guard is not forwarded, and only the dog is forwarded.

Java code
  1. Channel channel = Connection.createchannel ();
  2. Channel.exchangedeclare ("Exchangename", "direct"); //direct fanout topic
  3. Channel.queuedeclare ("QueueName");
  4. Channel.queuebind ("QueueName", "Exchangename", "Routingkey");
  5. byte[] messagebodybytes = "Hello World". GetBytes ();
  6. You need to bind the routing key
  7. Channel.basicpublish ("Exchangename", "Routingkey", Messageproperties.persistent_text_plain,  Messagebodybytes);



fanout Exchange – does not handle routing keys. You simply have to bind the queue to the switch. A message sent to the switch is forwarded to all queues that are bound to the switch. Much like a subnet broadcast, each host in the network receives a copy of the message. Fanout switch Forwarding message is the fastest.

Java code
  1. Channel channel = Connection.createchannel ();
  2. Channel.exchangedeclare ("Exchangename", "fanout"); //direct fanout topic
  3. Channel.queuedeclare ("QueueName");
  4. Channel.queuebind ("QueueName", "Exchangename", "Routingkey");
  5. Channel.queuedeclare ("queueName1");
  6. Channel.queuebind ("queueName1", "Exchangename", "RoutingKey1");
  7. byte[] messagebodybytes = "Hello World". GetBytes ();
  8. The routing key needs to be set to NULL
  9. Channel.basicpublish ("Exchangename", "" , Messageproperties.persistent_text_plain, messagebodybytes);



Topic Exchange – match the routing key with a pattern. At this point the queue needs to be bound to a pattern. The symbol "#" matches one or more words, and the symbol "*" matches no more than a few words. So "audit.#" is able to match to "Audit.irs.corporate", but "audit.*" matches only to "Audit.irs". My friend at Redhat made a nice diagram to show how the topic switch works:

Java code
      1. Channel channel = connection.createchannel ();   
      2. Channel.exchangedeclare (//direct fanout topic  
      3. Channel.queuedeclare ( "QueueName");   
      4. Channel.queuebind (
      5.   
      6. byte[]  Messagebodybytes =  "Hello world". GetBytes ();   
      7. channel.basicpublish (

RABBITMQ three types of 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.