原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11792398.html
RMQ Direct
Project Directory
Maven Dependency
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.fool.rmq</groupId> 8 <artifactId>rmq</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.2.0.RELEASE</version> 15 </parent> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-amqp</artifactId> 21 </dependency> 22 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-test</artifactId> 31 <scope>test</scope> 32 </dependency> 33 </dependencies> 34 35 <build> 36 <plugins> 37 <plugin> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-maven-plugin</artifactId> 40 </plugin> 41 </plugins> 42 </build> 43 </project>
application.properties
1 spring.application.name=rmq 2 server.port=8888 3 4 spring.rabbitmq.host=localhost 5 spring.rabbitmq.port=5672 6 spring.rabbitmq.username=admin 7 spring.rabbitmq.password=admin 8 9 rmq.config.exchange=log.direct 10 rmq.config.queue.info=log.info 11 rmq.config.queue.info.routing.key=log.info.routing.key 12 rmq.config.queue.error=log.error 13 rmq.config.queue.error.routing.key=log.error.routing.key
Source Code
Application.java
1 package org.fool.rmq; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class Application { 8 public static void main(String[] args) { 9 SpringApplication.run(Application.class, args); 10 } 11 }
RmqConfig.java
1 package org.fool.rmq.config; 2 3 import org.springframework.amqp.core.Queue; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 7 @Configuration 8 public class RmqConfig { 9 10 @Bean 11 public Queue queue() { 12 return new Queue("hello-rmq"); 13 } 14 }
LogProducer.java
1 package org.fool.rmq.direct; 2 3 import org.springframework.amqp.core.AmqpTemplate; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.stereotype.Component; 7 8 import java.util.Date; 9 10 @Component 11 public class LogProducer { 12 @Autowired 13 private AmqpTemplate rmqTemplate; 14 15 @Value("${rmq.config.exchange}") 16 private String exchange; 17 18 @Value("${rmq.config.queue.info.routing.key}") 19 private String infoRoutingKey; 20 21 @Value("${rmq.config.queue.error.routing.key}") 22 private String errorRoutingKey; 23 24 public void send() { 25 String message = "Hello " + new Date(); 26 rmqTemplate.convertAndSend(exchange, infoRoutingKey, message); 27 rmqTemplate.convertAndSend(exchange, errorRoutingKey, message); 28 } 29 }
LogInfoConsumer.java
1 package org.fool.rmq.direct; 2 3 import org.springframework.amqp.core.ExchangeTypes; 4 import org.springframework.amqp.rabbit.annotation.Exchange; 5 import org.springframework.amqp.rabbit.annotation.Queue; 6 import org.springframework.amqp.rabbit.annotation.QueueBinding; 7 import org.springframework.amqp.rabbit.annotation.RabbitHandler; 8 import org.springframework.amqp.rabbit.annotation.RabbitListener; 9 import org.springframework.stereotype.Component; 10 11 @Component 12 @RabbitListener(bindings = @QueueBinding( 13 value = @Queue(value = "${rmq.config.queue.info}", autoDelete = "true"), 14 exchange = @Exchange(value = "${rmq.config.exchange}", type = ExchangeTypes.DIRECT), 15 key = "${rmq.config.queue.info.routing.key}" 16 )) 17 public class LogInfoConsumer { 18 @RabbitHandler 19 public void handle(String message) { 20 System.out.println("consume info: " + message); 21 } 22 }
LogErrorConsumer.java
1 package org.fool.rmq.direct; 2 3 import org.springframework.amqp.core.ExchangeTypes; 4 import org.springframework.amqp.rabbit.annotation.Exchange; 5 import org.springframework.amqp.rabbit.annotation.Queue; 6 import org.springframework.amqp.rabbit.annotation.QueueBinding; 7 import org.springframework.amqp.rabbit.annotation.RabbitHandler; 8 import org.springframework.amqp.rabbit.annotation.RabbitListener; 9 import org.springframework.stereotype.Component; 10 11 @Component 12 @RabbitListener(bindings = @QueueBinding( 13 value = @Queue(value = "${rmq.config.queue.error}", autoDelete = "true"), 14 exchange = @Exchange(value = "${rmq.config.exchange}", type = ExchangeTypes.DIRECT), 15 key = "${rmq.config.queue.error.routing.key}" 16 )) 17 public class LogErrorConsumer { 18 @RabbitHandler 19 public void handle(String message) { 20 System.out.println("consume error: " + message); 21 } 22 }
ApplicationTest.java
1 package org.fool.rmq.test; 2 3 import org.fool.rmq.Application; 4 import org.fool.rmq.direct.LogProducer; 5 import org.fool.rmq.producer.Producer; 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.boot.test.context.SpringBootTest; 10 import org.springframework.test.context.junit4.SpringRunner; 11 12 @RunWith(SpringRunner.class) 13 @SpringBootTest(classes = Application.class) 14 public class ApplicationTest { 15 16 @Autowired 17 private Producer producer; 18 19 @Autowired 20 private LogProducer logProducer; 21 22 @Test 23 public void test() { 24 producer.send(); 25 } 26 27 @Test 28 public void testDirect() { 29 logProducer.send(); 30 } 31 }