Running the application

You can run the Hello World application from the command line or an IDE. The final topic in this guide describes how to run it from IntelliJ IDEA. But, before we run the application again, let’s take a quick look at the build tool: sbt.

The build files

sbt uses a build.sbt file to handle the project. This project’s build.sbt file looks like this:

name := "akka-quickstart-scala"

version := "1.0"

scalaVersion := "2.12.2"

lazy val akkaVersion = "2.5.3"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-testkit" % akkaVersion,
  "org.scalatest" %% "scalatest" % "3.0.1" % "test"

This build file is very simple. In essence, it creates one project hello-akka-scala and declares project dependencies. We must also declare what version of sbt to use and this is done in the file project/


Running the project

Just as you did earlier, run the application from a console:

  1. Enter ./sbt on OSX/Linux or sbt.bat on Windows

sbt downloads project dependencies. The > prompt indicates sbt has started in interactive mode.

  1. At the sbt prompt, enter reStart.

The output should look something like this (scroll all the way to the right to see the Actor output):

[info] Compiling 1 Scala source and 1 Java source to /Users/x/akka-quickstart-scala/target/scala-2.12/classes...
[info] Running com.lightbend.akka.sample.AkkaQuickstart
[INFO] [05/09/2017 09:57:15.979] [] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/howdyGreeter#-1854995773]): Howdy, Akka
[INFO] [05/09/2017 09:57:15.980] [] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/helloGreeter#-1072877049]): Hello, Scala
[INFO] [05/09/2017 09:57:15.980] [] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/goodDayGreeter#1972065097]): Good day, Play
[INFO] [05/09/2017 09:57:15.980] [] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/howdyGreeter#-1854995773]): Howdy, Lightbend

Remember that the test implementation set the Printer Actor to use Akka logger? This provides a lot of extra information. For example, the log output contains includes the time and name of the Actor. Let’s focus on the output from the Printer Actor for a while:

... Howdy, Akka
... Hello, Scala
... Good day, Play
... Howdy, Lightbend

This is the result of our code that sends messages to the Greeter Actor:

howdyGreeter ! WhoToGreet("Akka")
howdyGreeter ! Greet

howdyGreeter ! WhoToGreet("Lightbend")
howdyGreeter ! Greet

helloGreeter ! WhoToGreet("Scala")
helloGreeter ! Greet

goodDayGreeter ! WhoToGreet("Play")
goodDayGreeter ! Greet

To run the tests, enter test at the sbt prompt.

Try running the code a couple of more times and make sure to notice the order of the logging. Did you notice that it can change from one run to another. What’s happening here? The asynchronous behavior becomes evident. This might be a new mental model for you. But, once you gain experience with it everything will become clear; just like for Neo in the Matrix.

Next steps

If you use IntelliJ, try integrating the sample project with IntelliJ IDEA.

To continue learning more about Akka and Actor Systems, look at the Getting Started Guide next. Happy hakking!