Auto Ack
Automatic acknowledgment of queue messages on receipt
Overview
Auto-ack is the fire-and-forget receive mode for queues: the broker marks a message as consumed the instant it hands it to your client, instead of waiting for your code to settle it. Reach for it when the work is idempotent, low-value, or cheap to lose — a metrics ping, a cache warm, a best-effort notification — and you'd rather not carry the bookkeeping of explicit acknowledgment for every message.
It works by setting auto_ack = true on the PollRequest passed to PollQueue(). With it enabled, delivery and acknowledgment happen as one atomic step on the broker side, so there's no separate ack call and no in-flight "pending" state for the message to sit in.
Gotchas: if your consumer crashes after PollQueue() returns but before it finishes processing, that message is gone for good — auto-ack gives you no chance to nack or requeue it, unlike Ack & Reject. It's an at-most-once model, so never use it for messages where losing one silently would matter. And because acknowledgment happens on delivery, max_items and wait_timeout_seconds are your only throttles — there's no visibility-timeout window to tune.
Prerequisites
- KubeMQ server running on
localhost:50000 - C++ SDK installed (vcpkg or CMake FetchContent)
- C++17 compiler (GCC 9+, Clang 9+, MSVC 2019+)
Code
// Example: queues_stream/auto_ack
//
// Demonstrates receiving queue messages with automatic acknowledgment.
// When AutoAck is true, messages are automatically acknowledged upon receipt.
//
// Channel: cpp-queues.auto-ack
// Client ID: cpp-queues-auto-ack-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).
#include <kubemq/kubemq.h>
#include <iostream>
int main() {
std::cout << "[1] Connecting to localhost:50000" << std::endl;
kubemq::ClientOptions options;
options.set_address("localhost", 50000);
options.set_client_id("cpp-queues-auto-ack-client");
auto client_result = kubemq::Client::Create(options);
if (!client_result.ok()) {
std::cerr << "[ERROR] Failed to create client: " << client_result.status().message()
<< std::endl;
return 1;
}
auto& client = *client_result;
std::string channel = "cpp-queues.auto-ack";
// Send a message to the queue.
std::cout << "[2] Sending message to queue " << channel << std::endl;
auto msg_result =
kubemq::QueueMessage::Builder().SetChannel(channel).SetBody("auto-ack message").Build();
if (!msg_result.ok()) {
std::cerr << "[ERROR] Build message: " << msg_result.status().message() << std::endl;
return 1;
}
auto send_result = client->SendQueueMessage(*msg_result);
if (!send_result.ok()) {
std::cerr << "[ERROR] SendQueueMessage: " << send_result.status().message() << std::endl;
return 1;
}
std::cout << "[3] Message sent" << std::endl;
// Poll with auto-ack -- messages are acknowledged automatically on receipt.
std::cout << "[4] Polling with auto_ack=true" << std::endl;
kubemq::PollRequest poll_req;
poll_req.channel = channel;
poll_req.max_items = 10;
poll_req.wait_timeout_seconds = 5;
poll_req.auto_ack = true;
auto poll_result = client->PollQueue(poll_req);
if (!poll_result.ok()) {
std::cerr << "[ERROR] PollQueue: " << poll_result.status().message() << std::endl;
return 1;
}
if (poll_result->is_error()) {
std::cerr << "[ERROR] Poll response: " << poll_result->error() << std::endl;
return 1;
}
std::cout << "[5] Auto-ack: received " << poll_result->messages().size()
<< " messages (automatically acknowledged)" << std::endl;
for (const auto& dm : poll_result->messages()) {
std::cout << " body=" << dm.message().body() << std::endl;
}
// Close the client explicitly.
// Note: The Client destructor also calls Close(), but explicit
// cleanup is shown here for clarity and to match Go's defer pattern.
auto close_status = client->Close();
if (!close_status.ok()) {
std::cerr << "[ERROR] Close failed: " << close_status.message() << std::endl;
return 1;
}
std::cout << "[6] Client closed" << std::endl;
return 0;
}How It Works
- Polls with
auto_ack = truein thePollRequest. - Messages are automatically acknowledged upon receipt -- no manual ack needed.
- Simplifies consumption when message loss on failure is acceptable.
- Uses
PollQueue()as a convenience wrapper around the downstream receiver.
Related
Was this page helpful?