1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! # Queues Batch Send and Receive
//!
//! Demonstrates sending and receiving queue messages in batches. Ten messages
//! are sent in a single batch call, then all are received together. Batch
//! operations reduce round-trips compared to individual sends.
//!
//! ## Expected Output
//!
//! ```text
//! Batch sent: id=<uuid>, is_error=false
//! Batch sent: id=<uuid>, is_error=false
//! ...
//! Received 10 messages from batch
//! ```
//!
//! ## Running
//!
//! Requires a running KubeMQ broker. By default connects to `localhost:50000`.
//! Override with `KUBEMQ_ADDRESS`:
//!
//! ```bash
//! KUBEMQ_ADDRESS=my-host:50000 cargo run --example queues_batch
//! ```
use kubemq::prelude::*;
use kubemq::QueueMessageBuilder;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let channel = "queues.batch.example";
// Build a batch of messages
let messages: Vec<QueueMessage> = (0..10)
.map(|i| {
QueueMessageBuilder::new()
.channel(channel)
.body(format!("batch-message-{}", i).into_bytes())
.build()
})
.collect();
// Send batch
let results = client.send_queue_messages(messages).await?;
for r in &results {
println!("Batch sent: id={}, is_error={}", r.message_id, r.is_error);
}
// Receive all
let received = client.receive_queue_messages(channel, 10, 5, false).await?;
println!("Received {} messages from batch", received.len());
client.close().await?;
Ok(())
}