lets brush up on scala actors

ACTORS:

is a computation unit with state, behavior, and its own mailbox.

Behavior : the computation logic that needs to be executed in response to the message received.

State: contain some variables

1.Communication between actors can be asynchronous or synchronous.

2.Every actor is attached to exactly one mailbox.

3.When the message is sent to the actor, the message gets enqueued in its mailbox.

4.The mailboxes can be bounded or unbounded.

5.A bounded mailbox limits the number of messages that can be queued in the mailbox.

6.Messages are processed in the same order as they are enqueued in the mailbox.

7.Akka provides a priority mailbox where the messages are enqueued based on the assigned priority.

8.Actors can change their state and behavior based on the message passed.

9.Actors do not share state.

10.All messages passed should be immutable.

11.no guarantee on the sequence of the message arrival and execution.

12.An actor can change another actor’s state only by sending it a message.

11

An actor’s lifecycle:

1.is initialized and started.

2.receives and processes messages by executing a specific behavior.

3.stops itself when it receives a termination message.

4.preStart() and postStop() can be implemented to initialize/clean any resources used by the actor to process the messages.

5.preRestart() and postRestart() allow the actor to manage the state in case an exception has been raised and Supervisor actor restarts the actor.

Actor termination:

1.stops processing the mailbox messages.

2.sends the STOP signal to all the children.

3.waits for termination message from all its children.

4.Invoking the postStop() method.

5.Informing the supervisor about self-termination.

6.additional messages aren’t processed, they’re sent to the deadLetters actor of the ActorSystem.

An actor system:

1.is a hierarchical group of actors that share a common configuration.

2.Actors are organized in a hierarchy via the actor system.

3.At the top of the hierarchy is the guardian actor, created automatically with each actor System.

4.All other actors created by the given actor system become the child of the guardian actor.

5.In the actor system, each actor has one supervisor (the parent actor) that automatically takes care of the fault handling.

An actor path : uniquely identifies an actor in the actor system.

33

/user: The Guardian Actor

1.Actors created using system.actorOf() are children of this actor.

2.when this guardian terminates, all normal actors in the system will be shutdown too.

/system: The System Guardian

1.watch the user guardian and initiate its own shut-down upon reception of the Terminated message.

/: The Root Guardian

1.the grand-parent of all so-called “top-level” actors and supervises all the special actors.

22

ActorRef:

1.Actors are proxied via ActorRef.

2.is to send messages to the actor.

3.acts as a protection layer so you can’t access the actor directly and mutate its state.

4.It is immutable.

5.It has a one-to-one relationship with the Actor it represents.

6.It is serializable and network-aware. This lets you pass the ActorRef around the Network.

 Dispatcher:

1.Message passing to the actors happens using dispatchers.

2.Its responsibility is to send a message to the actor’s mailbox and execute the actor by invoking the receive block.

3.is backed by a thread pool.

Sending a message to an actor:

44

1.When you send a message to an actor, you’re only sending it to its ActorRef.

2.the ActorRef sends the message to the Dispatcher associated with the actor.

3.Dispatcher immediately queues the message in the mailbox of the actor.

4.If a free thread is available in the thread pool that thread is selected for execution of the actor.

5.If all the threads are busy, the actor will be executed when threads becomes available.

6.The available thread reads the messages from the mailbox.

7.messages can be passed to an actor in two modes:

Fire and forget:

where the producer of the message expects no reply from the consumer.

Send and receive:

the producer of the message expects a reply from the consumer and will wait for that reply.

 supervisor:

1.An actor has one parent (supervisor): the actor that created it.

2.start,stop, and monitor child actors.

SUPERVISION STRATEGIES:

All-for-One: all actors supervised by a supervisor are restarted when one of the actors dies

One-for-One: is default, each actor has one supervisor.

55

66

2 thoughts on “lets brush up on scala actors

Leave a comment