zoukankan      html  css  js  c++  java
  • rabbitmq的拉模式

    rabbitmq的拉模式

    rabbitmq(一)-基本入门我们已经展示了rabbitmq的推模式(mq主动推送,消费者监听)

    其实rabbitmq还提供了一种拉模式;

    1、直接上示例代码:

    rabbitmq(一)-基本入门的基础上
    我们把DemoLister注释掉

    同时增加主动获取消息的接口

        @GetMapping("getMq")
        public String getMq() throws IOException {
            ConnectionFactory connectionFactory = rabbitTemplate.getConnectionFactory();
            Connection connection = connectionFactory.createConnection();
            Channel channel = connection.createChannel(false);
            String queueName = "demo";
            boolean autoAck = false ;
            channel.basicQos(1);
            GetResponse response= channel.basicGet(queueName, autoAck);
    
            if(null == response){
                return "没有消息";
            }
            byte[] body = response.getBody();
            String msg = new String(body);
            channel.basicAck(response.getEnvelope().getDeliveryTag(),false);
            return msg;
        }
    

    2、模拟过程

    执行命令curl localhost:8080/sendMq?msg=ssg发送消息

    执行命令curl localhost:8080/getMq获取消息

    再执行一次命令curl localhost:8080/getMq获取消息

    备注

    不能将拉模式放在一个循环里来代替推模式;
    这样做会严重影响 RabbitMQ的性能。
    如果要实现高吞吐量,消费者理应使用推模式。

    代码地址:
    git clone -b teacher-pushpoll https://gitee.com/guoeryyj/rabbitmq-teacher.git

  • 相关阅读:
    8-6实战蒙版
    8-5渐变及半透明蒙版
    8-4修改蒙版
    8-3建立蒙版
    imageNamed、imageWithContentsOfFile、imageWithData
    #import、#include、@class、@protocol、@interface
    JSON解析
    控制器的生命周期
    纯代码方式实现九宫格布局
    KVC笔记
  • 原文地址:https://www.cnblogs.com/yeyongjian/p/13944021.html
Copyright © 2011-2022 走看看