admin管理员组文章数量:1794759
spring boot集成Active MQ 报错Consider revisiting the entries above or defining a bean of type
如何在Windows环境安装和启动Active MQ请看上一篇博文:
blog.csdn/h_j_c_123/article/details/108454607
首先我们使用idea新建一个spring boot工程,不会新建工程的请看该篇博文:
blog.csdn/h_j_c_123/article/details/93477170
然后我们开始说明如何继承:
首先添加依赖:
<!--ActiveMq--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> <version>1.5.0.RELEASE</version> </dependency> <dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency>有的博客还会说连接池使用下面这个依赖:
<!--消息队列连接池--> <!--<dependency>--> <!--<groupId>org.apache.activemq</groupId>--> <!--<artifactId>activemq-pool</artifactId>--> <!--<version>5.15.0</version>--> <!--</dependency>-->但是经过尝试这个依赖会报错:
*************************** APPLICATION FAILED TO START ***************************
Description:
Field jmsMessagingTemplate in com.dream.controller.ProviderController 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 boot版本的问题。
然后我们先在启动类上加上@EnableJms允许启动MQ
@SpringBootApplication // 允许启动ActiveMQ @EnableJms public class BootMqApplication { public static void main(String[] args) { SpringApplication.run(BootMqApplication.class, args); } }
然后就可以创建生产者:
点对点的生产者消息队列:
首先配置文件(点对点和发布订阅模式写在一起了):
server.port=8088 #61616是程序连接activemq的通讯地址 spring.activemq.broker-url=tcp://127.0.0.1:61616 spring.activemq.user=admin spring.activemq.password=admin spring.activemq.pool.enabled=true spring.activemq.pool.max-connections=10 spring.activemq.pool.idle-timeout=30000 spring.activemq.in-memory=false #如果是点对点(queue),那么此处默认应该是false,如果发布订阅,那么一定设置为true spring.jms.pub-sub-domain=true配置消息队列:
@Configuration public class BeanConfig { // 定义存放消息的队列 点对点模式 @Bean public Queue queue() { return new ActiveMQQueue("active.queue"); } }新建生产者:ProviderController
注入消息队列:
// 注入存放消息的队列 @Autowired private Queue queue; // 注入spring boot封装的工具 @Autowired private JmsMessagingTemplate jmsMessagingTemplate; // 发送消息队列 @RequestMapping(value = "/sendQueue") public void sendQueue(String name) { // 方法一:添加消息到消息队列 jmsMessagingTemplate.convertAndSend(queue, name); // 方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列 // jmsMessagingTemplate.convertAndSend("test", name); }然后我们来看消费者:
@Component public class ConsumerService { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息 @JmsListener(destination = "active.queue") // SendTo 会将此方法返回的数据, 写入到 OutQueue 中去. @SendTo(value = "SQueue") public String handleMessage(String name) { System.out.println("成功接受Name" + name); return "成功接受Name" + name; } }至此,我们的一个基础的消息队列完成,然后我们来看一下点对点的消息模式效果:
发送数据:
结果截图:
点对点的时候记得把
#spring.jms.pub-sub-domain=true注释掉或者改成false
下面来看发布和订阅模式:
配置消息队列-订阅模式:
@Configuration public class BeanConfig { // 发布订阅模式 @Bean public Topic topic() { return new ActiveMQTopic("active.topic"); } }// 订阅发布模式 @RequestMapping(value = "/sendTopic") public String sendTopic(String message) { // 指定消息发送的目的地和内容 jmsMessagingTemplate.convertAndSend(topic, message); return "消息发送成功!message=" + message; }
新建消费者:
// 监听和接收主题消息1 @JmsListener(destination = "active.topic") public void readActiveTopic1(String message) { System.out.println("Customer1接受到:" + message); } // 监听和接收主题消息2 @JmsListener(destination = "active.topic") public void readActiveTopic2(String message) { System.out.println("Customer2接受到:" + message); }下面来验证数据:
这样一个简单的spring boot和Active MQ的集成就完成了。
码云地址:gitee/hjc2/springboot.git
版权声明:本文标题:spring boot集成Active MQ 报错Consider revisiting the entries above or defining a bean of type 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686556584a81522.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论