pub struct Channel<Msg: Message> { /* fields omitted */ }
A specialized actor for providing Publish/Subscribe capabilities to users.
It is a common actor pattern to provide pub/sub features to other actors
especially in cases where choreography (instead of orchestration) is used.
See: Service Choreography
A channel can be started as you would any other actor. A channel expects
ChannelMsg
messages.
To publish a message to a channel you send the channel a ChannelMsg::Publish
message containing the topic and the message to publish.
A published message is cloned and sent to each subscriber to the channel where
the topic matches.
To subscribe to a channel you send the channel a ChannelMsg::Subscribe
message containing the topic to subscribe to and an ActorRef
of the
subscriber (e.g. .myself()
).
Since channels are actors themselves they provide excellent lightweight
facilitators of distributing data among actors that are working together to
complete a single goal or interaction (even short lived interactions).
use riker::actors::ChannelMsg::*;
struct MyActor;
impl Actor for MyActor {
type Msg = String;
fn receive(&mut self,
ctx: &Context<Self::Msg>,
msg: Self::Msg,
sender: Option<ActorRef<Self::Msg>>) {
println!("Received msg {:?}", msg);
}
}
impl MyActor {
fn actor() -> BoxActor<String> {
Box::new(MyActor)
}
}
let model: DefaultModel<String> = DefaultModel::new();
let sys = ActorSystem::new(&model).unwrap();
let props = Props::new(Box::new(MyActor::actor));
let sub1 = sys.actor_of(props.clone(), "sub1").unwrap();
let sub2 = sys.actor_of(props, "sub2").unwrap();
let chan = sys.actor_of(Channel::props(), "my-channel").unwrap();
chan.tell(Subscribe("my-topic".into(), sub1), None);
chan.tell(Subscribe("my-topic".into(), sub2), None);
let msg = Publish("my-topic".into(), "Remember the cant!".into());
chan.tell(msg, None);
type Msg = Msg
Invoked when an actor is being started by the system. Read more
Invoked when an actor receives a Riker predefined message Read more
Invoked when an actor receives a Riker system message Read more
Invoked when an actor receives a message Read more
Invoked after an actor has started. Read more
Invoked after an actor has been stopped.
Return a Some(PersistenceConf) to enable actor persistence. Read more
Invoked after an event is successfully inserted into the event store. Read more
Invoked for each event when the actor is recovering. Read more
Return a supervisor strategy that will be used when handling failed child actors.
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)