Qt应用Redis实现消息队列
类似BS模式,客户端发送任务请求给服务端,服务端将处理结果返回给客户端。 redis负责消息的存储和转发。
仿真病人挂号看病,Patient进程进行挂号,Doctor进程进行看病 ,程序代码如下:
////////////////////////////////////////////Patient////////////////////////////////////////////
Patient.h:
- #include <QObject>
- class QRedis;
- class Patient : public QObject
- {
- Q_OBJECT
- public:
- Patient(QObject *parent = nullptr);
- ~Patient();
- public slots:
- void pushTask(); //push任务
- private:
- void popResult(); //pop结果
- QRedis * m_redis;
- };
Patient.cpp
- #include "patient.h"
- #include "qredis.h"
- #include <QTimer>
- #include <QEventLoop>
- #include <QThread>
- static const QString KEYTASK = "MARKTASK";
- static const QString KEYRESULT = "MARKRESULT";
- Patient::Patient(QObject *parent)
- : QObject(parent)
- {
- //初始化通道
- m_redis = new QRedis(this);
- m_redis->connectHost("127.0.0.1", 6379);
- m_redis->auth("1234");
- qDebug() << "client thread id :" << int(QThread::currentThreadId());
- //轮询任务
- QTimer * timer = new QTimer(this);
- connect(timer, &QTimer::timeout, this, &Patient::popResult);
- timer->start(20);
- m_redis->del(KEYRESULT);
- m_redis->del(KEYTASK);
- pushTask();
- }
- Patient::~Patient()
- {
- }
- void Patient::pushTask()
- {
- static int i = 0;
- QString task = QStringLiteral("%1号,姓名:%2,状态:%3").arg(++i).arg(QStringLiteral("病人%1").arg(i)).arg(QStringLiteral("挂号"));
- qDebug() <<"========================================================\n\n"<< task;
- qDebug() << "thread id :" << int(QThread::currentThreadId());
- qint64 ret = m_redis->rpush(KEYTASK, task);
- }
- void Patient::popResult()
- {
- QString state;
- QString taskData = m_redis->lpop(KEYRESULT);
- if (taskData.compare("nil", Qt::CaseInsensitive) == 0 || taskData.isEmpty())
- {
- return;
- }
- QEventLoop loop;
- QTimer::singleShot(5000, &loop, &QEventLoop::quit);
- loop.exec();
- pushTask();
- }
main.cpp
- #include <QtCore/QCoreApplication>
- #include <QDebug>
- #include <QThread>
- #include "patient.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- qDebug() << QString("main thread id = : %1").arg(int(QThread::currentThreadId()));
- QThread patientThread;
- Patient patient;
- patient.moveToThread(&patientThread);
- patientThread.start();
- return a.exec();
- }
/////////////////////////////////////////////////Docktor/////////////////////////////////////////////////
Docktor.h
- #pragma once
- #include <QObject>
- class QRedis;
- class Docktor : public QObject
- {
- Q_OBJECT
- public:
- Docktor(QObject *parent = nullptr);
- ~Docktor();
- public slots:
- void popTask(); //pop任务
- private:
- void pushResult(const QString &task); //push结果
- QRedis * m_redis;
- };
Docktor.cpp
- #include "docktor.h"
- #include "qredis.h"
- #include <QTimer>
- #include <QEventLoop>
- #include <QThread>
- static const QString KEYTASK = "MARKTASK";
- static const QString KEYRESULT = "MARKRESULT";
- Docktor::Docktor(QObject *parent)
- : QObject(parent)
- {
- //初始化通道
- m_redis = new QRedis(this);
- m_redis->connectHost("127.0.0.1", 6379);
- m_redis->auth("1234");
- QTimer * timer = new QTimer(this);
- connect(timer, &QTimer::timeout, this, &Docktor::popTask);
- timer->start(20);
- }
- Docktor::~Docktor()
- {
- }
- void Docktor::popTask()
- {
- //获取任务
- QString taskData = m_redis->lpop(KEYTASK);
- if (taskData.compare("nil", Qt::CaseInsensitive) == 0 || taskData.isEmpty())
- {
- //qDebug() << QString("wait..............................");
- return;
- }
- //处理任务
- pushResult(taskData);
- }
- void Docktor::pushResult(const QString &task)
- {
- QStringList taskDatas = task.split(",");
- QString state = taskDatas.at(2);
- taskDatas.removeLast();
- taskDatas.append(QStringLiteral("状态:看病"));
- //push处理结果
- qDebug() <<"========================================================\n\n" << taskDatas.join(",");
- qDebug() << "thread id :" << int(QThread::currentThreadId());
- qint64 ret = m_redis->rpush(KEYRESULT, taskDatas.join(","));
- }
main.cpp
- #include <QtCore/QCoreApplication>
- #include <QDebug>
- #include <QThread>
- #include "docktor.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- qDebug() << QString("main thread id = : %1").arg(int(QThread::currentThreadId()));
- QThread docktorThread;
- Docktor docktor;
- docktor.moveToThread(&docktorThread);
- docktorThread.start();
- return a.exec();
- }
/////////////////截图/////////////
Qt应用Redis实现消息队列的更多相关文章
- Redis 做消息队列
一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现.定义: 生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...
- Redis作为消息队列服务场景应用案例
NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例 一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更 ...
- redis resque消息队列
Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- 【Redis】php+redis实现消息队列
在项目中使用消息队列一般是有如下几个原因: 把瞬间服务器的请求处理换成异步处理,缓解服务器的压力 实现数据顺序排列获取 redis实现消息队列步骤如下: 1).redis函数rpush,lpop 2) ...
- Lumen开发:结合Redis实现消息队列(1)
1.简介 Lumen队列服务为各种不同的后台队列提供了统一的API.队列允许你推迟耗时任务(例如发送邮件)的执行,从而大幅提高web请求速度. 1.1 配置 .env文件的QUEUE_DRIVER选项 ...
- Redis除了做缓存--Redis做消息队列/Redis做分布式锁/Redis做接口限流
1.用Redis实现消息队列 用命令lpush入队,rpop出队 Long size = jedis.lpush("QueueName", message);//返回存放的数据条数 ...
- sping+redis实现消息队列的乱码问题
使用spring支持redis实现消息队列,参考官方样例:https://spring.io/guides/gs/messaging-redis/ 实现后在运行过程中发现消费者在接收消息时会出现乱码的 ...
- 程序员过关斩将--redis做消息队列,香吗?
Redis消息队列 在程序员这个圈子打拼了太多年,见过太多的程序员使用redis,其中一部分喜欢把redis做缓存(cache)使用,其中最典型的当属存储用户session,除此之外,把redis作为 ...
随机推荐
- 关于微信小程序中的样式使用变量值的方法
在开发过程中,通常碰到样式非固定的情况,这时候就要使用变量来规定样式,例如,一个view的宽度需要使用变量: 1. 在wxss中,定义变量:width:var(--width--); 2. 在js中, ...
- div 清除浮动的四种方法
概述:为了解决父级元素因为子级内部高度为0的问题 (很多情况 不方便给父级元素高,因为不知道有多少内容,让里面的盒子自动撑起高度),清除浮动本质叫闭合浮动更好一些,清除浮动就是把浮动的盒子关到里面,让 ...
- ASE19团队项目alpha阶段model组 scrum4 记录
本次会议于11月6日,19时整在微软北京西二号楼sky garden召开,持续50分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...
- Delphi 定义线程对象
- Django—views系统:views基础
Django的View(视图)简介 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...
- Oralce问题之ORA-12560:TNS协议适配器错误
在Windows系统中,通过CMD命令打开命令窗口,通过命令:sqlplus / as sysdba回车后提示 协议适配器错误 可能原因: (1).Oralce数据库监听服务没启动起来 通过开始-&g ...
- hexo不蒜子网站访问量统计失效
问题 hexo博客的不蒜子网站访问量统计最近失效了. 解决 原因 不蒜子域名更改了,所以需要修改博客的配置文件. 方法 进入博客目录下\themes\next\layout\_third-party\ ...
- SendMessage到底是如何工作的?
以下内容摘自<<Windows核心编程>>: 概要: SendMessage对于在同一个线程中调用的话,直接调用的是当前线程所属窗口的窗口过程函数(WndProc);如果是跨线 ...
- 关于github报错 ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repository.
今天上午写demo的时候,突然pull不下代码了,报了一下这样情况的错误: 看了一下代码,怀疑是网路错误,因为在这以前一切都正常的,然后将代码复制搜索了一番,解决办法有很多什么配置config啦,gi ...
- [AHOI2007]密码箱 (数学 + 暴力)
链接:https://ac.nowcoder.com/acm/problem/19877来源:牛客网 题目描述 在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能 ...