RABBITMQ Learning Series (iv): Several exchange modes

Source: Internet
Author: User

On an article, the specific use of RabbitMQ, you can look at this article: RabbitMQ Learning Series (iii): How C # uses RabbitMQ. Say something about the theory today, several modes of Exchange.

The core idea in the AMQP protocol is that producers and consumers are isolated, and producers never send messages directly to the queue. Producers often do not know whether a message will be sent to the queue, just send the message to a switch. Received first by Exchange, and then exchange is forwarded to the queue for storage by a specific policy. Similarly, the same is true of consumers. Exchange is similar to a switch that forwards individual messages to the appropriate queue.

RABBITMQ provides four types of exchange modes: Fanout,direct,topic,header. The header pattern is less in practice, compared with the first three modes in this article.

  I. Fanout Exchange

 All messages sent to Fanout Exchange are forwarded to all the queue with that Exchange binding (binding).

Fanout Exchange does not need to process routekey. You simply need to bind the queue to exchange. This way, messages sent to Exchange are forwarded to all the queues that are bound to the switch. Similar to subnet broadcasts, each host in the network receives a copy of the message.

Therefore, Fanout Exchange forwarding messages are the fastest.

 // <summary>        ///producers/// </summary>        /// <param name= "Change" ></param>        Private Static voidproducermessage (mymessage msg) {varAdvancedbus =Createadvancedbus (); if(advancedbus.isconnected) {varExchange = Advancedbus.exchangedeclare ("User", exchangetype.fanout); Advancedbus.publish (Exchange,"",false,NewMessage<mymessage>(msg)); }            Else{Console.WriteLine ("Can ' t connect"); }        }        /// <summary>        ///Consumer/// </summary>        Private Static voidConsumemessage () {varAdvancedbus =Createadvancedbus (); varExchange = Advancedbus.exchangedeclare ("User", exchangetype.fanout); varQueue = Advancedbus.queuedeclare ("User.notice.wangwu"); Advancedbus.bind (Exchange, queue,"User.notice.wangwu"); Advancedbus.consume (Queue, registration={registration. ADD<MyMessage> (message, info) = {Console.WriteLine ("Body: {0}", message. Body);            });        }); }

  

Two. Direct Exchange

All messages sent to direct Exchange are forwarded to the queue specified in Routekey.

Direct mode, you can use RABBITMQ's own Exchange:default Exchange. Therefore, you do not need to bind exchange for any binding (binding) operations. when the message is delivered, the Routekey must match exactly to be received by the queue, or the message will be discarded.

     /// <summary>        ///producers/// </summary>        /// <param name= "Change" ></param>        Private Static voidproducermessage (mymessage msg) {varAdvancedbus =Createadvancedbus (); if(advancedbus.isconnected) {varQueue = Advancedbus.queuedeclare ("User.notice.zhangsan"); Advancedbus.publish (Exchange.getdefault (), queue. Name,false,NewMessage<mymessage>(msg)); }            Else{Console.WriteLine ("Can ' t connect"); }        }        /// <summary>        ///Consumer/// </summary>        Private Static voidConsumemessage () {varAdvancedbus =Createadvancedbus (); varExchange = Advancedbus.exchangedeclare ("User", Exchangetype.direct); varQueue = Advancedbus.queuedeclare ("User.notice.lisi"); Advancedbus.bind (Exchange, queue,"User.notice.lisi"); Advancedbus.consume (Queue, registration={registration. ADD<MyMessage> (message, info) ={Console.WriteLine ("Body: {0}", message.                Body);            });        }); }

  Three. Topic Exchange

All messages sent to topic Exchange are forwarded to all queues that are concerned with the topic specified in Routekey.

Exchange will Routekey and a topic for fuzzy matching. At this point the queue needs to bind a topic. You can use wildcard characters for fuzzy matching, the symbol "#" matches one or more words, and the symbol "*" matches not many words. So "log.#" is able to match to "Log.info.oa", but "log.*" matches only to "Log.error".

Therefore, Topic Exchange is very flexible to use.

 // <summary>        ///producers/// </summary>        /// <param name= "Change" ></param>        Private Static voidproducermessage (mymessage msg) {////Create message busIBus bus =Createbus (); Try{bus. Publish (msg, x=x.withtopic (Msg.            Messagerouter)); }            Catch(Easynetqexception ex) {//handling Connection Message Server exceptions} bus. Dispose ();//similar to database connection, remember to destroy bus objects after use        }        /// <summary>        ///Consumer/// </summary>        Private Static voidconsumemessage (mymessage msg) {////Create message busIBus bus =Createbus (); Try{bus. Subscribe<MyMessage> (Msg. Messagerouter, message = Console.WriteLine (Msg. MessageBody), x = X.withtopic ("user.notice.#")); }            Catch(Easynetqexception ex) {//handling Connection Message Server exceptions            }        }

  This is the actual use of RABBITMQ a few scenes, familiar with this, basically RABBITMQ also understand. Http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

At this point, RABBITMQ several exchange modes have been introduced, the actual use of the process, we will be more different scenarios to use different exchange patterns.

View RABBITMQ Series other articles, http://www.cnblogs.com/zhangweizhong/category/855479.html

RABBITMQ Learning Series (iv): Several exchange modes

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.