Example actor system

To illustrate features of Akka Typed, we'll reimplement an example we built in Chapter 11, An Introduction to the Akka and Actor Models, but this time with typed actors.

For those readers who are familiar with the content of the previous chapter, this approach will allow you to compare two different styles. For those who just joined, let's quickly recap the structure of this example.

We're building a small cookie bakery which is populated by a number of actors, each with their own set of responsibilities:

  • The Manager drives the process and passes over materials from one worker to another.
  • The Boy takes a ShoppingList and returns to the Manager the respective Groceries from the Store.
  • The Chef takes the Groceries and makes them into Dough. It does so by using a number of Mixers with the exact mixer count depending on the amount of stuff to mix.
  • The Cook takes Dough and makes RawCookies.
  • The Baker bakes the RawCookies in batches using a single Oven of a limited capacity.

The structure of the actor system we're going to build is represented in the following diagram:

Let's start with an implementation of the simplest actor in our system  the Oven. Here and later on in this chapter, we'll refer to the previous implementation, meaning the implementation we came up with in regards to untyped actors in Chapter 11, An Introduction to the Akka and Actor Models. The differences are very illustrative, so we advise the reader to refer to the code in the previous chapter, even if you haven't read it because you are already familiar with untyped Akka.

To be able to use Akka Typed in our code, we need to put the following dependency in build.sbt:

lazy val akkaVersion = "2.5.13"
libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % akkaVersion

Defining akkaVersion separately as a val has the advantage that it can be reused for other modules and changed in a single place at the moment a new version becomes available.

To keep our examples clean and short, we'll assume that the following inputs are present in every code snippet:

import akka.actor.typed._
import akka.actor.typed.scaladsl._

The first input brings lower level actor system abstractions into scope, and the second allows us to use higher level DSL for an actor's behavior definition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.37.250