Spring Boot Consolidation RABBITMQ fanout Exchange Mode (iii)

Source: Internet
Author: User
Tags rabbitmq

Absrtact: The direct mode (i) topic forwarding mode has been introduced before (b), this time fanout Exchange form is called broadcast form, so the message we send to the router causes every queue bound to the router to receive the message, This time, even if the key is specified, or the rule (that is, the Convertandsend method in the above parameter 2), will also be ignored! Then directly on the code, the sender configuration is as follows:


Package com.micai.springboot.mq.config;
Import Com.micai.springboot.base.BaseConfig;
Import org.springframework.amqp.core.*;
Import Org.springframework.beans.factory.annotation.Qualifier;
Import Org.springframework.context.annotation.Bean;

Import org.springframework.context.annotation.Configuration; /** * Description: Topic forwarding mode * <p> * Author: Shong * DATE:2017/11/3 18:51 * * @Configuration public class senderconf Extend s Baseconfig {//----------------------------------------------------Direct form------------------------------------
    --------///* @Bean public Queue queue () {return new queue (Queue_key); 

    }*///----------------------------------------------------Topic form--------------------------------------------//
    /* @Bean (name = ' message ') public Queue queuemessage () {return new queue ("Topic.message");
    @Bean (name = "Messages") public Queue queuemessages () {return new queue ("Topic.messages"); } @BeaN Public topicexchange Exchange () {return new Topicexchange ("Exchange");
        @Bean Binding bindingexchangemessage (@Qualifier ("message") Queue Queuemessage, Topicexchange Exchange) {
    Return Bindingbuilder.bind (Queuemessage). to (Exchange). With ("Topic.message");
        @Bean Binding bindingexchangemessages (@Qualifier ("messages") Queue Queuemessages, Topicexchange Exchange) { Return Bindingbuilder.bind (Queuemessages). to (Exchange). With ("topic.#");//* denotes a word, #表示零个或多个词}*///----------- ---------------------------------fanout Exchange Form-------------------------------------------//@Bean (name=) Amessage ") Public Queue amessage () {return new queue (" Fanout.
    A "); @Bean (name= "bmessage") public \ Bmessage () {return new queue ("Fanout.
    B "); @Bean (name= "cmessage") public \ Cmessage () {return new queue ("Fanout.
    C ");
      } @Bean fanoutexchange Fanoutexchange () {  return new Fanoutexchange ("Fanoutexchange");/configure broadcast Router} @Bean Binding Bindingexchangea (@Qualifier ("Amessage
    ") Queue Amessage,fanoutexchange fanoutexchange) {return Bindingbuilder.bind (amessage). to (Fanoutexchange);
        @Bean Binding Bindingexchangeb (@Qualifier ("Bmessage") Queue bmessage, Fanoutexchange fanoutexchange) {
    Return Bindingbuilder.bind (Bmessage). to (Fanoutexchange);
        @Bean Binding Bindingexchangec (@Qualifier ("Cmessage") Queue cmessage, Fanoutexchange fanoutexchange) {
    Return Bindingbuilder.bind (Cmessage). to (Fanoutexchange);
 }

}


The sending end is sent using the following code:

Package com.micai.springboot.mq;
Import Com.micai.springboot.base.BaseConfig;
Import Com.micai.springboot.entity.User;
Import Org.springframework.amqp.core.AmqpTemplate;
Import org.springframework.beans.factory.annotation.Autowired;

Import org.springframework.stereotype.Component;  /** * Description: Message producer * <p> * Author: Shong * DATE:2017/11/3 15:37 * * @Component public class Sender extends Baseconfig

    {@Autowired private amqptemplate rabbittemplate; public void Send () {//----------------------------------------------------Direct form--------------------------- -----------------////In the producer, we will produce a string and send it to the queue named Hello/*string context = "Hello" + "Rabbit MQ."
        ";
        SYSTEM.OUT.PRINTLN ("Send MQ message:" + context); This.rabbitTemplate.convertAndSend (Queue_key, context); *//Send object, but the object must implement Serializable interface/*user User = n EW User ();
        Realize serializable interface User.setid (1L);
        User.setname ("John"); This.rabbittemplate. Convertandsend (Queue_key, user);///----------------------------------------------------Topic form------------ --------------------------------///*this.rabbittemplate.convertandsend ("Exchange", "Topic.message", "Hello, Rabb It! "); *///--------------------------------------------fanout Exchange Form-------------------------------------------
 This.rabbitTemplate.convertAndSend ("Fanoutexchange", "", "Xixi,hlhdidi");//Parameter 2 will be ignored}}

The Receive-side listener configuration is as follows:

Package com.micai.springboot.mq;
Import Com.micai.springboot.base.BaseConfig;
Import Com.micai.springboot.entity.User;
Import Org.springframework.amqp.rabbit.annotation.RabbitListener;

Import org.springframework.stereotype.Component;
 /** * Description: Message consumer * @RabbitListener Note defines this class for the Hello queue, * and uses @rabbithandler annotations to specify how to handle the message. * So, the consumer realizes the consumption of the Hello queue, the consumption operation is the string content of the output message * Author: Shong * DATE:2017/11/3 15:42 * * @Component public class Receiver Exten DS Baseconfig {//----------------------------------------------------Direct form----------------------------------- ---------////listener listens on the specified QUEUE/* @RabbitListener (queues = queue_key) public void process (String str) {S
    YSTEM.OUT.PRINTLN ("Receive MQ message:" + str);
        }*/////listener listens for specified QUEUE/* @RabbitListener (queues = queue_key) public void process (user user) {//user as parameter
    SYSTEM.OUT.PRINTLN ("Receive MQ message:" + user); }*///----------------------------------------------------Topic form--------------------------------------------//////////////@RabbitListener (queues= "topic.message")///listener listens for specified queue public void Process1 (String
    STR) {System.out.println ("message:" +STR); @RabbitListener (queues= "topic.messages")//listener listens on the specified queue public void Process2 (String str) {SYSTEM.O
    UT.PRINTLN ("messages:" +STR); 
    }*///--------------------------------------------fanout Exchange Form-------------------------------------------// @RabbitListener (queues= "fanout.
    A ") public void Processa (String str1) {System.out.println (" Receivea: "+STR1); } @RabbitListener (queues= "fanout.
    B ") public void Processb (String str) {System.out.println (" RECEIVEB: "+str); } @RabbitListener (queues= "fanout.
    C ") public void Processc (String str) {System.out.println (" Receivec: "+str);
 }

}

Run the test code, found that three listeners have received the data, the test succeeded, the results are as follows:



Source code Download Address: https://gitee.com/micai/micai-springboot/tree/master/micai-springboot-rabbitmq-7



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.