SpringBoot+ActiveMQ启动报错:'JmsAutoConfiguration did not match'问题分析

SpringBoot整合ActiveMQ,一切准备工作就绪之后,启动报错如下:

java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:40)
2018-11-16 18:04:36.040  INFO 4088 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-11-16 18:04:36.155 ERROR 4088 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

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.

Disconnected from the target VM, address: '127.0.0.1:62933', transport: 'socket'

Process finished with exit code 1

提示JmsMessagingTemplate无法注入,这里说明一下,我的生产者中注入了JmsMessagingTemplate,代码如下:

package com.haozz.demo.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.Date;

/**
 * 

Coding

* * @author haozz * @version $Id: DemoClass.java, v 0.1 2018/11/15 19:26 haozz Exp $ */ @Component @EnableScheduling public class PromoteActProducer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Scheduled(fixedDelay = 2000) //每2s执行一次 public void send() { String date = new Date().toString(); this.jmsMessagingTemplate.convertAndSend(this.queue, date + " : hello ActiveMQ , this is producer ; "); } }

网上找了一下,所有的文章的都说是application.properties配置中关于ActiveMQ的配置后面多出了空格,可是我的配置后面明明没有空格,贴出来大家看一下:

server.port=8080
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.in-memory=false
spring.activemq.pool.enabled=true

这里吐槽一下,就这个问题百度一下,出来的帖子全都说配置后面有空格,来自各个平台不下二十篇,更离谱的是这些帖子的内容一模一样,标点符号都不差,而且每一篇都声明自己是原创。

最后,在茫茫的帖子中发现了这一篇,算是解决了问题:https://www.aliyun.com/jiaocheng/779295.html,感谢。这篇文章中给出的办法是不再使用Autowired注入,而是使用ConnectionFactory生成JmsTemplate,然后再生成JmsMessagingTemplate。当然,这样也可以解决一时无法启动的问题,最起码比盲目跟风说配置有空格的好。果然这样写也是可以启动成功的,代码如下:

package com.haozz.demo.mq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import java.util.Date;

/**
 * 

Coding

* * @author haozz * @version $Id: DemoClass.java, v 0.1 2018/11/15 19:26 haozz Exp $ */ @Component @EnableScheduling public class PromoteActProducer { // @Autowired // private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Bean ConnectionFactory connectionFactory() { return new ActiveMQConnectionFactory(); } @Bean JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) { JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory); jmsTemplate.setPriority(999); return jmsTemplate; } @Bean(value="jmsMessagingTemplate") JmsMessagingTemplate jmsMessagingTemplate(JmsTemplate jmsTemplate) { JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate(jmsTemplate); return messagingTemplate; } @Scheduled(fixedDelay = 2000) //每2s执行一次 public void send() { String date = new Date().toString(); JmsMessagingTemplate jmsMessagingTemplate = jmsMessagingTemplate(jmsTemplate(connectionFactory())); jmsMessagingTemplate.convertAndSend(this.queue, date + " : hello ActiveMQ , this is producer ; "); // this.jmsMessagingTemplate.convertAndSend(this.queue, date + " : hello ActiveMQ , this is producer ; "); } }

最终,在不断尝试之下,找出了一点曙光,其实原因还是在于配置文件。其中有一项pool.enabled,为true时表示使用连接池,但是pool.enabled=true时启动就会报JmsMessagingTemplate无法注入的错。将pool.enabled改为false,便可以启动成功,具体原因现在还不得而知,后面再做分析。

 

你可能感兴趣的:(Java,SpringBoot,ActiveMQ)