Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. AMQP-CPP ack not work
Forum Update on Monday, May 27th 2025

AMQP-CPP ack not work

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 568 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Pang
    wrote on last edited by
    #1

    #include <QCoreApplication>
    #include <QDebug>
    #include <QString>
    #include <QSocketNotifier>
    #include <amqpcpp.h>
    #include <amqpcpp/linux_tcp.h>

    class MyTcpHandler : public AMQP::TcpHandler
    {
    virtual void monitor(AMQP::TcpConnection *connection, int fd, int flags) override
    {
    auto tun_fd_monitor = new QSocketNotifier(fd, QSocketNotifier::Read, nullptr);
    QObject::connect(tun_fd_monitor, &QSocketNotifier::activated, [connection, flags](int fd){
    connection->process(fd, flags);
    });
    tun_fd_monitor = new QSocketNotifier(fd, QSocketNotifier::Write, nullptr);
    QObject::connect(tun_fd_monitor, &QSocketNotifier::activated, [connection, flags](int fd){
    connection->process(fd, AMQP::writable);
    });
    }
    };
    int main(int argc, char *argv[])
    {

    QCoreApplication a(argc, argv);
    MyTcpHandler myHandler;
    
    // address of the server
    AMQP::Address address("amqp://guest:guest@localhost/");
    
    // create a AMQP connection object
    AMQP::TcpConnection connection(&myHandler, address);
    
    // and create a channel
    AMQP::TcpChannel channel(&connection);
    
    // use the channel object to call the AMQP method you like
    channel.declareExchange("my-exchange", AMQP::fanout);
    channel.declareQueue("my-queue");
    channel.bindQueue("my-exchange", "my-queue", "");
    
    // callback function that is called when the consume operation starts
    auto startCb = [](const std::string &consumertag) {
        
        std::cout << "consume operation started" << std::endl;
    };
    
    // callback function that is called when the consume operation failed
    auto errorCb = [](const char *message) {
        
        std::cout << "consume operation failed" << std::endl;
    };
    
    // callback operation when a message was received
    auto messageCb = [&channel](const AMQP::Message &message, uint64_t deliveryTag, bool redelivered) {
        
        std::cout << "message received: " << message.body()<<std::endl;
        
        // acknowledge the message
        qWarning()<<channel.ack(deliveryTag);
    };
    
    // start consuming from the queue, and install the callbacks
    channel.consume("my-queue")
    .onReceived(messageCb)
    .onSuccess(startCb)
    .onError(errorCb);
    
    // start a transaction
    channel.startTransaction();
    
    // publish a number of messages
    channel.publish("my-exchange", "my-key", "my first message");
    channel.publish("my-exchange", "my-key", "another message");
    
    // commit the transactions, and set up callbacks that are called when
    // the transaction was successful or not
    channel.commitTransaction()
    .onSuccess([]() {
        qWarning()<<__LINE__;
    })
    .onError([](const char *message) {
        // none of the messages were published
        // now we have to do it all over again
        qWarning()<<__LINE__<<message;
    });
    return a.exec();
    

    }

    Here is my code. I can consume the msg, but rabbitmq-server docs not remove the msg from queue. I guess channel.ack(deliveryTag) has something wrong.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you have the same code not using Qt working ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved