SpringBoot与ActiveMQ集成后springboot版本升级导致报错

之前使用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>

先贴一下原来的配置代码:

@Configuration
public class QueueConfig {
	@Value("${queue}")
	private String queue;

	@Bean
	public Queue logQueue() {
		return new ActiveMQQueue(queue);
	}

	@Bean
	public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, Queue queue) {
		JmsTemplate jmsTemplate = new JmsTemplate();
		jmsTemplate.setDeliveryMode(2);// 进行持久化配置 1表示非持久化,2表示持久化</span>
		jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
		jmsTemplate.setDefaultDestination(queue); // 此处可不设置默认,在发送消息时也可设置队列
		jmsTemplate.setSessionAcknowledgeMode(4);// 客户端签收模式</span>
		return jmsTemplate;
	}

	// 定义一个消息监听器连接工厂,这里定义的是点对点模式的监听器连接工厂
	@Bean(name = "jmsQueueListener")
	public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(
			ActiveMQConnectionFactory activeMQConnectionFactory) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		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中找不到。
报错截图zyf97.cn
于是百度了下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>

activeMQ配置
经过测试发现使用成功,后续删掉了新增的依赖信息发现仍然成功,实际上只需变更连接工厂。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 集成 ActiveMQ 很简单,只需要在项目的 pom.xml 文件中添加 ActiveMQ 的依赖,然后在配置文件 application.properties 中配置 ActiveMQ 的地址即可。具体步骤如下: 1. 在 pom.xml 文件中添加 ActiveMQ 的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置 ActiveMQ 的地址,例如: ``` spring.activemq.broker-url=tcp://localhost:61616 ``` 3. 在启动类中添加 @EnableJms 注解,表示启用 JMS。 4. 创建一个 JmsTemplate 对象来发送和接收消息。 完成以上步骤即可在 Spring Boot 中使用 ActiveMQ。 ### 回答2: Spring Boot是Spring Framework的一个用于简化Spring应用程序开发和部署的框架。它提供了一种简单的方式来开发、构建和运行Spring应用程序,同时也能方便地集成其他框架和技术。 ActiveMQ是一个流行的开源消息代理,它支持多种协议和编程语言,包括Java、C#、C++、Python等。它提供了可靠的消息传递机制,可以用于构建分布式系统和异步消息处理应用。 Spring Boot提供了对ActiveMQ集成支持,可以通过在pom.xml文件中添加相关依赖和配置,实现快速集成和使用ActiveMQ。 下面是详细的步骤: 1. 在pom.xml文件中添加ActiveMQ的依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> </dependencies> ``` 2. 在application.properties文件中添加ActiveMQ的配置: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 其中broker-url是ActiveMQ连接地址,user和password是登录信息。 3. 在代码中使用JmsTemplate发送和接收消息: ``` @Service public class MessageService { @Autowired private JmsTemplate jmsTemplate; public void sendMessage(String message) { jmsTemplate.send("test.queue", new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } @JmsListener(destination = "test.queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 上面的代码定义了一个MessageService类,里面包含了一个sendMesssge方法和一个receiveMessage方法。sendMessage方法将消息发送到名为“test.queue”的队列中,而receiveMessage方法则监听该队列并接收消息。 在使用JmsTemplate发送消息时,需要提供消息的目的地和消息内容。这里使用createTextMessage方法创建一个文本消息,并将其发送到指定的队列中。 在使用@JmsListener注解接收消息时,需要指定监听的队列名称和接收消息的方法。接收到消息后,将会调用该方法并输出消息内容。 4. 运行应用程序并测试消息发送和接收功能。 启动应用程序后,调用MessageService的sendMessage方法即可发送消息到ActiveMQ,同时在控制台输出了接收到的消息。这样,就成功地实现了Spring Boot和ActiveMQ集成。 总之,通过以上步骤,我们可以很容易地将Spring Boot和ActiveMQ进行集成,在开发分布式系统和消息处理应用时,能够方便地使用ActiveMQ提供的可靠消息传递机制。 ### 回答3: Spring Boot是一种基于Spring框架的快速开发框架,它提供了很多便捷快捷的特性。ActiveMQ则是一种高性能、开源的消息传递中间件。Spring Boot和ActiveMQ的结合,能够搭建一个可插拔、可扩展、可重复利用的消息解决方案。下面将介绍如何在Spring Boot中集成ActiveMQ。 1、引入所需依赖 首先需要在项目中引入ActiveMQ相关的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 2、配置ActiveMQ 在application.properties文件中配置ActiveMQ,如下所示: #ActiveMQ configuration spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.jms.template.default-destination=test-queue spring.jms.listener.auto-startup=true 其中,spring.activemq.broker-url指定了ActiveMQ服务器的地址;spring.activemq.user和spring.activemq.password是登录ActiveMQ服务器的用户名和密码;spring.jms.template.default-destination设置了默认的目的地队列;spring.jms.listener.auto-startup设置消息监听器的自动启动。 3、编写消息生产者代码 @Inject private JmsMessagingTemplate jmsMessagingTemplate; public void produceMessage(String message) { jmsMessagingTemplate.convertAndSend("test-queue", message); } 首先在类中注入JmsMessagingTemplate,它是Spring Boot提供的用于发送消息的工具类。然后编写produceMessage()方法,将消息发送到目的地队列test-queue中。 4、编写消息消费者代码 @JmsListener(destination="test-queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } 使用@JmsListener注解,在方法上指定监听的目的地队列为test-queue。当有消息到达test-queue时,就会自动调用receiveMessage()方法,进行消息处理。 通过上述步骤,就可以在Spring Boot中集成ActiveMQ,实现消息生产者和消息消费者的功能。当然,还可以通过其他方式来实现集成,例如使用ConnectionFactory或者JmsTemplate等Spring集成方式。这样就能够应用在更多的项目中,提高应用的可靠性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值