Created
May 26, 2023 20:45
-
-
Save hietalajulius/2258e850e1a3bc49eb0677fe2d6cfdf9 to your computer and use it in GitHub Desktop.
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
use actix::{Actor, Context, Handler}; | |
use actix_broker::{BrokerIssue, BrokerSubscribe}; | |
use actix_rt::System; | |
use std::println; | |
#[derive(actix::Message, Clone, Debug)] | |
#[rtype(result = "()")] | |
struct Message1 { | |
pub level: i32, | |
} | |
#[derive(actix::Message, Clone, Debug)] | |
#[rtype(result = "()")] | |
struct Message2 { | |
pub level: i32, | |
pub from: String, | |
} | |
struct Actor1; | |
struct Actor2 { | |
pub name: String, | |
} | |
impl Actor for Actor1 { | |
type Context = Context<Self>; | |
fn started(&mut self, ctx: &mut Self::Context) { | |
self.subscribe_system_sync::<Message2>(ctx); | |
//self.subscribe_system_async::<Message2>(ctx); //Async works with multiple consumers of Message1 | |
println!("Actor1 - Subscribed to Message2"); | |
} | |
} | |
impl Actor for Actor2 { | |
type Context = Context<Self>; | |
fn started(&mut self, ctx: &mut Self::Context) { | |
self.subscribe_system_sync::<Message1>(ctx); | |
//self.subscribe_system_async::<Message1>(ctx); //Async works with multiple consumers of Message1 | |
println!("Actor2 ({:?}) - Subscribed to Message1", self.name); | |
} | |
} | |
impl Handler<Message2> for Actor1 { | |
type Result = (); | |
fn handle(&mut self, msg: Message2, ctx: &mut Self::Context) -> Self::Result { | |
//Shutdown at level 4 | |
if msg.level > 5 { | |
System::current().stop(); | |
}; | |
println!( | |
"Actor1 received level - {:?} from {:?}", | |
msg.level, msg.from | |
); | |
self.issue_system_sync::<Message1>( | |
Message1 { | |
level: msg.level + 1, | |
}, | |
ctx, | |
); | |
//self.issue_system_async::<Message1>(Message1 { level: msg.level + 1 }); //Async works with multiple consumers of Message1 | |
} | |
} | |
impl Handler<Message1> for Actor2 { | |
type Result = (); | |
fn handle(&mut self, msg: Message1, ctx: &mut Self::Context) -> Self::Result { | |
println!("Actor2 received level {:?} - {:?}", self.name, msg.level); | |
self.issue_system_sync::<Message2>( | |
Message2 { | |
level: msg.level + 1, | |
from: self.name.clone(), | |
}, | |
ctx, | |
); | |
//self.issue_system_async::<Message2>(Message2 { level: msg.level + 1, from: self.name.clone() }); //Async works with multiple consumers of Message1 | |
} | |
} | |
fn main() { | |
let system = actix::System::new(); | |
system.block_on(async { | |
//The start order matters? | |
let actor2_a = Actor2 { | |
name: "a".to_string(), | |
} | |
.start(); | |
let actor2_b = Actor2{name: "b".to_string()}.start(); //Async works with multiple consumers of Message1, commenting this line out also works with sync | |
let actor1 = Actor1 {}.start(); | |
let init = Message2 { | |
level: 1, | |
from: "init".to_string(), | |
}; | |
actor1.send(init).await.unwrap(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment