解決springboot+activemq啟動報註解錯誤的問題

springboot+activemq啟動報註解錯誤

Description:

Field jmsMessagingTemplate in com.haozz.demo.mq.PromoteActProducer 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.

原因總結有以下3點原因:

1.spring.activemq.pool.enabled=true,線程池開啟,後面有空格,且沒有引入線程池包

       <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>

2.springboot版本問題不支持,建議用2.0版本啟動

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

猜想:估計是因為2.1.0+版本有自己的線程池管理,導致沖突

3.spring.activemq.pool.enabled=false,關閉線程池

springboot整合activemq踩過坑

啟動時候就關閉瞭

配置如下

server:
 port: 8762
#    context-path: /memeber
eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/
spring:
   application:
       name: member
   redis:
       host: 127.0.0.1
       password: 123456
       port: 6379
       pool:
           max-idle: 100
           min-idle: 1
           max-active: 1000
           max-wait: -1
   datasource:
       name: test1
       url: jdbc:mysql://127.0.0.1:3306/dandan-member
       username: root
       password: 123456
       type: com.alibaba.druid.pool.DruidDataSource
       driver-class-name: com.mysql.jdbc.Driver
       filters: stat
       maxActive: 20
       initialSize: 1
       maxWait: 60000
       minIdle: 1
       timeBetweenEvictionRunsMillis: 60000
       minEvictableIdleTimeMillis: 300000
       validationQuery: select 'x'
       testWhileIdle: true
       testOnBorrow: false
       testOnReturn: false
       poolPreparedStatements: true
       maxOpenPreparedStatements: 20
    ##activemq連接信息
activemq:
   broker-url: tcp://localhost:61616
   in-memory: true
   pool:
     enabled: false
##隊列
messages:
    queue: messages_queue

解決如下

原本activemq 沒有在spring.後面直接在前面瞭,與spring同級瞭

解決後配置如下

server:
 port: 8762
#    context-path: /memeber
eureka:
 client:
   service-url:
     defaultZone: http://localhost:8761/eureka/
spring:
   application:
       name: member
   redis:
       host: 127.0.0.1
       password: 123456
       port: 6379
       pool:
           max-idle: 100
           min-idle: 1
           max-active: 1000
           max-wait: -1
   datasource:
       name: test1
       url: jdbc:mysql://127.0.0.1:3306/dandan-member
       username: root
       password: 123456
       type: com.alibaba.druid.pool.DruidDataSource
       driver-class-name: com.mysql.jdbc.Driver
       filters: stat
       maxActive: 20
       initialSize: 1
       maxWait: 60000
       minIdle: 1
       timeBetweenEvictionRunsMillis: 60000
       minEvictableIdleTimeMillis: 300000
       validationQuery: select 'x'
       testWhileIdle: true
       testOnBorrow: false
       testOnReturn: false
       poolPreparedStatements: true
       maxOpenPreparedStatements: 20
    ##activemq連接信息
   activemq:
       broker-url: tcp://localhost:61616
       in-memory: true
       pool:
          enabled: false
##隊列
messages:
    queue: messages_queue

建議。yml縮進要四個字節。一個字節不容易分辨

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: