2014-06-18 22:31:28 +08:00
|
|
|
#include <QCoreApplication>
|
2014-06-18 23:06:49 +08:00
|
|
|
#include <QStringList>
|
2014-06-18 22:31:28 +08:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
2014-06-18 23:06:49 +08:00
|
|
|
#include "amqp_client.h"
|
|
|
|
|
#include "amqp_exchange.h"
|
|
|
|
|
#include "amqp_queue.h"
|
|
|
|
|
using namespace QAMQP;
|
|
|
|
|
|
|
|
|
|
class TaskCreator : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
TaskCreator(QObject *parent = 0) : QObject(parent) {}
|
|
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
|
void start() {
|
|
|
|
|
connect(&m_client, SIGNAL(connected()), this, SLOT(clientConnected()));
|
|
|
|
|
connect(&m_client, SIGNAL(disconnected()), qApp, SLOT(quit()));
|
|
|
|
|
m_client.connectToHost();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
|
void clientConnected() {
|
|
|
|
|
Queue *queue = m_client.createQueue("task_queue");
|
|
|
|
|
connect(queue, SIGNAL(declared()), this, SLOT(queueDeclared()));
|
|
|
|
|
queue->declare();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void queueDeclared() {
|
|
|
|
|
Queue *queue = qobject_cast<Queue*>(sender());
|
|
|
|
|
if (!queue)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Exchange *defaultExchange = m_client.createExchange();
|
|
|
|
|
MessageProperties properties;
|
|
|
|
|
properties[Frame::Content::cpDeliveryMode] = "2"; // make message persistent
|
|
|
|
|
|
|
|
|
|
QString message;
|
|
|
|
|
if (qApp->arguments().size() < 2)
|
|
|
|
|
message = "Hello World!";
|
|
|
|
|
else
|
|
|
|
|
message = qApp->arguments().at(1);
|
|
|
|
|
|
|
|
|
|
defaultExchange->publish(message, "task_queue", properties);
|
|
|
|
|
qDebug(" [x] Sent '%s'", message.toLatin1().constData());
|
|
|
|
|
m_client.disconnectFromHost();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Client m_client;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2014-06-18 22:31:28 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
QCoreApplication app(argc, argv);
|
2014-06-18 23:06:49 +08:00
|
|
|
TaskCreator taskCreator;
|
|
|
|
|
taskCreator.start();
|
|
|
|
|
return app.exec();
|
2014-06-18 22:31:28 +08:00
|
|
|
}
|
2014-06-18 23:06:49 +08:00
|
|
|
|
|
|
|
|
#include "main.moc"
|