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
- 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 the routing key
- 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
- 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 ();
- The routing key needs to be set to NULL
- 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
- Channel channel = connection.createchannel ();
- Channel.exchangedeclare (//direct fanout topic
- Channel.queuedeclare ( "QueueName");
- Channel.queuebind (
-
- byte[] Messagebodybytes = "Hello world". GetBytes ();
- channel.basicpublish (
RABBITMQ three types of exchange