An actor is an object that receives messages and takes actions to handle them. It is decoupled from the source of the message and its only responsibility is to properly recognise the type of message it has received and take action accordingly.
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Mute
Please keep your window on mute
Avoid Disturbance
Avoid leaving your window
unmuted after asking a question
3. Agenda
01 Limitations of Multithreading Model
02 Why Akka Actors?
04 Communicating with Actors
05 Changing Actor Behaviour
06 Creating Child Actors
05 Actor Life Cycle
07
7
Creating Actors
03
6. Limitations of Multithreading Model
● OOP encapsulation is only valid in a single threaded model.
● Delegating something to a thread is a very difficult task.
● Difficult to trace and deal with errors in a multithreading environment.
8. Why Akka Actors?
● Actors inspire Object-oriented paradigm.
● Just like traditional objects, actors also store their state as data.
● Actors are objects we can’t access directly, but only send messages to.
● Actors interact via sending and receiving messages.
● Messages are asynchronous by nature.
10. Creating and Instantiating Actors
● Actor system is heavy weight data structure.
● Actor system controls the number of threads and allocates them to running actors.
● Actor is a data structure that needs a thread to run.
● Actors are uniquely identified.
● Definition of actor has some internal data and behaviour(receive handler).
● Each actor has unique behaviour of processing the messages.
● Every actor must override receive() method.
● Messages are passed asynchronously among actors.
11. Creating and Instantiating Actors
● Actors are instantiated by calling actorOf() method in the actor system.
● Actor reference is a data structure provided by Akka to prohibit direct access of
actor instance created by Akka.
● Actors can communicate via actor references only.
● Props object is data structure that is used to create actor reference.
13. Communicating with Actors
● Akka sends messages to actors asynchronously.
● Actors send messages to each other via actor references.
● tell(!) method is invoked to send message to an actor.
● Messages can be of any type.
● We can send any message under two conditions:
○ Messages must be immutable.
○ Messages must be serializable.
14. Communicating with Actors
● Each actor has a member called context. It holds the information about the actor
environment.
● context.self is used by actor to send messages to itself.
● context.sender() returns ActorRef to send a message back to sender.
● When a message is sent to an actor from main application, the sender() is
Actor.noSender
● DeadLetters is an inbuilt actor of Akka that receives messages which are not sent to
anyone. It is the garbage pool of messages.
15. Communicating with Actors
● forward method is invoked to send a message to an actorRef by keeping the
original sender.
● Return type of receive() is Receive. It is an alias of a partial function from Any to Unit.
● Each actor has message handler and mail box in which messages are enqueued.
● Akka provides following message delivery guarantees:
○ AtmostOnce Message Delivery
○ AtleastOnce Message Delivery
○ ExactlyOnce Message Delivery
17. Changing Actor Behavior
● Akka provides the ability to change Actor behavior defined in message handlers.
● We should create stateless actors, because state of an actor can be complex and it
can cause mutability.
● To change actor behavior, implement separate message and handlers and swap
between them based on requirement.
● We use context.become() and context.unbecome() to get rid of actor's mutable state,
by changing actor behavior as responses to messages.
19. Creating Child Actors
● Actors can create other other actors by using actor's context, i.e. by invoking
context.actorOf().
● Child actor's name is appended to parent actor's path.
● Akka provides the ability of creating actor hierarchies by using feature of child
creation.
● Every Akka actor system has 3 guardian actors:
○ System guardian
○ User guardian
○ Root guardian
20. Creating Child Actors
● Akka provides the feature of Actor selection. It is a wrapper over a potential
ActorRef which is used to send a message.
● We can locate an actor using actor selection by providing path of that actor.
● Actor selection is created by using system.actorSelection(path) and passing a path as
a string.
23. Stopping Actors
Two ways to stop an actor:
i. Calling stop method - system.stop() or context.stop()
It is a non blocking process. It means that it doesn’t stop immediately. The
actor may process some messages even after sending the stop signal.
ii. Sending special messages - PosionPill or Kill
PoisonPill simply invokes the stopping procedure. Kill is more brutal than
PoisonPill as it makes an actor throw ActorKilledException.
24. Death Watch
● It is a mechanism to be notified when an actor dies.
● Method we use for death watch:
i. context.watch()
It registers the actor for death watch. When an actor dies, the watcher
actor who has registered for the death watch receives a special
Terminated message.
ii. context.unwatch()
It unregisters the actor for death watch.
26. ● Parents must decide upon their child's failure with a supervision strategy.
● When an actor fails, it suspends all of its children and sends a special
message to its parent.
● Parent can decide to:
○ resume the actor
○ restart the actor
○ stop the actor
○ escalate and fail itself
Supervision Strategy
27. ● Supervision strategies:
○ OneForOneStrategy
It is applied to the exact actor which caused the failure
○ AllForOneStrategy
It is applied to all the actors regardless of one that actually caused the
failure
Supervision Strategy
29. Schedulers
● We can schedule messages to run at a defined point in the future using:
○ scheduleOnce(initial delay) to schedule a message once
○ schedule(initial delay, interval) to schedule messages repeatedly
● We can also define cancellable which means that we can schedule the
message to cancel at some time using schedule.cancel()
30. Routers
● Routers are useful when you want to delegate or spread work in between
multiple actors of the same kind.
● Routers are usually middle level actors that forward messages to other actors
which are called routees, either created by the routers themselves or from
the outside.
31. Routers
● Some routing logics:
○ Round-robin - which cycles between routees
○ Random - which picks routers randomly
○ Smallest mailbox - which sends next message to the actor with fewest
messages in the queue.
○ Broadcast - which send messages to all the routers
○ Scatter-gather-first - which do a broadcast and waits for first reply and
rest of the replies are discarded.
32. ● Mailboxes are data structures in the actor reference that store messages.
● Types of mailboxes:
○ Custom priority mailbox - messages are stored in mailbox on the basis of
their priority.
○ Control-aware mailbox - some messages need to be processed first
regardless of what has been queued in the mailbox. We make those
messages as control messages.
Mailboxes