Struct riker::actor::Channel [−][src]
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).
Examples
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) } } // main let model: DefaultModel<String> = DefaultModel::new(); let sys = ActorSystem::new(&model).unwrap(); // start two instances of MyActor 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(); // start a channel let chan = sys.actor_of(Channel::props(), "my-channel").unwrap(); // subscribe actors to channel chan.tell(Subscribe("my-topic".into(), sub1), None); chan.tell(Subscribe("my-topic".into(), sub2), None); // publish a message let msg = Publish("my-topic".into(), "Remember the cant!".into()); chan.tell(msg, None);
Methods
impl<Msg: Message> Channel<Msg>
[src]
impl<Msg: Message> Channel<Msg>
pub fn new(event_stream: Option<ActorRef<Msg>>) -> BoxActor<Msg>
[src]
pub fn new(event_stream: Option<ActorRef<Msg>>) -> BoxActor<Msg>
pub fn props() -> BoxActorProd<Msg>
[src]
pub fn props() -> BoxActorProd<Msg>
Trait Implementations
impl<Msg: Message> Actor for Channel<Msg>
[src]
impl<Msg: Message> Actor for Channel<Msg>
type Msg = Msg
fn pre_start(&mut self, ctx: &Context<Self::Msg>)
[src]
fn pre_start(&mut self, ctx: &Context<Self::Msg>)
Invoked when an actor is being started by the system. Read more
fn other_receive(
&mut self,
_: &Context<Msg>,
msg: ActorMsg<Msg>,
sender: Option<ActorRef<Msg>>
)
[src]
fn other_receive(
&mut self,
_: &Context<Msg>,
msg: ActorMsg<Msg>,
sender: Option<ActorRef<Msg>>
)
Invoked when an actor receives a Riker predefined message Read more
fn system_receive(
&mut self,
_: &Context<Self::Msg>,
msg: SystemMsg<Self::Msg>,
sender: Option<ActorRef<Self::Msg>>
)
[src]
fn system_receive(
&mut self,
_: &Context<Self::Msg>,
msg: SystemMsg<Self::Msg>,
sender: Option<ActorRef<Self::Msg>>
)
Invoked when an actor receives a Riker system message Read more
fn receive(&mut self, _: &Context<Msg>, _: Msg, _: Option<ActorRef<Msg>>)
[src]
fn receive(&mut self, _: &Context<Msg>, _: Msg, _: Option<ActorRef<Msg>>)
Invoked when an actor receives a message Read more
fn post_start(&mut self, ctx: &Context<Self::Msg>)
[src]
fn post_start(&mut self, ctx: &Context<Self::Msg>)
Invoked after an actor has started. Read more
fn post_stop(&mut self)
[src]
fn post_stop(&mut self)
Invoked after an actor has been stopped.
fn persistence_conf(&self) -> Option<PersistenceConf>
[src]
fn persistence_conf(&self) -> Option<PersistenceConf>
Return a Some(PersistenceConf) to enable actor persistence. Read more
fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]
fn apply_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
Invoked after an event is successfully inserted into the event store. Read more
fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
[src]
fn replay_event(&mut self, ctx: &Context<Self::Msg>, evt: Self::Msg)
Invoked for each event when the actor is recovering. Read more
fn supervisor_strategy(&self) -> Strategy
[src]
fn supervisor_strategy(&self) -> Strategy
Return a supervisor strategy that will be used when handling failed child actors.