-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from vertexclique/manual-commit
Implement manual commit
- Loading branch information
Showing
5 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use callysto::futures::StreamExt; | ||
use callysto::prelude::message::*; | ||
use callysto::prelude::*; | ||
use std::sync::atomic::{AtomicU32, Ordering}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone)] | ||
struct SharedState { | ||
value: Arc<AtomicU32>, | ||
} | ||
|
||
impl SharedState { | ||
fn new() -> Self { | ||
Self { | ||
value: Arc::new(AtomicU32::default()), | ||
} | ||
} | ||
} | ||
|
||
async fn manual_commit_counter_agent(mut stream: CStream, ctx: Context<SharedState>) -> Result<()> { | ||
while let Some(msg) = stream.next().await { | ||
let m = msg.unwrap(); | ||
// Read the incoming bytes as string | ||
let strm = m.payload_view::<str>().unwrap().unwrap().to_owned(); | ||
println!("Received payload: `{}`", strm); | ||
|
||
// Increment message counter and print it. | ||
// Show how you can store an application state. | ||
let state = ctx.state(); | ||
let msgcount = state.value.fetch_add(1, Ordering::AcqRel); | ||
println!("Message count: `{}`", msgcount); | ||
|
||
// We are done here. | ||
ctx.commit_async(m).await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
let mut app = Callysto::with_state(SharedState::new()); | ||
|
||
let mut config = Config::default(); | ||
config.kafka_config.enable_auto_commit = false; | ||
|
||
app.with_name("manual-commit").with_config(config); | ||
app.agent( | ||
"manual_commit_counter_agent", | ||
app.topic("example"), | ||
manual_commit_counter_agent, | ||
); | ||
|
||
app.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters