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]

Trait Implementations

impl<Msg: Message> Actor for Channel<Msg>
[src]

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.

Auto Trait Implementations

impl<Msg> Send for Channel<Msg>

impl<Msg> Sync for Channel<Msg>