springboot+activemq集成过程记录
本机window系统,已安装activemq
在用springboot集成activemq的时候,用到了消息队列连接池,我的springboot版本信息如下:
maven相关依赖如下:
activemq配置类,关于队列配置的
application.yml文件配置如下:
接着写了一个简单的发送消息以及消费消息的例子,生产者与消费者相关代码如下:
消费者代码片段:
当我在启动springboot入口类的时候,控制台报错了,报错信息如下:
Field jmsMessagingTemplate in com.alen.active.ptp.PtpSender required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' 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 'jmsMessagingTemplate' in 'JmsAutoConfiguration.MessagingTemplateConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration did not match
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' in your configuration.
字面意思就是对象jmsMessagingTemplate不能注入,注入的对象与spring库里面的不匹配,然后我截取错误日志在网上搜了下,很多都说是我配置文件里面末尾有空格,但我检查了我配置文件,确认是没有空格的,因此这种情况对我没有帮助。随机点开某一篇文章里面,使我豁然开朗起来,正解在这里:
通过比对springboot2.0以下和2.1以上自动化配置源码得知,很明显我们看的出来,2.1以上和2.0以下版本使用消息队列连接池时候的连接池对象不一样:
2.0+以下版本使用的是PooledConnectionFactory,它存在于org.apache.activemq.pool.PooledConnectionFactory
2.1+版本使用的是JmsPoolConnectionFactory,它存在于org.messaginghub.pooled.jms.JmsPoolConnectionFactory
终于找到问题所在了,因为JmsMessagingTemplate本身依赖连接工厂,启用连接池后就会依赖连接池工厂,因为版本的原因,如果没有正确的引入连接池依赖,那么必然会出现上面说的这个问题,当我引入正确jar包之后,项目启动成功。
项目启动控制台输出
消费端显示刚才发送的消息,显示消费成功,大功告成,记录一下!!
activemq管理页面截图,符合预期效果:
到这里基本上springboot+activemq集成完成,记录一下!!