Rabbitmq three types of exchange

Source: Internet
Author: User
Tags rabbitmq
From: http://melin.iteye.com/blog/691265RabbitMQ three types of exchange
    Blog type: 

  • Amqp
Exchangeredhat job Direct Exchange-Process the route key. To bind a queue to a vswitch, the message must match a specific route key. This is a complete match. If a queue is bound to a vswitch and the routing key "dog" is required, only messages marked as "dog" are forwarded and dog messages are not forwarded. puppy, and does not forward dog. guard, only the dog will be 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 a route key.
  7. Channel. basicpublish ("exchangename", "routingkey", messageproperties. persistent_text_plain, messagebodybytes );

Fanout exchange-Route keys are not processed. You only need to simply bind the queue to the vswitch. Messages sent to a vswitch are forwarded to all queues bound to the vswitch. Similar to subnet broadcast, each host in the subnetwork receives a copy message. The fanout switch forwards messages as quickly as possible.

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. // Set the route key to null.
  9. Channel. basicpublish ("exchangename", "", messageproperties. persistent_text_plain, messagebodybytes );

Topic exchange-Match the route key with a certain mode. In this case, the queue needs to be bound to a mode. The symbol "#" matches one or more words, and the symbol "*" does not match many words. Therefore, "audit. #" can match "audit. IRS. Audit ate", but "audit. *" only matches "audit. IRS ". My friend at RedHat made a good figure to show how the topic switch works:

Java code

  1. Channel channel = connection. createchannel ();
  2. Channel. exchangedeclare ("exchangename", "topic"); // direct fanout topic
  3. Channel. queuedeclare ("queuename ");
  4. Channel. queuebind ("queuename", "exchangename", "routingkey .*");
  5. Byte [] messagebodybytes = "Hello world". getbytes ();
  6. Channel. basicpublish ("exchangename", "routingkey. One", messageproperties. persistent_text_plain, messagebodybytes );
  • The size is 16.3 kb.
  • The size is 19.3 kb.
  • The size is 50.1 kb.

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.