Producer End
1. Creation of Channel
No matter what kind of exchange is used, creating the channel code is the same, as follows
1 New connectionfactory (); 2 factory.sethost ("localhost"); 3 Connection Connection = factory.newconnection (); 4 Channel channel = Connection.createchannel ();
2. Exchange creation
2.1 Direct
Direct uses the default Exchange, does not need to be declared, and requires a specified message to be sent to that queue
false false false null);
2.2 Fanout
Channel.exchangedeclare (Exchange_name, "fanout")
2.3 Topic as follows
Channel.exchangedeclare (exchange_name, "topic");
3. Sending of messages
3.1 Direct
null, Message.getbytes ());
3.2 fanout
null, Message.getbytes ());
3.3 topic
null, Message.getbytes ());
Cusumer End
1. Create Channel
New connectionfactory (); Factory.sethost ("localhost"== Connection.createchannel ();
2, the preparation before the message
2.1 Direct binding queue for message consumption
false false false null);
2.2 fanout, you need to first specify the exchange type Fanout
Channel.exchangedeclare (Exchange_name, "fanout"="");
2.3 topic
Channel.exchangedeclare (exchange_name, "topic"=for(String bindingkey:argv) { Channel.queuebind (QueueName, Exchange_name, Bindingkey);}
3, the specific consumption message code is the same
Queueingconsumer consumer =NewQueueingconsumer (channel); Channel.basicconsume (Queue_name,true, consumer); while(true) {queueingconsumer.delivery Delivery=Consumer.nextdelivery (); Envelope ELP=Delivery.getenvelope (); String message=NewString (Delivery.getbody ()); System.out.println ("[x] Received '" + Message + "'"); Channel.basicack (Elp.getdeliverytag (),false); //Channel.basicnack ();?}
RABBITMQ direct/fanout/topic Three types of Exchange Java code comparison