之前使用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>
先贴一下原来的配置代码:
@ConfigurationpublicclassQueueConfig{
@Value("${queue}")private String queue;
@Beanpublic QueuelogQueue(){returnnewActiveMQQueue(queue);}
@Beanpublic JmsTemplatejmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue){
JmsTemplate jmsTemplate=newJmsTemplate();
jmsTemplate.setDeliveryMode(2);// 进行持久化配置 1表示非持久化,2表示持久化</span>
jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
jmsTemplate.setDefaultDestination(queue);// 此处可不设置默认,在发送消息时也可设置队列
jmsTemplate.setSessionAcknowledgeMode(4);// 客户端签收模式</span>return jmsTemplate;}// 定义一个消息监听器连接工厂,这里定义的是点对点模式的监听器连接工厂
@Bean(name="jmsQueueListener")public DefaultJmsListenerContainerFactoryjmsQueueListenerContainerFactory(
ActiveMQConnectionFactory activeMQConnectionFactory){
DefaultJmsListenerContainerFactory factory=newDefaultJmsListenerContainerFactory();
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>
经过测试发现使用成功,后续删掉了新增的依赖信息发现仍然成功,实际上只需变更连接工厂。