Closed ximply closed 11 years ago
Move declare-bind chain in slot connected with signal "connected" from client.
Thanks for your answer! Today, I try it.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Step 1>> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// I modified the test.h and test.cpp Add a slot [ void connected(); ] to the test.h, Then I modified the test.cpp, just as follows,
Test::Test() { //QUrl con(QString("amqp://guest:guest@localhost:5672/"));
// to fit Qt 4.7.4
QUrl con;
con.setScheme("amqp");
con.setUserName("guest");
con.setPassword("guest");
con.setHost("192.168.84.100");
con.setPort(5672);
con.setPath("/");
client_ = new QAMQP::Client(this);
connect(client_, SIGNAL(connected()), this, SLOT(connected()));
client_->open(con);
exchange_ = client_->createExchange("test.test2");
exchange_->declare("fanout", QAMQP::Exchange::Durable);
queue_ = client_->createQueue("test.my_queue", exchange_->channelNumber());
//queue_->declare();
queue2 = client->createQueue("test.myqueue2"); //queue2->declare();
//exchange_->bind(queue_);
//exchange_->bind(queue2_);
connect(queue2_, SIGNAL(declared()), this, SLOT(declared()));
connect(queue_, SIGNAL(messageReceived()), this, SLOT(newMessage()));
connect(queue2_, SIGNAL(messageReceived()), this, SLOT(newMessage()));
}
Test::~Test() {
}
void Test::connected() { queue_->declare("test.myqueue", QAMQP::Queue::Durable); queue2->declare("test.my_queue2", QAMQP::Queue::Durable);
exchange_->bind(queue_);
exchange_->bind(queue2_);
}
void Test::declared() { qDebug("\t-= Ready =-"); //queue_->purge();
/*QAMQP::Exchange::MessageProperties properties;
properties[QAMQP::Frame::Content::cpDeliveryMode] = 2; // Make message persistent
exchange_->publish("123", exchange_->name(), properties);*/
//queue_->remove(true, false, false);
queue_->setQOS(0,10);
queue_->setConsumerTag("qamqp-consumer");
queue_->consume(QAMQP::Queue::coNoAck);
//queue->setNoAck(false); //queue->get();
queue2_->setQOS(0,10);
queue2_->setConsumerTag("qamqp-consumer2");
queue2_->consume(QAMQP::Queue::coNoAck);
//exchange_->remove(false, false);
}
void Test::newMessage() { QAMQP::Queue * q = qobject_cast<QAMQP::Queue *>(sender()); while (q->hasMessage()) { QAMQP::MessagePtr message = q->getMessage(); qDebug("+ RECEIVE MESSAGE"); qDebug("| Exchange-name: %s", qPrintable(message->exchangeName)); qDebug("| Routing-key: %s", qPrintable(message->routeKey)); qDebug("| Content-type: %s", qPrintable(message->property[QAMQP::Frame::Content::cpContentType].toString())); qDebug("| Msg Size: %d", message->payload.size()); qDebug("| Msg: " + message->payload);
if(!q->noAck())
{
q->ack(message);
}
}
} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Step 2>> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// I run the test QAMQP Client, I send message via the RabbitMQ Management and the Client can get the message. Now I disconnect the network of my PC and the Client print the DEBUG info [ AMQP Socket Error: "The remote host closed the connection" ], then I wait for the Client connection time out, when another DEBUG info [ AMQP Socket Error: "Connection timed out" ] print, I connect the network of my PC again and wait the next connection. Then this DEBUG info [ QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.84.100" AMQP Socket Error: "The remote host closed the connection" ] print. At last, I wait a new connection ...... It connects to the MQ Server again, then I send message via the RabbitMQ Management and the Client can get the message. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Summary>> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 1.My env: Qt 4.7.4, RabbitMQ Server 3.0.2, Windows 7 2.All the steps are right? ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Then this DEBUG info [ QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.84.100" AMQP Socket Error: "The remote host closed the connection" ] This I fix later. In other part , if client can receive message then all right.
Thanks very much!
when network disconnected. Then connected later. A problem come, the QAMQP client cannot get any message from queue again! I found that the channel doesn't opend again when connected again later.