The Ultimate Guide to Akka Interview Questions and Answers (2023)
In this post, questions from Akka Interviews will be answered for Experienced and Freshers. We're trying to share our experience and learn how to help you make progress in your career.
- What is Akka?
- What is Akka in Java?
- What is Akka Actor?
- What is Akka Actor System?
- How to terminate Akka ActorSystem?
- What is Akka Props?
- How to create Akka actor?
- What is Akka Props?
- What is difference between Java 9 Reactive streams versus Akka Streams?
Q: What is Akka?
Ans:
Akka is a free , open source toolkit for developing highly concurrent, distributed, and resilient messaging-driven Java and Scala applications. Akka supports various competitiveness programming models but prefers actor-based concurrency, inspired by Erlang.
Q: What is Akka in Java?
Ans:
Akka is an open source and runtime toolkit running on the JVM. It's written in Scala (a language mostly used for concurrency purposes) so you can use Java code (or Scala) to call all of its libraries and functions.
Q: What is Akka Actor?
Ans:
Actors are objects that encapsulate state and behavior and communicate mainly through the exchange of messages that are put in the recipient's mailbox. Actors are message driven, stateful building blocks.
Q: What is Akka Actor System?
Ans:
ActorSystem is a heavyweight model that allocates 1 ... N threads, which create one for each logical application.
Q: How to terminate Akka ActorSystem?
Ans:
If you know that all is done with your application, you can either stop the user guardian actor or call the ActorSystem terminate
method. CoordinatedShutdown will stop all the running
actors.
Q: What is Akka Props?
Ans:
Akka Props is a configuration class used to define options when creating an actor.
Q: How to create Akka actor?
Ans:
Akka actor are created by extending the Actor base trait and implementing the receive method.
import akka.actor.Actor; // Import Actor
import akka.actor.ActorSystem;
import akka.actor.Props;
class AkkaActorSample extends Actor{ // Extend Actor
def receive = { // Receive messages
case msg:String => println(msg)
case _ =>println("Default message")
}
}
object HelloWorldMain{
def main(args:Array[String]){
// Create ActorSystem
var actorSystem = ActorSystem("ActorSystem");
// Create Actor
var actor = actorSystem.actorOf(Props[AkkaActorSample],"AkkaActorSample")
// Send messages using !
actor ! "Hello World!"
}
}
-----------------------------------------
Output
Hello World!
Default message
Q: What is Akka Props?
Ans:
Props is a configuration class, which is used when creating an actor to define options.
Q: How to implement Akka Props?
Ans:
Import akka.actor.Props package to implement Akka Props.
import akka.actor.Actor; // Import actor trait
import akka.actor.ActorSystem; // Import ActorSystem
import akka.actor.Props;
class AkkaPropsSample extends Actor {
def receive= { // Receive messages
case msg:String => println(msg+" "+self.path.name)
}
}
object HelloWorldPropMain{
def main(args:Array[String]){
var actorSystem = ActorSystem("ActorSystem"); // Create ActorSystem
// Create Actor
var actorObj = actorSystem.actorOf(Props[AkkaPropsSample]," AkkaPropsSample");
actorObj ! "Welcome to" // Send messages using !
}
}
------------------------
Output
Welcome to AkkaPropsSample
Q: What is difference between Java 9 Reactive streams versus Akka Streams?
Ans:
Reactive streams are a feature in streaming with non-blocking backpressure for asynchronous data processing. They became part of the JDK in the java.util.concurrent package since Java 9.
Akka Streams is a library which uses bounded/limited buffer space to process and transfer a sequence of elements. This last property is the defining feature of Akka Streams that we refer to as boundedness.
Akka streams have been built over reactive streams such that the terminology are different but the workflow / feature is the same.