UP | HOME
../../ | 2020-04-14

Akka Actors with Scala

Table of Contents

1 Overview of Lectures

  1. What do these lecture notes do?
  2. Describe Scala minimally. Just enough to do Akka. Pretty much ignore its (i) functional programming features, (ii) syntactic conveniences and (iii) type inference [not wholly]. Goto What is Scala?
  3. Describe Carl Hewitt's Actors Model, comparable in depth to our treatment of SMP, AMP, Shared-Vars, Semaphores. Goto Actors Model.
  4. Describe Akka, an implementation of Actors. Just enough so you can re-do all the classics such as Readers/Writers, Dining Philosophers. Goto What is Akka?
  5. Suggested order of study.
    1. Go through all of this page without visiting the links.
    2. Study the source code of Akka examples described in ./akkaPractical.html lecture notes. Equally importantly build and run these.
    3. Study the Required Reading References listed.
    4. Re-visit this page.

2 What is Scala?

  1. Scala can be thought of as a significantly enhanced Java.
  2. Combines OOP and Functional Programming.
  3. Uses Type Inference heavily.
  4. Compiles to JVM .class files.
  5. Scala is described fully at http://www.scala-lang.org/. Its compilers and other tools are open source.
  6. Scala Practical My lecture notes. Required Reading.

3 Actors Model

  1. A model of distributed computation.
  2. Uses AMP only.
  3. No semaphores. No SMP.
  4. Can we build these in Actors?
  5. On Actors My lecture notes. Required Reading.

4 What is Akka?

  1. Akka is an open source implementation of Hewitt's (pure) Actor Model, as a library in Scala. It makes practical concessions to the pure actor model.
  2. "Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM" http://www.akka.io. [Note the use of concurrent and distributed.]
  3. Akka helps with scaling both UP (utilizing multi-core processors) and OUT (utilizing the cloud/ grid/ cluster).
  4. For fault-tolerance, Akka adopts the "Let it crash" model, which has been used with great success in the telecom industry to build applications that self-heal and never stop.
  5. Reactive Applications = Concurrency + Events

4.2 Who Is Using Akka?

fig-actor-users.png

Figure 1: Who Is Using Akka?

4.3 What is Akka? v.nonFrivolous

  1. Includes an implementation of Actor Model, with improvements for practicality.
  2. Also includes Software Transactional Memory (STM).
  3. Event driven, middleware framework
  4. Part of Typesafe Stack. Open source, Apache License.
  5. Created by Jonas Bonér.

4.4 Large Scale Examples of Akka

  1. http://www.playframework.com/ "The High Velocity Web Framework For Java and Scala" Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications. RESTful by default.
    1. REST stands for "Representational State Transfer". It describes a web architecture.
  2. Play https://developer.lightbend.com/start/?group=play
  3. http://spray.io/ spray is an open-source toolkit for building REST/HTTP-based integration layers on top of Scala and Akka. It is asynchronous, and actor-based.

5 Akka Actor Architecture

fig-actor-hierarchy.png

Figure 2: Actor Hierarchy and Name Resolution

  1. An actor in Akka always belongs to a parent. Who is the parent of the first actor you create? "Root Guardian", similar to parent of / in Linux.

6 Akka Ops: Create, Send, Become, Supervise

  1. Akka has Create, Send, Become, Supervise operations on its actors. These are essential to use Akka.
  2. ./akkaOps.html My Lectures Notes on Akka Operations.

7 Routers, Dispatchers, and Schedulers

  1. A router is a type of actor. Routes incoming messages to outbound actors.
  2. A dispatcher chooses an actor and a message from the actors mbox, and allocates a thread.
  3. You can schedule sending of messages and execution of tasks (functions or Runnable).
  4. ./akkaRDS.html My Lecture Notes on Routers, Dispatchers, and Schedulers

8 Futures and Promises

  1. ./akkaFutures.html My lecture notes on Akka futures and promises. Below is a crude and quick summary.
  2. Akka gets Futures and Promises from Scala.
  3. Example of a Future: Below, s.getFriends is a normal method. Had we not enclosed it inside the future braces, control would not return until this method completes. The value f is a future object that encapsulates the computation that is inside the getFriends method. The computation is not yet scheduled, but will be.

    import scala.concurrent.{ future, promise }
    import scala.concurrent.ExecutionContext.Implicits.global
    val s = socialNetwork.createSessionFor("user", credentials)
    val f: Future[List[Friend]] = future {
      s.getFriends()           // may take a while
    }
    
  4. Invoking the future-braces starts an asynchronous computation and "immediately" returns a "future object". The results of this f become available once the async computation completes.
  5. Example of a Promise: We re-do the well-known producer-consumer.

    import scala.concurrent.{ future, promise }
    import scala.concurrent.ExecutionContext.Implicits.global
    val p = promise[T]                 // T is unimportant for now
    val f = p . future                 // extra spaces for clarity
    val prdcr = future {               // producer
      val r = produceSomething()
      p . success . r
      continueDoingSomethingUnrelated()
    }
    val cnsmr = future {              // consumer
      startDoingSomething()
      f . onSuccess {
        case r => doSomethingWithResult()
      }}
    
  6. A Promise p is a value that completes a future f. Methods: success, failure, complete

9 Distributed Actor System

  1. A Distributed Actor System is a collection of Akka actors running on multiple machines that are networked.
  2. ./akkaDistributed.html My lecture notes.

10 Termination

  1. What Does "Finished" Mean? The most natural answer to this question appears to be, "When all the Mailboxes are empty." Natural, yes; correct, no. :)
  2. ./akkaTerm.html My notes on Distributed termination as applied to an Akka Actor System.

11 References

  1. https://doc.akka.io/docs/akka/current/ Nearly all the code snippets and figures are from here. Reference.
  2. Jonas Boner, "Above the Clouds: Introducing Akka", 2011. Web search. Video or pdf. Highly recommended.
  3. https://doc.akka.io/docs/akka/current/typed/guide/ Getting Started Guide. Must visit. The following are linked there.
    1. Introduction to Akka. Required Reading
    2. Part 1: Actor Architecture. Required Reading
    3. Part 2: Creating the First Actor. Required Reading
    4. Part 3: Working with Device [in the small] Actors Optional Reading
    5. Part 4: Working with Device Groups Optional Reading
    6. Part 5: Querying Device Groups Optional Reading
  4. http://allaboutscala.com/scala-frameworks/akka/ 2020 Recommended Reading.

12 End


Copyright © 2020 www.wright.edu/~pmateti • 2020-04-14