Qt应用Redis实现消息队列

 

版权声明:本文为博主原创文章,未经博主允许不得转载。 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.  
    }

/截图/

 

转载于:https://www.cnblogs.com/lvdongjie/p/9896649.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot与Redis结合实现消息队列的方法如下: 1. 首先,确保你的Spring Boot项目中已经引入了Redis的依赖。 2. 创建一个消息发布者类,用于发布消息Redis消息队列中。可以使用RedisTemplate来实现消息的发布。以下是一个示例代码: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component public class MessagePublisher { @Autowired private RedisTemplate<String, Object> redisTemplate; public void publish(String channel, Object message) { redisTemplate.convertAndSend(channel, message); } } ``` 3. 创建一个消息订阅者类,用于监听Redis消息队列并处理接收到的消息。可以使用@RedisListener注解来实现消息的订阅。以下是一个示例代码: ```java import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.stereotype.Component; @Component public class MessageSubscriber implements MessageListener { @Override public void onMessage(Message message, byte[] pattern) { String channel = new String(message.getChannel()); String body = new String(message.getBody()); // 处理接收到的消息 System.out.println("Received message: " + body + " from channel: " + channel); } } ``` 4. 在需要发布消息的地方,通过调用消息发布者类的publish方法来发布消息。以下是一个示例代码: ```java @Autowired private MessagePublisher messagePublisher; public void sendMessage(String channel, Object message) { messagePublisher.publish(channel, message); } ``` 5. 在需要订阅消息的地方,通过在消息订阅者类的方法上添加@RedisListener注解来监听指定的频道。以下是一个示例代码: ```java @RedisListener(channels = "myChannel") public void handleMessage(String message) { // 处理接收到的消息 System.out.println("Received message: " + message); } ``` 通过以上步骤,你就可以使用Spring Boot与Redis结合实现消息队列了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值