Akka is an open-source framework that uses the Actor Model to make it easy to create concurrent and distributed applications in Java or Scala. This Akka tutorial covers all the important topics of Akka such as, why the actor model matches the needs of modern distributed systems, actor lifecycle, and more
In this Akka tutorial, you will learn why Akka is widely used in developing applications. How Akka is used to embed the features like multithreading, and concurrency into an application.How an actor is created in the actor model. The life cycle of an actor helps us to know how the actor is stopped and restarted. Concepts like creating an actor, and configuring an actor enable you to get basic programming knowledge of the Akka framework. You can learn about the methods used for sending and receiving messages among actors. You will acquire knowledge on implementing multi-threading concepts through the message-passing mechanism. Strategies implemented by the Akka to make applications Fault-tolerant and Location Transparent.
Akka Tutorial - Table of Content |
Akka is a framework or a toolkit used for building applications on JVM(Java Virtual Machine). The applications built by the Akka framework are concurrent, fault-tolerant, and distributed. Akka adopts an actor model to handle the concurrency. In the actor model, everything is an actor likewise in object-oriented design everything is an object. In the Scala actor model, actors share information and tasks by using a message passing Mechanism.
If you want to enrich your career and become a professional in Akka, then visit Mindmajix - a global online training platform: "Akka Online Training". This course will help you to achieve excellence in this domain. |
Akka framework is widely used to build applications because the applications built by Akka implement concurrency and multi-threading. Applications that are deployed in the real world should contain features like concurrency or multi-threading because they will serve multiple clients at a time. Akka creates a layer between the actor and baseline system so that the actor’s only job is to process the messages. All the difficulty of generating threads, scheduling threads, Managing race conditions, and synchronization, sending and receiving messages, is assigned to the framework to manage lucidly.
In 1973 Carl Eddie Hewitt implemented the Actor Model as a conceptual model to manage concurrent computation while the software sector is looking for an alternative to implement the features like concurrency and multi-threading in an application, considering it as the best option.
In the actor model, the actor is represented as an individual entity. Some characteristics of the actor are as follows:
Actors in Akka are represented using the hierarchy system. They share a common configuration and ActorSystem defines it. We can define a sample ActorSystem by calling the Create() method:
ActorSystem system= ActorSystem.create(“test1-system”);
For handling the messages received from other actor extends an abstract class called “AbstractActor” and implements the CreateReceive() method.
The actor we created should be included in the Actor System.
ActorRef readingActorRef
= system.actorOf(Props.create(MyActor1.class), "my-actor1");
Myactor1 is the actor we created.
Props class is used for Actor Configuration. Props are immutable class, so it is shared during the creation of new actors. Actor Configuration can be explained more by defining an actor named Actor1, and it will process some text.
public class MyActor1 extends AbstractActor {
public Receive createReceive() {
return receiveBuilder1().build();
}
}
Akka actor has life cycle methods. Following are the life cycle methods of Akka actor.
1) preStart()
To provide a particular implementation for an actor, the prestart() method is called immediately after creating the actor. This method is overridable. Syntax for prestart() method is as follows:
def prestart() {
println(“prestart method is defined”);
}
2) postStop()
This method is used to release the resources of an actor after it is stopped. Messages sent to a stopped actor are forwarded to dead letters of the Actor system. This method is asynchronous and overridable.
Syntax:
def postStop(){
println(“postStop method is defined”);
}
3) preRestart()
When an exception is thrown, the actor may be restarted. After the actor is restarted, preRestart() method is called with the exception, which made the actor restart.
Syntax
def preRestart(reason:Throwable,){
println(“prerestart method is invoked”);
}
4) postRestart()
This method is invoked immediately after restarting the newly created actor. It is used to reinitialize the actor which is crashed due to an exception.
def postRestart(){
println(“postRestart method is invoked”);
}
Sending Messages using the tell() method
For sending messages, Akka actor does not wait or block a thread. It sends messages asynchronously. Messages can be sent by using a (!) exclamation marks. Using the tell() method we can send messages as follows:
actor.tell(“ !hai”,null);
In the above statement “hai” is the message.
Message sending using ask method
Ask method is used to send messages and returns future which indicates a reply to the message. After a specific time-out period, the future is completed and TimeOut() Exception is thrown. Always use the tell() method for performance and ask() method for the reply. Messages can be sent using the ask() method as follows:
Val future = actor("Hello");
An actor can give reply to a message by using the sender() method. This method provides you an instance called ActorRef and we can store it. If we want to give a reply to a message. You can do it by the sender()! ReplyMsg. Akka actor can reply to messages as follows:
class ActorChildReplyExample extends Actor{
def receive ={
case message:String => println("Message received from "+sender.path.name+" message: "+message);
println("Replying to "+sender().path.name);
sender()! "I got your message";
}
}
Messages are forwarded from one actor to another. Although the message is forwarded through a third party, we can maintain the address and reference of an actor.
Supervisory strategies of Akka are the fundamental and direct strategies to define the fault tolerance of your system. The message indicating a failure reaches the supervisor, the following actions are taken:
1. Resuming children and keeping its internal state:
This strategy is applied when the error does not damage the state of the child, and it can continue working correctly.
2. Restarting the child and clearing its internal state
This strategy is applied when the case is just opposite to the above one i.e. when the error damages the state of the child it is restarted.
3. Terminating the child permanently.
This strategy can be used in scenarios when the error condition is not fixable, and the rest of the operations are performed in the absence of this child.
4. Stop itself and accelerate the error.
This strategy is applicable when the supervisor doesn’t know how to handle the fault, and it accelerates it to the own supervisor.
Another feature that is supported by Akka is Location transparency. This feature enables the actors to know the origin of the messages they receive. The sender of the message may exist in the same JVM or another JVM. Akka architecture facilitates to manage of all these scenarios individually in such a way that it is fully transparent to the actor,
Actor systems are made to run in a distributed environment without needing any dedicated code. The configuration file is sufficient for Akka to tell to which node the messages should be sent.
Akka Stream API is built on the Akka framework. Akka stream API enables us to form data transition flow from stand-alone steps. Essential concepts of Akka Stream are as follows:
Akka builds the applications on JVM(Java Virtual Machine) which satisfies the requirements like concurrency and working on a distributed environment. It is written in Scala and conceals the complexity of the developer. This Akka tutorial is more than sufficient to gain basic knowledge of the Akka framework.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Akka Training | Dec 24 to Jan 08 | View Details |
Akka Training | Dec 28 to Jan 12 | View Details |
Akka Training | Dec 31 to Jan 15 | View Details |
Akka Training | Jan 04 to Jan 19 | View Details |
As a Senior Writer for Mindmajix, Saikumar has a great understanding of today’s data-driven environment, which includes key aspects such as Business Intelligence and data management. He manages the task of creating great content in the areas of Programming, Microsoft Power BI, Tableau, Oracle BI, Cognos, and Alteryx. Connect with him on LinkedIn and Twitter.