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

QAmqpClient not work with QtService #55

Closed
PaguaToP opened this issue Oct 4, 2016 · 3 comments
Closed

QAmqpClient not work with QtService #55

PaguaToP opened this issue Oct 4, 2016 · 3 comments

Comments

@PaguaToP
Copy link

PaguaToP commented Oct 4, 2016

Now QAmqpClient not work with QtService.
For connect I use code from tutorials/helloworld/receive:

  1. Single run tutorials receive worked
  2. Same code in my project with QtService not worked (in debug write connecting to host: ... and nothing happens)

Please, help me

@mbroadst
Copy link
Owner

mbroadst commented Oct 4, 2016

@PaguaToP you're going to have to provide more context or source code for your issue. I actually originally used this library in a project that used QtService, so I know that it works. It is likely you are having issues with threads or something similar - a code sample would probably resolve this.

@PaguaToP
Copy link
Author

PaguaToP commented Oct 4, 2016

My code

Receiver from examples:

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("myconnectstring");
    }

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;
};

My Service class:

class QAService : public QtService<QCoreApplication>
{
public:
    QAService(int argc, char *argv[]) : QtService<QCoreApplication>(argc, argv, "Qa Spider")
    {
        setServiceDescription("Qa Spider");
        setServiceFlags(QtServiceBase::CanBeSuspended);
    }
    ~QAService();

protected:
    void start() { receiver.start(); }
    void pause()  {  }
    void resume() {  }

private:
    Receiver receiver;
};

main.cpp:

int main(int argc, char *argv[])
{
#if !defined(Q_WS_WIN)
    // QtService stores service settings in SystemScope, which normally require root privileges.
    // To allow testing this example as non-root, we change the directory of the SystemScope settings file.
    QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope, QDir::tempPath());
#endif

    QAService service(argc, argv);
    return service.exec();
}

@PaguaToP
Copy link
Author

Fixed. I use static build library QAmqpClient

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