-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreliable_dispatch_consumer.rs
63 lines (52 loc) · 1.76 KB
/
reliable_dispatch_consumer.rs
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
use anyhow::Result;
use danube_client::{DanubeClient, SubType};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct MyMessage {
field1: String,
field2: i32,
}
#[tokio::main]
async fn main() -> Result<()> {
// Setup tracing
tracing_subscriber::fmt::init();
let client = DanubeClient::builder()
.service_url("http://127.0.0.1:6650")
.build()
.unwrap();
let topic = "/default/reliable_topic";
let consumer_name = "cons_reliable";
let subscription_name = "subs_reliable";
let mut consumer = client
.new_consumer()
.with_topic(topic)
.with_consumer_name(consumer_name)
.with_subscription(subscription_name)
.with_subscription_type(SubType::Exclusive)
.build();
// Subscribe to the topic
consumer.subscribe().await?;
println!("The Consumer {} was created", consumer_name);
// Start receiving messages
let mut message_stream = consumer.receive().await?;
let mut total_received_size = 0;
while let Some(message) = message_stream.recv().await {
let payload = message.payload.clone();
total_received_size += payload.len();
match String::from_utf8(payload) {
Ok(message_str) => {
println!(
"Received message: {:?} , from segment: {}, with offset: {}, total received bytes: {}",
message_str.split_once("!").unwrap().0,
&message.msg_id.segment_id,
&message.msg_id.segment_offset,
total_received_size
);
consumer.ack(&message).await?;
}
Err(e) => println!("Failed to convert Payload to String: {}", e),
}
}
Ok(())
}