This article focuses on the following fanout types of exchange. Fanout, as the name implies, is like a fan blowing flour, blowing everywhere. If you use Fanout type of exchange, then routing key is not important. Because we do not have to specify routing key when sending messages to exchange, it sends a message to each queue that is bound to the exchange.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/E9/wKiom1cMls3yXUUyAADLVeCjZ4c024.png "title=" 1.png " alt= "Wkiom1cmls3yxuuyaadlvecjz4c024.png"/>
package com.jaeger.exchange.fanout;import java.io.ioexception;import Java.util.concurrent.timeoutexception;import org.junit.test;import com.rabbitmq.client.amqp;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.connectionfactory;import com.rabbitmq.client.consumer;import Com.rabbitmq.client.defaultconsumer;import com.rabbitmq.client.envelope;public class producer {private static final String MY_EXCHANGE_NAME = "Myexchange";p rivate static final string my_routing_key1 = "MyRoutingKey1";p rivate static final String MY_QUEUE_NAME1 = "MyQueue1";p rivate static final string my_ routing_key2 = "MyRoutingKey2";p rivate static final string my_queue_name2 = "MyQueue2";p Rivate static final string my_routing_key3 = "MyRoutingKey3";p rivate static final string my_queue_name3 = "MyQueue3" ;p rivate static final string fanout = "Fanout";p rivate static final String HOST = "172.19.64.21";p rivate static final string user = "Jaeger";p rivate static final string password = "root";p rivate static final int PORT = 5672; @Testpublic void createexchangeandqueue () Throws ioexception, timeoutexception {connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel ();// Create a fanout type of exchangechannel.exchangedeclarE (my_exchange_name, fanout);// creation of three queuechannel.queuedeclare (My_queue_name1, false, false, false, null); Channel.queuedeclare (My_queue_name2, false, false, false, null); Channel.queuedeclare (My_queue_name3, false, false, false, null);// creation of three routing Key, bind EXCHANGE and QUEUE together to Channel.queuebind (My_queue_name1, my_exchange_name, my_routing_key1); Channel.queuebind (My_queue_name2, my_exchange_name, my_routing_key2); Channel.queueBind (MY_QUEUE_ Name3, my_exchange_name, my_routing_key3); Channel.close (); Connection.close ();} @Testpublic void produce () throws ioexception, timeoutexception { Connectionfactory connectionfactory = new connectionfactory (); ConnectionFactory.setHost ( HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport ( PORT); Connection connection = connectionfactory.neWconnection (); Channel channel = connection.createchannel (); string message = "hello world!"; * Send a message to RABBITMQ. We specify the name of Exchange and Routing key, and RABBITMQ will go to exchange that has the name, and if it finds the Exchange is the fanout type, it will not see routing key, instead, put the message in all the queues that are bound to the exchange. Here we specify a routing key, but in fact there is no effect, we can also use an empty string, the last message is to reach all the queue. */channel.basicpublish (My_exchange_name, my_routing_key1, null, message.getbytes ("Utf-8"));// Channel.basicpublish (my_exchange_name, "", null, message.getbytes ("Utf-8")); System.out.println ("sent " + message + "'"); Channel.close (); Connection.close ();} @Testpublic void consume () throws IOException, TimeoutException, Interruptedexception{connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT); COnnection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); Consumer consumer = new defaultconsumer (channel) {@Overridepublic void Handledelivery (STRING CONSUMERTAG, ENVELOPE ENVELOPE, AMQP. Basicproperties properties,byte[] body) throws IOException {String message = new string (body, "UTF-8"); System.out.println ("received " + message + "'");}; Channel.basicconsume (My_queue_name1, true, consumer); Thread.Sleep (1000);}}
Let's run Createexchangeandqueue and bind the three queue to an fanout type of exchange:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7E/E6/wKioL1cMmeaQkMn6AACtQ0_qV_U108.png "style=" float: none; "title=" 2.PNG "alt=" Wkiol1cmmeaqkmn6aactq0_qv_u108.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/E9/wKiom1cMmTHg5JnyAACNkoXCD0w829.png "style=" float: none; "title=" 3.PNG "alt=" Wkiom1cmmthg5jnyaacnkoxcd0w829.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/E6/wKioL1cMmebz9wiPAAB04RuIxJA495.png "style=" float: none; "title=" 4.PNG "alt=" Wkiol1cmmebz9wipaab04ruixja495.png "/>
Run the produce method again to send the message to exchange for it to forward:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/E9/wKiom1cMmjuybAXXAAB7mAnSNSA105.png "title=" 5.PNG " alt= "Wkiom1cmmjuybaxxaab7mansnsa105.png"/>
We have seen that although we have specified a routing key, it is not actually used, or it can be replaced with "", and the message will be sent to each queue.
Finally we run the consume method and let it consume messages inside the MyQueue1 queue:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/E6/wKioL1cMnFaAcN-bAAB5I7JYHyM121.png "title=" 6.PNG " alt= "Wkiol1cmnfaacn-baab5i7jyhym121.png"/>
Can see the MyQueue1 inside the message was consumed.
This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1762983
RABBITMQ Introduction (iii)--FANOUT exchanger