1
1
use futures:: StreamExt ;
2
2
use rabbitmq_stream_client:: error:: StreamCreateError ;
3
3
use rabbitmq_stream_client:: types:: { ByteCapacity , OffsetSpecification , ResponseCode } ;
4
- use std:: io:: stdin;
5
4
use std:: sync:: atomic:: { AtomicI64 , Ordering } ;
6
5
use std:: sync:: Arc ;
6
+ use tokio:: sync:: Notify ;
7
7
use tokio:: task;
8
8
9
9
#[ tokio:: main]
@@ -14,6 +14,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
14
14
let received_messages = Arc :: new ( AtomicI64 :: new ( -1 ) ) ;
15
15
let first_offset = Arc :: new ( AtomicI64 :: new ( -1 ) ) ;
16
16
let last_offset = Arc :: new ( AtomicI64 :: new ( -1 ) ) ;
17
+ let notify_on_close = Arc :: new ( Notify :: new ( ) ) ;
17
18
let create_response = environment
18
19
. stream_creator ( )
19
20
. max_length ( ByteCapacity :: GB ( 2 ) )
@@ -41,8 +42,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
41
42
. await
42
43
. unwrap ( ) ;
43
44
44
- println ! ( "Starting consuming" ) ;
45
- println ! ( "Press any key to close the consumer" ) ;
45
+ println ! ( "Started consuming" ) ;
46
46
47
47
let mut stored_offset: u64 = consumer. query_offset ( ) . await . unwrap_or_else ( |_| 0 ) ;
48
48
@@ -59,13 +59,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
59
59
60
60
let first_cloned_offset = first_offset. clone ( ) ;
61
61
let last_cloned_offset = last_offset. clone ( ) ;
62
+ let notify_on_close_cloned = notify_on_close. clone ( ) ;
62
63
63
64
task:: spawn ( async move {
64
65
while let Some ( delivery) = consumer. next ( ) . await {
65
66
let d = delivery. unwrap ( ) ;
66
67
67
68
if first_offset. load ( Ordering :: Relaxed ) == -1 {
68
- println ! ( "consuming first message" ) ;
69
+ println ! ( "First message received " ) ;
69
70
_ = first_offset. compare_exchange (
70
71
first_offset. load ( Ordering :: Relaxed ) ,
71
72
d. offset ( ) as i64 ,
@@ -85,13 +86,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
85
86
last_offset. store ( d. offset ( ) as i64 , Ordering :: Relaxed ) ;
86
87
let handle = consumer. handle ( ) ;
87
88
_ = handle. close ( ) . await ;
89
+ notify_on_close_cloned. notify_one ( ) ;
88
90
89
91
}
90
92
}
91
93
}
92
94
} ) ;
93
95
94
- _ = stdin ( ) . read_line ( & mut "" . to_string ( ) ) ;
96
+ notify_on_close . notified ( ) . await ;
95
97
96
98
if first_cloned_offset. load ( Ordering :: Relaxed ) != -1 {
97
99
println ! (
0 commit comments