Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use QAMQP not from main thread? #8

Open
OlegJakushkin opened this issue Dec 6, 2012 · 1 comment
Open

How to use QAMQP not from main thread? #8

OlegJakushkin opened this issue Dec 6, 2012 · 1 comment

Comments

@OlegJakushkin
Copy link

So I tried to modify test sample presented with lib to something like:

#include <QObject>
#include "qamqp/amqp.h"
#include "qamqp/amqp_exchange.h"
#include "qamqp/amqp_queue.h"
#include <QPointer>
#include <QThread>

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time.hpp>
class OutThread : public QThread
{
    Q_OBJECT
protected:
    QPointer<QAMQP::Client> client_out;
    QPointer<QAMQP::Exchange> exchange_out;

    void run()
    {
        std::cout << "w1" << std::endl;

        QUrl con(QString("amqp://guest:guest@localhost:5672/"));
        client_out = new QAMQP::Client(this);
        client_out->open(con);
        exchange_out =  client_out->createExchange("test.test2");
        exchange_out->declare("fanout");

        QAMQP::Exchange::MessageProperties properties;
        properties[QAMQP::Frame::Content::cpDeliveryMode] = 2; // Make message persistent
        std::cout << "w2" << std::endl;

        while(1){
            exchange_out->publish("123 123 123", exchange_out->name(), properties);
            boost::this_thread::sleep(boost::posix_time::milliseconds(1));
        }
        std::cout << "w3" << std::endl;

        exec();
    }
};

class Test : public QObject
{
    Q_OBJECT

public:
    Test(){}
    ~Test(){}

    Q_INVOKABLE void test() {
        std::cout << "t1" << std::endl;

        OutThread oThread;
        oThread.start();
        //InThread iThread;
        //iThread.start();
        std::cout << "t2" << std::endl;

        oThread.wait(); 
        std::cout << "t3" << std::endl;

    };
};

yet it cannot create a connection from another thread and talls me:

t1
t2
w1
QObject::setParent: Cannot set parent, new parent is in a different thread
w2

How to fix QAMQP in order to support new threads?

@fuCtor
Copy link
Owner

fuCtor commented Dec 7, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants