之前使用springboot2.0版本整合了ActiveMQ,閒來無事想將springboot版本更新到2.1.*結果出現了一些問題。
之前引用的ActiveMQ依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
升級後springboot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
先貼一下原來的配置代碼:
@Configuration
public class QueueConfig {
@Value("${queue}")
private String queue;
@Bean
public Queue logQueue() {
return new ActiveMQQueue(queue);
}
@Bean
public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue) {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setDeliveryMode(2);// 進行持久化配置 1表示非持久化,2表示持久化</span>
jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
jmsTemplate.setDefaultDestination(queue); // 此處可不設置默認,在發送消息時也可設置隊列
jmsTemplate.setSessionAcknowledgeMode(4);// 客戶端簽收模式</span>
return jmsTemplate;
}
// 定義一個消息監聽器連接工廠,這裏定義的是點對點模式的監聽器連接工廠
@Bean(name = "jmsQueueListener")
public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(
ActiveMQConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory);
// 設置連接數
factory.setConcurrency("1-10");
// 重連間隔時間
factory.setRecoveryInterval(1000L);
factory.setSessionAcknowledgeMode(4);
return factory;
}
}
只更改了springboot版本,啓動後發現報錯,錯誤信息如下:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method JmsTemplate in cn.zyf97.springbootactivemq.QueueConfig required a bean of type 'org.apache.activemq.ActiveMQConnectionFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
The following candidates were found but could not be injected:
- Bean method 'jmsConnectionFactory' in 'ActiveMQConnectionFactoryConfiguration.SimpleConnectionFactoryConfiguration' not loaded because @ConditionalOnProperty (spring.activemq.pool.enabled=false) found different value in property 'enabled'
- Bean method 'nonXaJmsConnectionFactory' in 'ActiveMQXAConnectionFactoryConfiguration' not loaded because @ConditionalOnBean (types: org.springframework.boot.jms.XAConnectionFactoryWrapper; SearchStrategy: all) did not find any beans of type org.springframework.boot.jms.XAConnectionFactoryWrapper
Action:
Consider revisiting the entries above or defining a bean of type 'org.apache.activemq.ActiveMQConnectionFactory' in your configuration.
ActiveMQConnectionFactory無法自動創建bean,pringboot-activemq-starter中的自定義配置ActiveMQConnectionFactoryConfiguration中注入了ActiveMQConnectionFactory,ActiveMQConnectionFactory在spring.activemq.pool.enabled=false時纔會創建,所以我們此時再注入ActiveMQConnectionFactory肯定在spring中找不到。
於是百度了下springboot2.1與activeMQ整合的相關文章,發現引用依賴是使用方法有所改變,這裏不詳細說明,把參考文章地址貼出:
http://blog.sina.com.cn/s/blog_7d1968e20102x1vm.html
https://www.jianshu.com/p/272925fe431d
更新了依賴引用,與配置方法:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
經過測試發現使用成功,後續刪掉了新增的依賴信息發現仍然成功,實際上只需變更連接工廠。