版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liulihuo_gyh/article/details/78425763

类似BS模式,客户端发送任务请求给服务端,服务端将处理结果返回给客户端。 redis负责消息的存储和转发。

仿真病人挂号看病,Patient进程进行挂号,Doctor进程进行看病 ,程序代码如下:

////////////////////////////////////////////Patient////////////////////////////////////////////

Patient.h:

  1.  
    #include <QObject>
  2.  
     
  3.  
    class QRedis;
  4.  
     
  5.  
    class Patient : public QObject
  6.  
    {
  7.  
    Q_OBJECT
  8.  
     
  9.  
    public:
  10.  
    Patient(QObject *parent = nullptr);
  11.  
    ~Patient();
  12.  
     
  13.  
    public slots:
  14.  
    void pushTask(); //push任务
  15.  
     
  16.  
    private:
  17.  
    void popResult(); //pop结果
  18.  
    QRedis * m_redis;
  19.  
    };

Patient.cpp

  1.  
    #include "patient.h"
  2.  
    #include "qredis.h"
  3.  
    #include <QTimer>
  4.  
    #include <QEventLoop>
  5.  
    #include <QThread>
  6.  
     
  7.  
    static const QString KEYTASK = "MARKTASK";
  8.  
    static const QString KEYRESULT = "MARKRESULT";
  9.  
     
  10.  
    Patient::Patient(QObject *parent)
  11.  
    : QObject(parent)
  12.  
    {
  13.  
    //初始化通道
  14.  
    m_redis = new QRedis(this);
  15.  
    m_redis->connectHost("127.0.0.1", 6379);
  16.  
    m_redis->auth("1234");
  17.  
     
  18.  
    qDebug() << "client thread id :" << int(QThread::currentThreadId());
  19.  
     
  20.  
    //轮询任务
  21.  
    QTimer * timer = new QTimer(this);
  22.  
    connect(timer, &QTimer::timeout, this, &Patient::popResult);
  23.  
    timer->start(20);
  24.  
     
  25.  
    m_redis->del(KEYRESULT);
  26.  
    m_redis->del(KEYTASK);
  27.  
    pushTask();
  28.  
    }
  29.  
     
  30.  
    Patient::~Patient()
  31.  
    {
  32.  
    }
  33.  
     
  34.  
    void Patient::pushTask()
  35.  
    {
  36.  
    static int i = 0;
  37.  
    QString task = QStringLiteral("%1号,姓名:%2,状态:%3").arg(++i).arg(QStringLiteral("病人%1").arg(i)).arg(QStringLiteral("挂号"));
  38.  
    qDebug() <<"========================================================\n\n"<< task;
  39.  
    qDebug() << "thread id :" << int(QThread::currentThreadId());
  40.  
    qint64 ret = m_redis->rpush(KEYTASK, task);
  41.  
    }
  42.  
     
  43.  
    void Patient::popResult()
  44.  
    {
  45.  
    QString state;
  46.  
    QString taskData = m_redis->lpop(KEYRESULT);
  47.  
    if (taskData.compare("nil", Qt::CaseInsensitive) == 0 || taskData.isEmpty())
  48.  
    {
  49.  
    return;
  50.  
    }
  51.  
    QEventLoop loop;
  52.  
    QTimer::singleShot(5000, &loop, &QEventLoop::quit);
  53.  
    loop.exec();
  54.  
    pushTask();
  55.  
    }
  56.  
     

main.cpp

  1.  
    #include <QtCore/QCoreApplication>
  2.  
    #include <QDebug>
  3.  
    #include <QThread>
  4.  
    #include "patient.h"
  5.  
     
  6.  
    int main(int argc, char *argv[])
  7.  
    {
  8.  
    QCoreApplication a(argc, argv);
  9.  
     
  10.  
    qDebug() << QString("main thread id = : %1").arg(int(QThread::currentThreadId()));
  11.  
     
  12.  
    QThread patientThread;
  13.  
    Patient patient;
  14.  
    patient.moveToThread(&patientThread);
  15.  
    patientThread.start();
  16.  
    return a.exec();
  17.  
    }

/////////////////////////////////////////////////Docktor/////////////////////////////////////////////////

Docktor.h

  1.  
    #pragma once
  2.  
     
  3.  
    #include <QObject>
  4.  
     
  5.  
    class QRedis;
  6.  
     
  7.  
    class Docktor : public QObject
  8.  
    {
  9.  
    Q_OBJECT
  10.  
     
  11.  
    public:
  12.  
    Docktor(QObject *parent = nullptr);
  13.  
    ~Docktor();
  14.  
     
  15.  
    public slots:
  16.  
    void popTask(); //pop任务
  17.  
     
  18.  
    private:
  19.  
    void pushResult(const QString &task); //push结果
  20.  
     
  21.  
    QRedis * m_redis;
  22.  
    };

