Here in this blog i am going to explain what is an Akka Actor and how we actually work with Akka Actors.
What is Akka
Akka is a toolkit for building highly concurrent, distributed and resilient message- driven application for Scala and java. Akka support several programming models for concurrency. it is very useful for writing server – side scalable applications.
Using Akka it is very easy to send message to every different nodes of any application , The weight of the machine will determine how Akka handles the traffic.
What is an Akka Actor
Akka Actor is the type of entity that communicates by sending messages to other actors. The actor’s state and behaviour are distinct , as everything is an object in object-oriented programming , just as everything is an actor in actor – based system.
Hence an actor is an object that encapsulate both state and behaviour.
If we have to transmitted from one actor to another using one of the syntactic protocol listed below.
- (“tell”) ! : Sends the message and return immediately
- (“ask”) ? : Sends the message asynchronously and it returns a Future which represents a possible reply
How to create an Actor
We can create actor in Scala by extending the Actor trait and implementing its get method. This method is invoked whenever the actor receives a message. The receive method analyses the message and decides what to do based on pattern matching.
All the Incoming messages for the actor are delivered to a mailbox. There are several mailbox implementations to choose from, with FIFO being the default implementation.
Hence we have to add the following Dependencies to build.sbt file , for successfully work on Akka Actor
libraryDependencies ++= Seq( "com.typesafe.akka" %% "akka-actor-typed" % "2.6.18", "com.typesafe.akka" %% "akka-actor-testkit-typed" % "2.6.18" % Test )
In this above example we have created an Actor “FirstActor” and extended the actor trait, and override the receive method. For all received messages, you must provide matching cases. If there is an unknown message, you must provide a default case, as shown in the preceding example.
Akka Actor System
In the actor’s structure, the actor system is a root actor. Hierarchy of actors that share a common configuration is such as Dispatcher, Deployment, Remote Capabilities, and Addresses, is referred to as an actor system.
It is also a starting point for creating or watching actors. It is a class that extends the ActorRefFactory attribute.
The ActorSystem provides an ActorOf() method for creating an Actor instance:
Components of an Akka Actor System
1.) Dead Letter Office
If we are sending the message to an Actor, but before receiving that message the actor terminate then that will be send as a dead letter.
Messages sent over non trustable networks will be lost and will not be forwarded to the Dead Letter Office.
The main purpose of this feature is for debugging, especially if an actor’s message does not arrive consistently. You can do so by importing the akka.actor.DeadLetter package.
2.) User Guardian Actor
It is a native actor made by the user using the actor system. This parent is used to achieve a sequential shutdown sequence in which logging continues while all normal actors are terminated. It keeps track of all user-generated actors.
3.) System Guardian Actor
This actor functions in the same way as the user parent actor, but only for system actors. When the system receives the completed message, it monitors the parent user parent and initiates its shutdown.
A scheduler is a type of attribute that extends AnyRef. The scheduler is in charge of scheduled tasks. It enables the scheduling of messages. You can plan the delivery of messages and the completion of tasks. This creates a new instance for each actor system to schedule tasks to occur at a predetermined time. It returns a Cancellable context, which allows you to cancel the scheduled operation by calling the cancel method on this context object.
5.) Event System
The event system, also known as the eventStream, is each ActorSystem’s main event bus. We saved carry log messages and dead letters this way. It can also be used to broadcast messages to the entire actor system. Calling the actorSystemRef.eventStream() method returns the EventStream context.
ActorSystem includes a configuration component for configuring applications. You can get to it through your actor system.
In this blog we have covered basics of Akka or Akka Actors . we learn how to create actors and how to established communication between actors. For more blogs Click here