Open ZhaoYouBo opened 3 weeks ago
Greetings, I do not know what happened before. I use it like this:
constructor:
{ ... m_RabbitMQ.setHost(m_Host); m_RabbitMQ.setUsername(m_User); m_RabbitMQ.setPassword(m_Pass); m_RabbitMQ.setAutoReconnect(true); ... }
` void bindToChanges(const QString &exchangeName ) { // exchangeName == "something.state"; createExchange(exchangeName, name); auto *queue = createQueue(l, exchangeName, name); bindQueueToExchange(queue, exchangeName, name); }
void createExchange(const RabbitMQContext &exchangeName, const FeatureName &name) { auto exc = m_RabbitMQ.createExchange(exchangeName); connect(exc, &QAmqpExchange::declared, exc, [exchangeName, exc, name] { qDebug() << "RabbitMQ [] Declared Exchange" << name << exchangeName << exc->isDeclared(); }); exc->declare(QAmqpExchange::FanOut, QAmqpExchange::Durable); }
QAmqpQueue createQueue(Bindable l, const RabbitMQContext &exchangeName, const FeatureName &name) { auto queue = m_RabbitMQ.createQueue(name + m_QueueSuffix.value()); emit queueCreated(queue); const auto rerequest = m_Feats.value(name).rabbitMqSendsSingleItemUpdates; disconnect(queue, 0, 0, 0); // in case this is a reconnect connect(queue, &QAmqpQueue::declared, this, [=] { connect(queue, &QAmqpQueue::messageReceived, this, [=] { auto data = queue->dequeue().payload(); qDebug() << "RabbitMQ update" << l->m_FeatureName << data.first(qMin(200, data.length())) << "..."; if (rerequest) { reRequestRestValue(name); return; } l->value = data; }); queue->consume(QAmqpQueue::coNoAck); qDebug() << "RabbitMQ [] Declared Queue" << l->m_FeatureName << queue->name(); }); queue->declare(); return queue; }
void bindQueueToExchange(QAmqpQueue queue, const RabbitMQContext &exchangeName, const FeatureName &name) { connect(queue, &QAmqpQueue::bound, this, [name, exchangeName, queue] { qDebug() << "RabbitMQ [] Bound Queue" << name << queue->name() << "to exchange " << exchangeName << " waiting for updates"; }); queue->bind(exchangeName, ""); } `
The local server 127.0.0.1 already has a queue, but when I use creatQueue, it doesn't detect it and continues to create a new one.