Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Akka

In this you will learn about Akka – What is Akka, some design principles behind it, and why was it developed. So let’s begin.

  • Be the first to comment

  • Be the first to like this

Introduction to Akka

  1. 1. Presented By: Purva Agrawal Introduction To AKKA
  2. 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. Keep Your Screens on Mute Keep your laptop screens on mute. Avoid Disturbance Avoid any other kind of disturbance during the session.
  3. 3. Our Agenda 01 Introduction-What is Akka 02 Akka Actors And Creating Actors 03 Actor System and Life Cycle of an Actor 04 Testing and Fault Tolerance 05 Demo
  4. 4. Introduction to AKKA Akka is a toolkit and runtime for building highly concurrent, distributed, and fault-tolerant message-driven applications on the JVM (Java Virtual Machine). Akka is written in Scala. It implements the Actor Based Model. AKKA is reactive,i.e,it follows the reactive architecture. What is Akka? Why named Akka? ● Name of a beautiful Swedish mountain up in the northern part of Sweden called Laponia. ● Name of a goddess in the Sami mythology. She is the goddess that stands for all the beauty and good in the world.
  5. 5. Reactive Architecture And its Principles
  6. 6. AKKA ACTORS ● Actors were invented in 1973 by Carl Hewitt. Akka is an implementation of the Actor Model. ● An actor is an entity which communicates to other actor by message passing. Actor has it's own state and behavior. As in object-oriented programming everything is an object same like everything is an actor in actor-based system. In other words, we can say that an actor is an object that encapsulates state and behavior. Creating Actors We can create actor by extending Actor trait and implementing it's receive method. This method is called each time a message is received by the actor. The receive method does pattern matching on the received message and decide what to do.
  7. 7. Actor System The ActorSystem is a root actor in actors structure. An ActorSystem is a hierarchical group of actors which share common configuration, e.g. dispatchers, deployments, remote capabilities and addresses. It is also the entry point for creating or looking up actors. ActorSystem provides an actorOf() method which is used to create actor instance.
  8. 8. Actor Life Cycle So, here is a glimpse of how an Akka Actor Lifecycle works. The actor lifecycle begins as soon as the actor is created. The stages of an Akka Actor Lifecycle are as follows – ● preStart() ● Start ● Stop ● postStop() ● terminated
  9. 9. Anatomy of an Actor Each Actor is represented by an ActorRef. So, we never get access to an Actor instance directly. The Actor reference lets us send messages to the Actor. Each Actor has a mailbox and a dispatcher. So, the ActorRef enqueues incoming messages in the mailbox. After that, the dispatcher schedules message processing. Only one message at a time is passed to an Actor.
  10. 10. Creating Child Actors Akka provides facility to create child actor. You can create child actor by using implicit context reference. ActorSystem is used to create root-level or top-level actor. Akka provides you context so that you can create child actor also. var childActor = context.actorOf(Props[Child],"Child"); Actor Communication In Akka, actors communicate with each other by sending and receiving messages. The messages can be sent in many patterns like ● Fire and Forget ● Request and Response Akka provides two predefined methods tell() and ask() for message exchange.
  11. 11. Replying to Messages You can reply to a message by using sender() method. This method gives you ActorRef which is a instance of sender Actor. You can also store this reference. Replying to a message is simple, you just need to use sender() ! ReplyMsg. If there is no sender i.e. a message which is sent without an actor or future context, the default sender is a 'dead-letter' actor reference. What is Dead Letter ? ● Messages which cannot be delivered will be delivered to an actor called deadLetters. ● Implement this by importing package.
  12. 12. Scheduling Messages in Akka ● Often, a need exists to schedule the sending of a message to an actor: ○ a single time after some delay (send a message for once after a period of time and then we never send that message) ○ periodically with a given interval (e.g after every 5sec, 5min, etc.) ● Two Akka facilities exist to achieve this: ○ The Akka Scheduler service ○ The akka.actors.Timers trait Testing Actors In Akka generally we have two kinds of tests, synchronous and asynchronous. ● ‘Unit tests‘ are synchronous, you directly test the receive method without requiring an actor system. ● ‘Integration tests‘ are asynchronous, and generally test more than one actor working together. Akka comes with a dedicated module akka-testkit for supporting tests. To use Actor Testkit in our project we need to add the following dependency, val AkkaVersion = "2.6.10" libraryDependencies += "com.typesafe.akka" %% "akka-testkit" % AkkaVersion % Test
  13. 13. Akka Actor LifeCycle with Failure
  14. 14. Supervising Actors in Akka In case of failure, rather than forcing it back on the caller(customer), we prefer to handle it internally. Within Akka, it is done using a technique called Supervision. The parent decides from the below directives, depending on the nature of failure : ● Resume: Simply resume message processing. ● Restart: Transparently replace affected actor(s) with new instance(s) and then resume message processing. ● Stop: Stop affected Actor(s) permanently. ● Escalate: Escalate the failure by failing itself and propagate failure to its parent. Supervision Strategy There are two type of supervision strategies to supervise any actor: 1. One-For-One – Only the faulty child is affected when it fails. 2. One-For-All – All children are affected when one child fails.
  15. 15. DEMO
  16. 16. Thank You !