本機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集成完成,記錄一下!!