Consider a communication happening between a Employee and its HR Department.
Broadly these are explained in the following six steps when a message is passed to the actor:
Employee creates something called an ActorSystem
.
It uses the ActorSystem to create something called as ActorRef
. The message(MSG) is sent to the ActorRef
(a proxy to HR Actor).
Actor ref passes the message along to a Message Dispatcher
.
The Dispatcher enqueues the message in the target Actor’s MailBox
.
The Dispatcher then puts the Mailbox
on a Thread (more on that in the next section).
The MailBox
dequeues a message and eventually delegates that to the actual HR Actor’s receive
method.
/** The Main Program consider it as a Employee Actor that is sending the requests **/
object EmployeeActorApp extends App{
//Initialize the ActorSystem
val actorSystem=ActorSystem("HrMessageingSystem")
//construct the HR Actor Ref
val hrActorRef=actorSystem.actorOf(Props[HrActor])
//send a message to the HR Actor
hrActorRef!Message
//Let's wait for a couple of seconds before we shut down the system
Thread.sleep (2000)
//Shut down the ActorSystem.
actorSystem.shutdown()
}
/** The HRActor reads the message sent to it and performs action based on the message Type **/
class HRActor extends Actor {
def receive = {
case s: String if(s.equalsIgnoreCase(“SICK”)) => println("Sick Leave applied”)
case s: String if(s.equalsIgnoreCase(“PTO”)) => println("PTO applied “)
}
}