環境
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