一、环境
- Idea 2020.1
- JDK 1.8
- maven
二、目的
spring boot 通过整合activemq
gitHub地址: https://github.com/ouyushan/ouyushan-spring-boot-samples
三、步骤
3.1、点击File -> New Project -> Spring Initializer,点击next
3.2、在对应地方修改自己的项目信息
3.3、选择Web依赖,选中Spring Web、Spring Boot DevTools、Spring for Apache ActiveMQ 5。可以选择Spring Boot版本,本次默认为2.3.0,点击Next
3.4、项目结构
四、添加文件
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ouyushan</groupId>
<artifactId>ouyushan-spring-boot-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.ouyushan</groupId>
<artifactId>spring-boot-mq-activemq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mq-activemq</name>
<description>ActiveMQ project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties文件
# ACTIVEMQ (ActiveMQProperties)
spring.activemq.in-memory=true
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.password=admin
spring.activemq.user=admin
spring.activemq.packages.trust-all=false
#spring.activemq.packages.trusted=
#spring.activemq.pool.configuration.*=
spring.activemq.pool.enabled=false
spring.activemq.pool.idleTimeout=0
spring.activemq.pool.idle-timeout=30000
spring.activemq.pool.max-connections=1
激活Jms并配置消息队列
JmsConfiguration.java
package org.ouyushan.springboot.mq.activemq.config;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import javax.jms.Queue;
/**
* @Description: JMS 配置队列
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/6/4 15:16
*/
@Configuration
@EnableJms
public class JmsConfiguration {
@Bean
public Queue queue() {
return new ActiveMQQueue("ouyushan.queue");
}
}
mq消费监听器
ActiveMQConsumerListener.java
package org.ouyushan.springboot.mq.activemq.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
/**
* @Description: 消费监听
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/6/4 15:26
*/
@Component
public class ActiveMQConsumerListener {
private static Logger logger = LoggerFactory.getLogger(ActiveMQConsumerListener.class);
/* 自动注入JmsConfiguration中定义的queue (ouyushan.queue)*/
@Autowired
private Queue queue;
/* 接收消息 */
@JmsListener(destination = "ouyushan.queue")
public void receiveQueue(String text) {
logger.info("接受到:" + text);
}
}
通过接口调用模拟mq生产者
ActiveMQProducerController.java
package org.ouyushan.springboot.mq.activemq.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
/**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/6/4 15:31
*/
@RestController
public class ActiveMQProducerController {
private static Logger logger = LoggerFactory.getLogger(ActiveMQProducerController.class);
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/* 自动注入JmsConfiguration中定义的queue (ouyushan.queue)*/
@Autowired
private Queue queue;
/* 发送消息 */
@RequestMapping(value = "/sendMsg")
public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
logger.info("msg:" + msg);
}
}
五、测试
SpringBootMqActivemqApplicationTests.java
package org.ouyushan.springboot.mq.activemq;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class SpringBootMqActivemqApplicationTests {
@Test
public void testMQProducer(@Autowired MockMvc mockMvc) throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/sendMsg?msg=value")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print());
}
}
执行测试方法后,控制台打印:
2020-06-04 16:32:00.008 INFO 2732 --- [enerContainer-1] o.o.s.b.m.a.l.ActiveMQConsumerListener : 接受到:value
2020-06-04 16:32:00.017 INFO 2732 --- [ main] o.o.s.b.m.a.c.ActiveMQProducerController : msg:value
同时通过用户 admin/admin
访问 http://localhost:8161/admin/queues.jsp
也能查看相关信息