1. Contents of articles

· Summary - Article directory - Project structure. Start using - Add POM package - Configuration file Editing - Queue Configuration - Sender class editing - Receiver class editing - Test class editing - Results show - Results show - Sending and receiving of custom objects - Use of Topic Exchange -Fanout Use of Exchange · article recommendations - a source code interpretation - a video tutorialCopy the code

2. The key classes are circled in the project structure diagram

Two, start to use



spring-boot-starter-amqp

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Copy the code

2. Edit the configuration file to configure the IP address, port, user name, password, and path of the MQ server

Spring. The rabbitmq. Host = 127.0.0.1 spring. The rabbitmq. Port = 5672 spring. The rabbitmq. Username = guest spring. The rabbitmq. Password = guest spring.rabbitmq.virtual-host=/Copy the code

The diagram below:

@Bean
public Queue Queue(){// Parameters can include attributes for queues and persistence Settings, as with switchesreturn new Queue("hello"); // The default switch is used, so there is no need to declare and bind.Copy the code


@Autowired private AmqpTemplate rabbitTemplate; Public Boolean SendMessage(String p_Message){try {Integer I = 0; // Declare a flagwhile (true){// loop system.out.println ("Sender : " + p_Message + i);
                this.rabbitTemplate.convertAndSend("hello", p_Message + i); // argument: routingKey, message content i++; Thread.sleep(5000); }} catch (Exception ex){return  false; }}Copy the code

Queues = @rabbitListener (Queues =); // Queues = @rabbitListener (Queues =)"hello")
@RabbitHandler
public void process(String p_Message) {
    System.out.println("Receiver : " + p_Message);
}
Copy the code


@Autowired
RabbitMqSendServices rabbitMqSendServices;
@Test
void sendMsg(){
	rabbitMqSendServices.SendMessage("hello"); // Parameters: what to send}Copy the code

@RabbitListener(queues = "hello")


Three, advanced use


Public Boolean SendMessage(String p_RoutingKey){try {Integer I = 0; // Declare a flagwhile (true){// loop to send Department Department = new Department(); // Declare the object department.setName("han");
            department.setId(i);
            System.out.println("Sender : "+ department); this.rabbitTemplate.convertAndSend(p_RoutingKey,department); // Arguments: routingKey, message content thread.sleep (5000); // i++; } } catch (Exception ex){return  false; }} @rabbitListener (queues ="objectQueue")
public void obQueueProcess(Department p_Department) {
    System.out.println("Receiver : "+ p_Department.getName() + p_Department.getId()); } // Define @bean public Queue in the configuration classObQueue() {return new Queue("objectQueue");
}
@Bean
public MessageConverter messageConverter(){// Declare a Json converter, which is crucialreturnnew Jackson2JsonMessageConverter(); } // Test class @test voidsendObMsg(){
	rabbitMqSendServices.SendMessage("objectQueue"); } public class Department {private Integer ID; private String name; public IntegergetId() {return  id;
    }
    public String getName() {return name;
    }
    public void setId(Integer id){
        this.id = id;
    }
    public void setName(String name){ this.name = name; }}Copy the code

2. Usage of Topic Exchange · Profile definition

// Define queue name Final static String topicQueueOne ="topic.queueOne";
final static String topicQueueTwo = "topic.queueTwo";

@Bean
public Queue queueTopicQueueOne() {returnnew Queue(topicQueueOne); // Parameter: Queue name} @bean public QueuequeueTopicQueueTwo() {return new Queue(topicQueueTwo);
}

@Bean
TopicExchange topicExchange() {return new TopicExchange("topicExchange_HanTest"); // Parameters: switch name} Queue, switch, routingKey@bean Binding bindingExchangeQueueOne(Queue queueTopicQueueOne, TopicExchange TopicExchange){return BindingBuilder.bind(queueTopicQueueOne).to(topicExchange).with("topic.message");
}
@Bean
Binding bindingExchangeQueueTwo(Queue queueTopicQueueTwo, TopicExchange topicExchange){
    return BindingBuilder.bind(queueTopicQueueTwo).to(topicExchange).with("topic.#");
}
Copy the code

The sender,

/ / parameters: Switch name, RoutingKey, Public Boolean sendMessage(String p_ExchangeName,String p_RoutingKey,String p_Message){try {Integer I = 0; // Declare a flagwhile (trueString MSG = p_Message + I; System.out.println("Sender : "+msg); rabbitTemplate.convertAndSend(p_ExchangeName,p_RoutingKey,msg); i++; Thread.sleep(5000); }} catch (Exception ex){return  false; }}Copy the code

The receiver,

@RabbitListener(queues = "topic.queueOne")
public void queueOneProcess(String p_Message){
    System.out.println("Receiver One : " + p_Message);
}
@RabbitListener(queues = "topic.queueTwo")
public void queueTwoprocess(String p_Message){
    System.out.println("Receiver Two : " + p_Message);
}
Copy the code

The test class,

@Test
void sendTopicMsg1(){
	rabbitMqSendServices.sendMessage("topicExchange_HanTest"."topic.message"."salahei");
}
@Test
void sendTopicMsg2(){
	rabbitMqSendServices.sendMessage("topicExchange_HanTest"."topic.other"."salahei");
}
Copy the code

3. Use of Fanout Exchange · Profile definition

// declare @bean public QueuefanoutQueueOne() {return new Queue("fanoutOne");
}
@Bean
public Queue fanoutQueueTwo() {return new Queue("fanoutTwo");
}
@Bean
public Queue fanoutQueueThree() {return new Queue("fanoutThree"); } // Declare the switch @bean FanoutExchangefanoutExchange() {return new FanoutExchange("fanoutExchange_HanTest"); } / / bind Queue @ beans Binding bindingExchangeOne (Queue fanoutQueueOne, FanoutExchange FanoutExchange) {return BindingBuilder.bind(fanoutQueueOne).to(fanoutExchange);
}
@Bean
Binding bindingExchangeTwo(Queue fanoutQueueTwo,FanoutExchange fanoutExchange){
    return BindingBuilder.bind(fanoutQueueTwo).to(fanoutExchange);
}
@Bean
Binding bindingExchangeThree(Queue fanoutQueueThree,FanoutExchange fanoutExchange){
    return BindingBuilder.bind(fanoutQueueThree).to(fanoutExchange);
}
Copy the code

The sender,

// Parameters: switch name, message -- // Note: Public Boolean sendMessage(String p_ExchangeName,String p_Message){try {Integer I = 0; // Declare a flagwhile (trueString MSG = p_Message + I; System.out.println("Sender : " +msg);
            rabbitTemplate.convertAndSend(p_ExchangeName,"",msg); i++; Thread.sleep(5000); }} catch (Exception ex){return  false; }}Copy the code

The receiver,

@RabbitListener(queues = "fanoutOne")
public void fanoutProcessOne(String p_Message){
    System.out.println("Receiver : "+ p_Message); }} @rabbitListener (queues = {"fanoutTwo"."fanoutThree"})
public void fanoutProcessThree(String p_Message){
    System.out.println("Receiver : " + p_Message);
}
Copy the code

The test class,

@Test
void sendFanoutMsg(){
	rabbitMqSendServices.sendMessage("fanoutExchange_HanTest"."salahei");
}
Copy the code

JinchaoLv SpringBoot tutorial – a detailed learning video from code base – RabbitListener implementation process source code interpretation