Poll Mode
Poll for KubeMQ queue messages with a configurable wait timeout that blocks until messages arrive, in Rust.
Overview
Poll mode is a pull-based way to consume queue messages: the consumer decides exactly when to ask for work and how much, instead of holding an open stream the broker pushes into. That control matters for batch jobs, cron-triggered workers, and any consumer that only runs intermittently and would rather ask "is there anything for me?" than keep a subscription alive.
A single call to receive_queue_messages sends a channel, a max item count, and a wait_time_seconds timeout; the broker holds the request open as a long poll and returns once enough messages are available or the timeout elapses, so the call never spins on an empty queue. The final argument is is_peek — leave it false to consume messages normally (acknowledged immediately upon delivery), or set it true to read them without removing them from the queue.
Gotchas: with is_peek false, messages are acknowledged the instant they're delivered — a crash mid-processing loses them, so there's no way to defer acknowledgment with this API; the timeout bounds latency, not throughput, so a small item count on a busy queue means many round trips; and an empty vector just means nothing arrived in that window, not that the queue is empty for good.
Prerequisites
- KubeMQ server running on
localhost:50000 - Rust SDK installed (
cargo add kubemq)
Code
use kubemq::prelude::*;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let channel = "rust-queues.poll-mode";
println!("Polling for messages (5 second timeout)...");
let messages = client.receive_queue_messages(channel, 10, 5, false).await?;
if messages.is_empty() {
println!("No messages available");
} else {
for m in &messages {
println!("Received: id={}, body={}", m.id, String::from_utf8_lossy(&m.body));
}
}
client.close().await?;
Ok(())
}How It Works
wait_time_secondscontrols how long the server waits for messages before returning.- Returns an empty vector if no messages are available within the timeout.
- Review timeouts, channel names, and client IDs before running against shared environments.
- Run the program while the server from the prerequisites is available.
Related
Was this page helpful?