spring boot version: 2.1.10.RELEASE
This article involves two projects, rabbitmq topic provider and rabbitmq topic consumer, with the same dependency and configuration.
Dependent dependence
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Related configuration
#rabbitmq configuration spring.rabbitmq.host=192.168.xxx.xxx spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin spring.rabbitmq.virtual-host=/ #Set up the switch(Custom variable) mq.config.exchange=log.topic mq.config.queue.info=log.info mq.config.queue.error=log.error mq.config.queue.logs=log.msg
Rabbitmq topic provider project
(1) Order message sender
package com.ebook.rabbitmq; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author:JZ * @date:2020/2/2 */ @Component public class OrderSender { @Value("${mq.config.exchange}") private String exchange; @Autowired private AmqpTemplate rabbitTemplate; public void send() { this.rabbitTemplate.convertAndSend(this.exchange, "order.log.debug", "order.log.debug......."); this.rabbitTemplate.convertAndSend(this.exchange, "order.log.info", "order.log.info......."); this.rabbitTemplate.convertAndSend(this.exchange, "order.log.warn", "order.log.warn......."); this.rabbitTemplate.convertAndSend(this.exchange, "order.log.error", "order.log.error......."); } }
(2) Product message sender
package com.ebook.rabbitmq; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author:JZ * @date:2020/2/2 */ @Component public class ProductSender { @Value("${mq.config.exchange}") private String exchange; @Autowired private AmqpTemplate rabbitTemplate; public void send() { this.rabbitTemplate.convertAndSend(this.exchange, "product.log.debug", "product.log.debug......."); this.rabbitTemplate.convertAndSend(this.exchange, "product.log.info", "product.log.info......."); this.rabbitTemplate.convertAndSend(this.exchange, "product.log.warn", "product.log.warn......."); this.rabbitTemplate.convertAndSend(this.exchange, "product.log.error", "product.log.error......."); } }
(3) User message sender
package com.ebook.rabbitmq; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; /** * @author:JZ * @date:2020/2/2 */ @Component public class UserSender { @Value("${mq.config.exchange}") private String exchange; @Autowired private AmqpTemplate rabbitTemplate; public void send() { this.rabbitTemplate.convertAndSend(this.exchange, "user.log.debug", "user.log.debug......."); this.rabbitTemplate.convertAndSend(this.exchange, "user.log.info", "user.log.info......."); this.rabbitTemplate.convertAndSend(this.exchange, "user.log.warn", "user.log.warn......."); this.rabbitTemplate.convertAndSend(this.exchange, "user.log.error", "user.log.error......."); } }
(4) Test class
import com.ebook.rabbitmq.OrderSender; import com.ebook.rabbitmq.ProductSender; import com.ebook.rabbitmq.RabbitmqTopicProvider; import com.ebook.rabbitmq.UserSender; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = RabbitmqTopicProvider.class) public class RabbitMqTopicApplicationTests { @Autowired private UserSender userSender; @Autowired private ProductSender productSender; @Autowired private OrderSender orderSender; @Test public void send() throws InterruptedException { this.userSender.send(); this.productSender.send(); this.orderSender.send(); } }
Rabbitmq topic consumer project
(1) Info recipient
package com.ebook.rabbitmq; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.rabbit.annotation.*; import org.springframework.stereotype.Component; /** * @author:JZ * @date:2020/2/2 */ @Component @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "${mq.config.queue.info}", autoDelete = "true"),//autoDelete indicates that the queue is temporary exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC), key = "*.log.info" )) public class InfoReciver { @RabbitHandler public void process(String msg) { System.out.println("Receive INFO Journal:" + msg); } }
(2) Error message receiver
package com.ebook.rabbitmq; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.rabbit.annotation.*; import org.springframework.stereotype.Component; /** * @author:JZ * @date:2020/2/2 */ @Component @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "${mq.config.queue.error}", autoDelete = "true"),//autoDelete indicates that the queue is temporary exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC), key = "*.log.error" )) public class ErrorReciver { @RabbitHandler public void process(String msg) { System.out.println("Receive ERROR Journal:" + msg); } }
(3) All message recipients
package com.ebook.rabbitmq; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.rabbit.annotation.*; import org.springframework.stereotype.Component; /** * @author:JZ * @date:2020/2/2 */ @Component @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "${mq.config.queue.logs}", autoDelete = "true"),//autoDelete indicates that the queue is temporary exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC), key = "*.log.*" )) public class LogsReciver { @RabbitHandler public void process(String msg) { System.out.println("Receive ALL Journal:" + msg); } }
Operation result
Run the rabbitmq topic consumer project and run the test class.
Result:
INFO log received: user.log.info INFO log received: product.log.info INFO log received: order.log.info ERROR log received: user.log.error ERROR log received: product.log.error ERROR log received: order.log.error Received ALL log: user.log.debug Received ALL log: user.log.info Received ALL log: user.log.warn Received ALL log: user.log.error Received ALL log: product.log.debug Received ALL log: product.log.info Received ALL log: product.log.warn Received ALL log: product.log.error Received ALL log: order.log.debug Received ALL log: order.log.info Received ALL log: order.log.warn Received ALL log: order.log.error