win10 QT 使用rabbitmq小结

qt使用rabbitmq。部署rabbitmq暂时不表,网上资料很多
官网
http://www.rabbitmq.com/
有开发介绍。

源码自取
https://download.csdn.net/download/cubmonk/12116702

下载rabbitmq源码。编译库。

需要自己重新编译,因为每个人的qt版本不一样,编译出来可能会有区别。
下载地址:https://github.com/mbroadst/qamqp
进入src目录,打开pro工程文件构建工程,会生成库文件,将库库文件还有源码里的头文件打包放在自己工程目录下面。在qt项目文件内添加一下代码,根据自己的路径修改。。

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-untitled1-Desktop_Qt_5_13_1_MinGW_64_bit-Release/bin/ -lqamqp
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-untitled1-Desktop_Qt_5_13_1_MinGW_64_bit-Release/bin/ -lqamqpd

INCLUDEPATH += $$PWD/../build-untitled1-Desktop_Qt_5_13_1_MinGW_64_bit-Release/QAMQP
DEPENDPATH += $$PWD/../build-untitled1-Desktop_Qt_5_13_1_MinGW_64_bit-Release/QAMQP

2测试基本工程

测试很简单,先使用,最基本的pc模式,官网helloword历程。
先打开rabbitmq管理页面,添加用户,virtualhost,设置完成后

在这里插入图片描述

QAmqpClient m_client;

    m_client.setHost("127.0.0.1");
    m_client.setUsername("python_c");
    m_client.setPassword("python_c");
    m_client.setVirtualHost("anodize");
    connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));

    m_client.connectToHost();


void QRabbitMQ::clientConnected()
{
    /*
    */
    qDebug() << "RabbitMQ connect to server ok";
}

在这里插入图片描述
这样就算连接上了

3发送消息

1/设置参数

    m_client.setHost("127.0.0.1");
    m_client.setUsername("python_c");
    m_client.setPassword("python_c");
    m_client.setVirtualHost("anodize");
    connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));
    m_client.connectToHost();

2/创建交换机和queue

void QRabbitMQ::clientConnected()
{
    QAmqpQueue *queue = m_client.createQueue("hello");
    connect(queue, SIGNAL(declared()), this, SLOT(queueDeclared()));
    queue->declare();
    qDebug() << "RabbitMQ connect to server ok";
}

3/发送消息到指定queue

void QRabbitMQ::queueDeclared()
{
    QAmqpQueue *queue = qobject_cast<QAmqpQueue*>(sender());
    if (!queue)
        return;
    QAmqpExchange *defaultExchange = m_client.createExchange();
    defaultExchange->publish("Hello World!", "hello");
    qDebug() << " [x] Sent 'Hello World!'";
    defaultExchange->close();
}

在这里插入图片描述
这样在当前queue内就出现了一条消息

在这里插入图片描述

4接收消息

接收消息使用官方提供的源码即可


#include "qamqpclient.h"
#include "qamqpexchange.h"
#include "qamqpqueue.h"

class Receiver : public QObject
{
    Q_OBJECT
public:
    Receiver(QObject *parent = 0) : QObject(parent) {
      m_client.setAutoReconnect(true);
    }

public Q_SLOTS:
    void start() {
        connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));
        m_client.connectToHost();
    }

private Q_SLOTS:
    void clientConnected() {
        QAmqpQueue *queue = m_client.createQueue("hello");
        disconnect(queue, 0, 0, 0); // in case this is a reconnect
        connect(queue, SIGNAL(declared()), this, SLOT(queueDeclared()));
        queue->declare();
    }

    void queueDeclared() {
        QAmqpQueue *queue = qobject_cast<QAmqpQueue*>(sender());
        if (!queue)
            return;

        connect(queue, SIGNAL(messageReceived()), this, SLOT(messageReceived()));
        queue->consume(QAmqpQueue::coNoAck);
        qDebug() << " [*] Waiting for messages. To exit press CTRL+C";
    }

    void messageReceived() {
        QAmqpQueue *queue = qobject_cast<QAmqpQueue*>(sender());
        if (!queue)
            return;

        QAmqpMessage message = queue->dequeue();
        qDebug() << " [x] Received " << message.payload();
    }

private:
    QAmqpClient m_client;

};

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    Receiver receiver;
    receiver.start();
    return app.exec();
}

其实官方把各种应用都告诉你了,看原理去rabbtmq官网,看应用直接看这个源文件github地址,里面6种应用都有
https://github.com/mbroadst/qamqp

在这里插入图片描述

以上仅是最简单的应用,仅供参考

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值