qamqp/tutorials/helloworld/receive/main.cpp

82 lines
2.0 KiB
C++
Raw Normal View History

#include <QCoreApplication>
#include <QDebug>
#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()));
2025-07-31 17:17:40 +08:00
// queue->consume(QAmqpQueue::coNoAck);
// queue->consume(QAmqpQueue::coNoLocal);
qint32 sizeQueue = queue->messageCount();
while (sizeQueue--) {
queue->get(false);
}
qDebug() << " [*] Waiting for messages. To exit press CTRL+C";
2025-07-31 17:17:40 +08:00
queue->ack(3, false); // Acknowledgement the 3rd message.
queue->reopen();
// m_client.disconnectFromHost();
}
void messageReceived() {
QAmqpQueue *queue = qobject_cast<QAmqpQueue*>(sender());
if (!queue)
return;
QAmqpMessage message = queue->dequeue();
2025-07-31 17:17:40 +08:00
qDebug() << " [x] Received in" << message.payload();
int input=0;
// std::scanf("%d", &input);
qDebug() << " [x] Received out, " << message.deliveryTag() << " | " << message.payload() << " , input = " << input;
}
private:
QAmqpClient m_client;
};
int main(int argc, char **argv)
{
2025-07-31 17:17:40 +08:00
qDebug() << " Recieve starts ... ";
QCoreApplication app(argc, argv);
Receiver receiver;
receiver.start();
return app.exec();
}
#include "main.moc"