1. 程式人生 > >springboot整合activeMq 跳坑

springboot整合activeMq 跳坑

final model com red fas https from rom pac

安裝

  activeMq 安裝請看我的另一篇https://www.cnblogs.com/milicool/p/8420926.html

版本

  springboot 2.0.5.RELEASE

項目結構

技術分享圖片

POM.xml

我這裏開啟了activemq連接池, 畢竟管理一下連接才更合理

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.activemq</groupId>
 7     <artifactId>demo</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>demo</name>
12     <description>Demo project for Spring Boot activeMq</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>2.0.5.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-activemq</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-web</artifactId>
35         </dependency>
36 
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-starter-test</artifactId>
40             <scope>test</scope>
41         </dependency>
42 
43         <!-- activemq連接池 -->
44         <dependency>
45             <groupId>org.apache.activemq</groupId>
46             <artifactId>activemq-pool</artifactId>
47             <version>5.14.5</version>
48         </dependency>
49 
50         <!-- fastjson -->
51         <dependency>
52             <groupId>com.alibaba</groupId>
53             <artifactId>fastjson</artifactId>
54             <version>1.2.38</version>
55         </dependency>
56     </dependencies>
57 
58     <build>
59         <plugins>
60             <plugin>
61                 <groupId>org.springframework.boot</groupId>
62                 <artifactId>spring-boot-maven-plugin</artifactId>
63             </plugin>
64         </plugins>
65     </build>
66 </project>

application.yml

技術分享圖片

生產者

 1 /**
 2  * 生產者
 3  * @author milicool
 4  * Created on 2018/9/13
 5  */
 6 @Service
 7 public class Producer {
 8 
 9     /** JmsMessagingTemplate是對jmsTemplate的封裝 */
10     @Autowired
11     private JmsMessagingTemplate jmsTemplate;
12 
13     /** 這裏參數用Queue更好 */
14     public void sendTestMessage(Queue queue, final String message) {
15         jmsTemplate.convertAndSend(queue, message);
16     }
17 }

消費者

技術分享圖片消費者

測試類

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
    private Logger log = LoggerFactory.getLogger(DemoApplicationTests.class);

    @Autowired
    private Producer producer;

    @Test
    public void contextLoads() {
        Queue queue = new ActiveMQQueue("spring_queue_test");
        for (int i = 0; i < 5; i++) {
            String msg = "hello world, 序號: " + i;
            producer.sendTestMessage(queue, msg);
            log.info("發送隊列, msg: {}" + msg);
        }
    }
}

結果

技術分享圖片

感謝觀看哦

技術分享圖片

springboot整合activeMq 跳坑