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

creatQueue question #90

Open
ZhaoYouBo opened this issue Nov 5, 2024 · 1 comment
Open

creatQueue question #90

ZhaoYouBo opened this issue Nov 5, 2024 · 1 comment

Comments

@ZhaoYouBo
Copy link

Snipaste_2024-11-05_20-57-06The 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.

@WollZebra
Copy link
Collaborator

WollZebra commented Nov 25, 2024

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, "");
}
`

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