RMQ HelloWorld

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11791681.html

 

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>
View Code

 

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

 

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 }

 

Producer.java

 1 package org.fool.rmq.producer;
 2 
 3 import org.springframework.amqp.core.AmqpTemplate;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Component;
 6 
 7 import java.util.Date;
 8 
 9 @Component
10 public class Producer {
11     @Autowired
12     private AmqpTemplate rmqTemplate;
13 
14     public void send() {
15         String message = "Hello " + new Date();
16         rmqTemplate.convertAndSend("hello-rmq", message);
17     }
18 }

 

Consumer.java

 1 package org.fool.rmq.consumer;
 2 
 3 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public class Consumer {
 8     @RabbitListener(queues = "hello-rmq")
 9     public void handle(String message) {
10         System.out.println("consume: " + message);
11     }
12 }

 

ApplicationTest.java

 1 package org.fool.rmq.test;
 2 
 3 import org.fool.rmq.Application;
 4 import org.fool.rmq.producer.Producer;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.boot.test.context.SpringBootTest;
 9 import org.springframework.test.context.junit4.SpringRunner;
10 
11 @RunWith(SpringRunner.class)
12 @SpringBootTest(classes = Application.class)
13 public class ApplicationTest {
14 
15     @Autowired
16     private Producer producer;
17 
18     @Test
19     public void test() {
20         producer.send();
21     }
22 }

 

Console Output

 

RMQ Management

 

posted @ 2019-11-04 13:52  李白与酒  阅读(191)  评论(0编辑  收藏  举报