Spring boot consolidates RABBITMQ's Fanout Exchange broadcast mode

Source: Internet
Author: User
Tags rabbitmq

Creating Queues and switches

Package com.yijiupi.login.queue;

Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.amqp.core.Binding;
Import Org.springframework.amqp.core.BindingBuilder;
Import Org.springframework.amqp.core.FanoutExchange;
Import Org.springframework.amqp.core.Queue;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

/**
* RabbitMQ broadcast mode, do not process rout key broadcast to all the queues bound to it
* @author Zly
*/
@Configuration
public class Rabbitmqfanoutconfig {

private static final Logger Logger = Loggerfactory.getlogger (Rabbitmqfanoutconfig.class);

/**
* Create a 11th broadcast queue
* @author Zly
* @return Queue
*/
@Bean
Public Queue Thefirstonebroadcastqueue () {
Logger.info ("created thefirstonebroadcastqueue queue");
return new Queue ("Thefirstonebroadcastqueue");
}

/**
* Create a second broadcast queue
* @author Zly
* @return Queue
*/
@Bean
Public Queue Thesecondonebroadcastqueue () {
Logger.info ("created thesecondonebroadcastqueue queue");
return new Queue ("Thesecondonebroadcastqueue");
}

/**
* Create a broadcast router named Fanoutexchange
* @author Zly
* @return Fanoutexchange
* */
@Bean
Public Fanoutexchange Fanoutexchange () {
Logger.info ("created fanoutexchange switch");
return new Fanoutexchange ("Fanoutexchange");
}

/**
* Bind the Thefirstonebroadcastqueue queue to the route Fanoutexchange
* @author Zly
* @return Binding
* */
@Bean
Public Binding thefirstonebinding () {
Logger.info ("Bind thefirstonebroadcastqueue queue to switch Fanoutexchange");
Return Bindingbuilder.bind (Thefirstonebroadcastqueue ()). to (Fanoutexchange ());
}

/**
* Bind the Thesecondonebroadcastqueue queue to the switch fanoutexchange
* @author Zly
* @return Binding
* **/
@Bean
Public Binding thesecondonebinding () {
Logger.info ("Bind thesecondonebroadcastqueue queue to switch Fanoutexchange");
Return Bindingbuilder.bind (Thesecondonebroadcastqueue ()). to (Fanoutexchange ());
}
}

Consumers

/**
* First RABBITMQ broadcast mode consumer Thefirstonebroadcastcustomer listens for messages in Thefirstonebroadcastqueue queue and consumes
* @author Zly
* @param String
* */
@RabbitListener (queues = "Thefirstonebroadcastqueue")
@RabbitHandler
public void Thefirstonebroadcastcustomer (String receptionstring) {
String now_time = Timeutil.getnowtime ();
Logger.info ("The first broadcast mode of the consumer in the" +now_time+ "from Thefirstonebroadcastqueue to obtain the message is" +receptionstring);
}

/**
* The second broadcast mode consumer Thesecondonebroadcastcustomer listens to messages in the Thesecondonebroadcastqueue queue and consumes
* */
@RabbitListener (queues = "Thesecondonebroadcastqueue")
@RabbitHandler
public void Thesecondonebroadcastcustomer (String receptionstring) {
String now_time = Timeutil.getnowtime ();
Logger.info ("The second broadcast mode of the consumer in the" +now_time+ "from Thesecondonebroadcastqueue to obtain the message is" +receptionstring);
}


Producers

/**
     * Send data from controller to switch Fanoutexchange
     * @author zly
     * @param String
     * */
    public void Sendmassagetofanoutexchange (String userinput) {
        logger.info (" The message content sent to the switch Fanoutexchange is "+userinput";
       //The middle is to set the routing rules, because it is broadcast mode, this rule will be discarded, but this field must be written,
        //If not written on will cause the switch to forward the content as if the routing rules are discarded, causing the consumer to hear the queue without data
         amqptemplate.convertandsend ("Fanoutexchange", "This is the field to be discarded", userinput);
   }

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.