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);
}