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
//! # Commands — Timeout
//!
//! Demonstrates command timeout behavior. A command is sent to a channel with
//! no handler, using a short timeout. The broker returns a timeout error since
//! no responder processes the command.
//!
//! ## Expected Output
//!
//! ```text
//! Sending command with 2s timeout and no handler...
//! Command timed out (expected): <error message>
//! ```
//!
//! ## 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 commands_timeout
//! ```
use kubemq::prelude::*;
use kubemq::CommandBuilder;
use std::time::Duration;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let channel = "commands.timeout.example";
println!("Sending command with 2s timeout and no handler...");
let command = CommandBuilder::new()
.channel(channel)
.body(b"will-timeout".to_vec())
.timeout(Duration::from_secs(2))
.build();
match client.send_command(command).await {
Ok(resp) => println!(
"Response (unexpected): executed={}, error='{}'",
resp.executed, resp.error
),
Err(e) => println!("Command timed out (expected): {}", e),
}
client.close().await?;
Ok(())
}