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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! # Pattern — Request/Reply
//!
//! Demonstrates the request/reply RPC pattern using queries. A query handler
//! acts as a server returning data, while the client sends queries and reads
//! the response body — classic synchronous RPC over messaging.
//!
//! ## Expected Output
//!
//! ```text
//! Request: get-user-info
//! Reply: user-info:id=42,name=Alice
//! Request: get-user-info
//! Reply: user-info:id=42,name=Alice
//! ```
//!
//! ## 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 pattern_request_reply
//! ```
use kubemq::prelude::*;
use kubemq::{QueryBuilder, QueryReplyBuilder};
use std::time::Duration;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let channel = "patterns.request_reply.example";
// Server: subscribe and respond to queries
let rc = client.clone();
let sub = client
.subscribe_to_queries(
channel,
"",
move |query| {
let c = rc.clone();
Box::pin(async move {
let reply = QueryReplyBuilder::new()
.request_id(&query.id)
.response_to(&query.response_to)
.body(b"user-info:id=42,name=Alice".to_vec())
.build();
tokio::spawn(async move {
let _ = c.send_query_response(reply).await;
});
})
},
None,
)
.await?;
tokio::time::sleep(Duration::from_millis(500)).await;
// Client: send queries and read replies
for _ in 0..2 {
let query = QueryBuilder::new()
.channel(channel)
.body(b"get-user-info".to_vec())
.timeout(Duration::from_secs(10))
.build();
println!("Request: get-user-info");
let response = client.send_query(query).await?;
println!("Reply: {}", String::from_utf8_lossy(&response.body));
}
sub.unsubscribe().await;
client.close().await?;
Ok(())
}