环境
springboot 1.5.9 RELEASE
activeMQ 5.15.10
1. ActiveMQ下载启动
http://activemq.apache.org/download-archives.html ,本文用的是windows版的5.15.10版本,下载下来是压缩包,自行解压一个到目录下,CMD进入到解压目录下的bin目录下,执行 activemq.bat start 启动。
如果能成功访问http://localhost:8161/admin(用户名和密码默认为admin),则启动成功。
2.错误再现
在启动项目时直接报如下错误:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE)
2019-10-31 15:43:00.733 INFO 1940 --- [ main] c.example.provider.ProviderApplication : Starting ProviderApplication on my-THINK with PID 1940 (G:\lianxi\springboot\provider\target\classes started by my in G:\lianxi\springboot\provider)
2019-10-31 15:43:00.736 INFO 1940 --- [ main] c.example.provider.ProviderApplication : No active profile set, falling back to default profiles: default
2019-10-31 15:43:00.778 INFO 1940 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3c9d0b9d: startup date [Thu Oct 31 15:43:00 CST 2019]; root of context hierarchy
2019-10-31 15:43:01.453 INFO 1940 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2019-10-31 15:43:01.457 INFO 1940 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2019-10-31 15:43:01.465 INFO 1940 --- [ main] c.example.provider.ProviderApplication : Started ProviderApplication in 0.965 seconds (JVM running for 1.615)
2019-10-31 15:43:01.466 INFO 1940 --- [ Thread-4] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3c9d0b9d: startup date [Thu Oct 31 15:43:00 CST 2019]; root of context hierarchy
2019-10-31 15:43:01.466 INFO 1940 --- [ Thread-4] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 2147483647
2019-10-31 15:43:01.467 INFO 1940 --- [ Thread-4] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Process finished with exit code 0
错误解析
我项目中存在的错误原因在pom文件中导入了错误的web包引起
<!--错误引入的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!--正确的包-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--</dependency>-->
把错误的包去掉之后就可以正常启动:
正确的pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>
<!--web包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
activeMQ配置类
package com.example.provider.config;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
import javax.jms.Topic;
/**
* @Date 2019/10/31 14:22
* @Desc
*/
@Configuration
public class BeanConfig {
//定义存放消息的队列
@Bean
public Queue queue() {
return new ActiveMQQueue("ActiveMQQueue");
}
@Bean
public Topic topic() {
return new ActiveMQTopic("ActiveMQTOPIC");
}
}
yml文件
spring:
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
#true 表示使用内置的MQ,false则连接服务器
in-memory: false
#true表示使用连接池;false时,每发送一条数据创建一个连接
pool:
enabled: true
#连接池最大连接数
max-connections: 10
#空闲的连接过期时间,默认为30秒
idle-timeout: 30000
#强制的连接过期时间,与idleTimeout的区别在于:idleTimeout是在连接空闲一段时间失效,而expiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never
expiry-timeout: 0
server:
port: 9002