RABBITMQ direct/fanout/topic Three types of Exchange Java code comparison

Source: Internet
Author: User
Tags rabbitmq

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

Related Article

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.