spring boot集成Active MQ 报错Consider revisiting the entries above or defining a bean of type

16 篇文章 0 订阅
2 篇文章 0 订阅

如何在Windows环境安装和启动Active MQ请看上一篇博文:

https://blog.csdn.net/h_j_c_123/article/details/108454607

首先我们使用idea新建一个spring boot工程,不会新建工程的请看该篇博文:

https://blog.csdn.net/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的集成就完成了。

码云地址:https://gitee.com/hjc2/springboot.git

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值