1
1
use clap:: { App , Arg } ;
2
- use log:: { debug, error , info} ;
2
+ use log:: { debug, info} ;
3
3
use rdkafka:: config:: { ClientConfig , RDKafkaLogLevel } ;
4
4
use rdkafka:: consumer:: stream_consumer:: StreamConsumer ;
5
- use rdkafka:: consumer:: CommitMode ;
6
5
use rdkafka:: consumer:: Consumer ;
7
6
use rdkafka:: producer:: FutureProducer ;
8
7
use rdkafka:: util:: get_rdkafka_version;
9
- use rust_arroyo:: backends:: kafka:: create_kafka_message;
10
8
use rust_arroyo:: backends:: AssignmentCallbacks ;
11
- use rust_arroyo:: processing:: strategies:: async_noop:: build_topic_partitions;
12
9
use rust_arroyo:: processing:: strategies:: async_noop:: AsyncNoopCommit ;
13
10
use rust_arroyo:: processing:: strategies:: async_noop:: CustomContext ;
11
+ use rust_arroyo:: processing:: StreamingStreamProcessor ;
14
12
use rust_arroyo:: types:: { Partition , Topic } ;
15
13
use std:: collections:: HashMap ;
16
- use std:: time:: Duration ;
17
14
use std:: time:: SystemTime ;
18
- use tokio:: time:: timeout;
19
15
20
16
// A type alias with your custom consumer can be created for convenience.
21
17
type LoggingConsumer = StreamConsumer < CustomContext > ;
@@ -71,7 +67,7 @@ async fn consume_and_produce(
71
67
) ;
72
68
let batch = Vec :: new ( ) ;
73
69
74
- let mut strategy = AsyncNoopCommit {
70
+ let strategy = AsyncNoopCommit {
75
71
topic : topic_clone,
76
72
producer,
77
73
batch,
@@ -80,35 +76,16 @@ async fn consume_and_produce(
80
76
dest_topic : dest_topic. to_string ( ) ,
81
77
source_topic : source_topic. to_string ( ) ,
82
78
} ;
83
- loop {
84
- match timeout ( Duration :: from_secs ( 2 ) , consumer. recv ( ) ) . await {
85
- Ok ( result) => match result {
86
- Err ( e) => panic ! ( "Kafka error: {}" , e) ,
87
- Ok ( m) => {
88
- match strategy. poll ( ) . await {
89
- Some ( partition_list) => {
90
- let part_list = build_topic_partitions ( partition_list) ;
91
- consumer. commit ( & part_list, CommitMode :: Sync ) . unwrap ( ) ;
92
- info ! ( "Committed: {:?}" , part_list) ;
93
- }
94
- None => { }
95
- }
96
79
97
- strategy. submit ( create_kafka_message ( m) ) . await ;
98
- }
99
- } ,
100
- Err ( _) => {
101
- error ! ( "timeoout, flushing batch" ) ;
102
- match strategy. poll ( ) . await {
103
- Some ( partition_list) => {
104
- let part_list = build_topic_partitions ( partition_list) ;
105
- consumer. commit ( & part_list, CommitMode :: Sync ) . unwrap ( ) ;
106
- info ! ( "Committed: {:?}" , part_list) ;
107
- }
108
- None => { }
109
- }
110
- }
111
- }
80
+ let mut processor = StreamingStreamProcessor {
81
+ consumer,
82
+ strategy,
83
+ shutdown_requested : false ,
84
+ } ;
85
+
86
+ match processor. run ( ) . await {
87
+ Ok ( _) => { }
88
+ Err ( _) => panic ! ( "Kafka error" ) ,
112
89
}
113
90
}
114
91
0 commit comments