From: http://melin.iteye.com/blog/691265RabbitMQ three types of exchange
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
- Channel channel = connection. createchannel ();
- Channel. exchangedeclare ("exchangename", "direct"); // direct fanout topic
- Channel. queuedeclare ("queuename ");
- Channel. queuebind ("queuename", "exchangename", "routingkey ");
- Byte [] messagebodybytes = "Hello world". getbytes ();
- // You need to bind a route key.
- 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
- Channel channel = connection. createchannel ();
- Channel. exchangedeclare ("exchangename", "fanout"); // direct fanout topic
- Channel. queuedeclare ("queuename ");
- Channel. queuebind ("queuename", "exchangename", "routingkey ");
- Channel. queuedeclare ("queuename1 ");
- Channel. queuebind ("queuename1", "exchangename", "routingkey1 ");
- Byte [] messagebodybytes = "Hello world". getbytes ();
- // Set the route key to null.
- 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
- Channel channel = connection. createchannel ();
- Channel. exchangedeclare ("exchangename", "topic"); // direct fanout topic
- Channel. queuedeclare ("queuename ");
- Channel. queuebind ("queuename", "exchangename", "routingkey .*");
- Byte [] messagebodybytes = "Hello world". getbytes ();
- Channel. basicpublish ("exchangename", "routingkey. One", messageproperties. persistent_text_plain, messagebodybytes );