Docktor.cpp

  1.  
    #include "docktor.h"
  2.  
    #include "qredis.h"
  3.  
    #include <QTimer>
  4.  
    #include <QEventLoop>
  5.  
    #include <QThread>
  6.  
     
  7.  
    static const QString KEYTASK = "MARKTASK";
  8.  
    static const QString KEYRESULT = "MARKRESULT";
  9.  
     
  10.  
    Docktor::Docktor(QObject *parent)
  11.  
    : QObject(parent)
  12.  
    {
  13.  
    //初始化通道
  14.  
    m_redis = new QRedis(this);
  15.  
    m_redis->connectHost("127.0.0.1", 6379);
  16.  
    m_redis->auth("1234");
  17.  
     
  18.  
    QTimer * timer = new QTimer(this);
  19.  
    connect(timer, &QTimer::timeout, this, &Docktor::popTask);
  20.  
    timer->start(20);
  21.  
    }
  22.  
     
  23.  
    Docktor::~Docktor()
  24.  
    {
  25.  
    }
  26.  
     
  27.  
    void Docktor::popTask()
  28.  
    {
  29.  
    //获取任务
  30.  
    QString taskData = m_redis->lpop(KEYTASK);
  31.  
    if (taskData.compare("nil", Qt::CaseInsensitive) == 0 || taskData.isEmpty())
  32.  
    {
  33.  
    //qDebug() << QString("wait..............................");
  34.  
    return;
  35.  
    }
  36.  
    //处理任务
  37.  
    pushResult(taskData);
  38.  
    }
  39.  
     
  40.  
    void Docktor::pushResult(const QString &task)
  41.  
    {
  42.  
    QStringList taskDatas = task.split(",");
  43.  
    QString state = taskDatas.at(2);
  44.  
    taskDatas.removeLast();
  45.  
    taskDatas.append(QStringLiteral("状态:看病"));
  46.  
    //push处理结果
  47.  
    qDebug() <<"========================================================\n\n" << taskDatas.join(",");
  48.  
    qDebug() << "thread id :" << int(QThread::currentThreadId());
  49.  
    qint64 ret = m_redis->rpush(KEYRESULT, taskDatas.join(","));
  50.  
    }
  51.  
     

main.cpp

  1.  
    #include <QtCore/QCoreApplication>
  2.  
    #include <QDebug>
  3.  
    #include <QThread>
  4.  
    #include "docktor.h"
  5.  
     
  6.  
    int main(int argc, char *argv[])
  7.  
    {
  8.  
    QCoreApplication a(argc, argv);
  9.  
    qDebug() << QString("main thread id = : %1").arg(int(QThread::currentThreadId()));
  10.  
     
  11.  
    QThread docktorThread;
  12.  
    Docktor docktor;
  13.  
    docktor.moveToThread(&docktorThread);
  14.  
    docktorThread.start();
  15.  
    return a.exec();
  16.  
    }

/////////////////截图/////////////

Qt应用Redis实现消息队列的更多相关文章

  1. Redis 做消息队列

    一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现.定义: 生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...

  2. Redis作为消息队列服务场景应用案例

    NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例   一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更 ...

  3. redis resque消息队列

    Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...

  4. 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能

    springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...

  5. 【Redis】php+redis实现消息队列

    在项目中使用消息队列一般是有如下几个原因: 把瞬间服务器的请求处理换成异步处理,缓解服务器的压力 实现数据顺序排列获取 redis实现消息队列步骤如下: 1).redis函数rpush,lpop 2) ...

  6. Lumen开发:结合Redis实现消息队列(1)

    1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...

  7. Redis除了做缓存--Redis做消息队列/Redis做分布式锁/Redis做接口限流

    1.用Redis实现消息队列 用命令lpush入队,rpop出队 Long size = jedis.lpush("QueueName", message);//返回存放的数据条数 ...

  8. sping+redis实现消息队列的乱码问题

    使用spring支持redis实现消息队列,参考官方样例:https://spring.io/guides/gs/messaging-redis/ 实现后在运行过程中发现消费者在接收消息时会出现乱码的 ...

  9. 程序员过关斩将--redis做消息队列,香吗?

    Redis消息队列 在程序员这个圈子打拼了太多年,见过太多的程序员使用redis,其中一部分喜欢把redis做缓存(cache)使用,其中最典型的当属存储用户session,除此之外,把redis作为 ...

随机推荐

  1. ELECTRON 打包

    安装electron-packager cnpm install electron-packager -g 配置package.json "scripts": { "st ...

  2. 使用2种python脚本工具将2个txt文档中的文字进行比较,并计算出Corr, WER正确率,准确率

    一.准备: linux服务器,src2mlf.py   rec2mlf.py   HResults文件,1份源文件和1份需要对比的文件.文件放置于本人云盘 二.使用方法: 1. 对比工具 HResul ...

  3. 6.SpringMVC 配置式开发-处理器

    处理器除了实现Controller 接口外,还可以继承自一些其他的类,来完成一些特殊的功能 1.继承自AbstractController类 若处理器继承自AbstractController类,那么 ...

  4. Django—Form组件

    Django From简介 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比 ...

  5. PAT Basic 1065 单身狗 (25 分)

    “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣的对数:随后 N ...

  6. Kinect视频中运用全身运动和人体测量统计学的人物识别技术

    摘要: 对于人物识别技术来说,动作和人体测量统计学对于光学差异并不敏感,甚至对于眼镜,头发,帽子的描述相当粗糙,现在的以步态为基础的识别技术都是基于对细节的精确描述和对步态周期的精确测量.这种方法需要 ...

  7. E - GCD HDU - 2588

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  8. C - Calculation 2 HDU - 3501 (欧拉)

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  9. jsp的标签库

    Java Server Pages Standard Tag Libray(JSTL):JSP 标准标签库,是一个定制标签类库的集合,用于解决一些常见的问题,例如迭代一个映射或者集合.条件测试.XML ...

  10. python+Appium自动化:屏幕截图

    屏幕截图 主要是为了程序出现错误时,开发除了可以分析日志之外,还可以进行截图更好地去定位问题. 截图一般有两种方法: 第一种save_screenshot(self,filename) driver. ...