• springboot整合rabbitmq入门(三)


    在上一篇文章中介绍了rabbitmq的fanout模式。今天继续学习另一种模式——direct模式。这种模式是rabbitmq的最简单一种模式。

    首先创建一个名为helloDirect1的对列

    1. @Configuration
    2. public class DirectRabbitConfig {
    3. @Bean
    4. public Queue directA(){
    5. return new Queue("helloDirect1");
    6. }
    7. }

    创建发送端

    1. @Component
    2. public class DirectSend {
    3. @Autowired
    4. private RabbitTemplate rabbitTemplate;
    5. public void send(String context){
    6. System.out.println("Sender:" + context);
    7. // 这里需要指定发送的消息队列
    8. this.rabbitTemplate.convertAndSend("helloDirect1",context);
    9. }
    10. }

    接受端

    1. @Component
    2. @RabbitListener(queues = "helloDirect1")
    3. public class DirectReceiveA {
    4. @RabbitHandler
    5. public void receive(String context){
    6. System.out.println("direct.A"+context);
    7. }
    8. }

    在这里,我有点不是特别清楚的地方,就是rabbitmq中说的是direct模式不需要exchange,只需要绑定routingKey即可,即

    this.rabbitTemplate.convertAndSend("helloDirect1",context);中第一个参数为routingKey,但是我在写代码过程发现其实这块应该写的是你想发送到哪个对列,就写这个对列名称。不知道这样理解对不对,如果有不对的地方请指出。

    控制台打印:

    说明我们接受成功。

    在写代码过程中突然想到,@RabbitListener注解是不是可以同时监听到多个对列呢?

    在创建一个为ceshi的对列

    1. @Bean
    2. public Queue directB(){
    3. return new Queue("ceshi");
    4. }

    同时监听多个对列

    1. @Component
    2. @RabbitListener(queues = {"helloDirect1","ceshi"})
    3. public class DirectReceiveA {
    4. @RabbitHandler
    5. public void receive(String context){
    6. System.out.println("direct.A"+context);
    7. }
    8. }

    发送端

    1. @Component
    2. public class DirectSend {
    3. @Autowired
    4. private RabbitTemplate rabbitTemplate;
    5. public void send(String context){
    6. System.out.println("Sender:" + context);
    7. // 这里需要指定发送的消息队列
    8. this.rabbitTemplate.convertAndSend("ceshi",context);
    9. this.rabbitTemplate.convertAndSend("helloDirect1",context);
    10. }
    11. }

    控制台打印

    成功

    当生产者和消费者是一对多时,会是什么样的效果呢?

    在创建一个消费端同时监听helloDirect1对列

    1. @Component
    2. @RabbitListener(queues = "helloDirect1")
    3. public class DirectReceiveB {
    4. @RabbitHandler
    5. public void receive(String context){
    6. System.out.println("direct.B"+context);
    7. }
    8. }

    改一下测试类,循环发送10遍,看效果

    1. @Test
    2. public void DirectSend(){
    3. for (int i=0;i<10;i++){
    4. directSend.send("测试"+i);
    5. }
    6. }

    但是如果一个消费者消费快,另一个慢的话回怎么样呢?

    我们对消费者B休眠2秒,看效果

    1. @Component
    2. @RabbitListener(queues = "helloDirect1")
    3. public class DirectReceiveB {
    4. @RabbitHandler
    5. public void receive(String context){
    6. try {
    7. Thread.sleep(2000);
    8. } catch (InterruptedException e) {
    9. e.printStackTrace();
    10. }
    11. System.out.println("direct.B"+context);
    12. }
    13. }

    会发现A消费者消费结束了,但B消费者还没有完成。这就造成了A消费者空闲下来了,资源浪费。

  • 相关阅读:
    计算机组成原理(一)系统概论
    (附源码)计算机毕业设计SSM洁强汽车美容
    13.Mybatis之动态sql查询(面试)
    Python快速刷题网站——牛客网 数据分析篇(三)
    html大作业【NBA篮球介绍 22个页面】学生网页设计源码
    利用闭包的特点来实现一个简单的缓存
    一:高通量虚拟筛选技术与中药/天然产物挖掘药效分子专题
    代码随想录算法训练营第53天|1143. 最长公共子序列,1035. 不相交的线,53. 最大子数组和
    Flutter 问题集
    【vue】使用 apache 给前后端服务做反向代理
  • 原文地址:https://blog.csdn.net/java_chegnxuyuan/article/details/90048624