Scala Actors FAQ: Can you share a Scala Akka Actors example/tutorial?
Sure. Most of the Scala Akka Actor tutorials I see jump right into the deep end, throwing you into some tough concepts right away. Personally I'm more into the “crawl before you walk approach,” and to that end, here are some simple Akka Actor examples, of “Hello, world” variety.
A simple “Hello, world” example
My first Akka Actor example is about as simple as I can make it:
import akka.actor.Actor import akka.actor.ActorSystem import akka.actor.Props class HelloActor extends Actor { def receive = { case "hello" => println("hello back at you") case _ => println("huh?") } } object Main extends App { val system = ActorSystem("HelloSystem") // default Actor constructor val helloActor = system.actorOf(Props[HelloActor], name = "helloactor") helloActor ! "hello" helloActor ! "buenos dias" }
Here’s a quick description of this example:
- Import all the Akka actor classes you need.
- Define an Actor, defining behavior in the special “receive” method.
- Create a “main” object where you can test things.
- You need an
ActorSystem
to get things started, so create one, with an arbitrary string. - You create an Actor instance with
actorOf
, and that line shows the syntax for an Actor whose default constructor takes no arguments. (More on this shortly.) - Now that we have an instance of an actor, we send it two messages.
Assuming (a) you're working on a Unix/Linux system and (b) you have Scala and SBT installed, you can run this test class as follows. First, create a directory named “Hello,” “HelloTest,” or something similar, and then cd
into that directory.
Next, create a build.sbt file like this in that directory:
name := "Hello Test #1" version := "1.0" scalaVersion := "2.10.0" resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" libraryDependencies += "com.typesafe.akka" % "akka-actor_2.10" % "2.2-M1"
Then paste the Scala source code above into a file named Hello.scala, also in that same directory. After you’ve done that, run this sbt
command:
$ sbt run
After sbt downloads all the dependencies it needs, you should see this output:
[info] Loading global plugins from /Users/Al/.sbt/plugins [info] Set current project to AkkaHelloWorld (in build file:/Users/Al/Projects/Scala/Tests/Scala2.10/AkkaHelloWorld/) [info] Running Main hello back at you huh? ^C
If you’re using Scala 2.10.0 and SBT 0.12 or newer, that should work.
The program will sit there and run forever, so as shown, press Ctrl-
C
when you’re ready to stop it and move on to the next example.
Creating an Akka Actor with a class constructor that takes arguments
As you saw in the example above, if you want to create an actor whose default constructor takes no arguments, you use this syntax:
// create an actor with the "actorOf(Props[TYPE])" syntax val helloActor = system.actorOf(Props[HelloActor], name = "helloactor")
However, if your actor’s default constructor takes an argument, such as a String
, you create your actor a little differently, using this syntax:
val helloActor = system.actorOf(Props(new HelloActor("Fred")), name = "helloactor")
This is still pretty simple, you just need to know this syntax.
If you want to test this, here’s the “Hello, world” example with a modified constructor to demonstrate this syntax:
import akka.actor._ // (1) changed the constructor here class HelloActor(myName: String) extends Actor { def receive = { // (2) changed these println statements case "hello" => println("hello from %s".format(myName)) case _ => println("'huh?', said %s".format(myName)) } } object Main extends App { val system = ActorSystem("HelloSystem") // (3) changed this line of code val helloActor = system.actorOf(Props(new HelloActor("Fred")), name = "helloactor") helloActor ! "hello" helloActor ! "buenos dias" }
If you follow the steps listed above, you can run this example, and you should see output like this:
$ sbt run [info] Loading global plugins from /Users/Al/.sbt/plugins [info] Set current project to AkkaHelloWorld (in build file:/Users/Al/Projects/Scala/Tests/Scala2.10/AkkaHelloWorld/) [info] Compiling 1 Scala source to /Users/Al/Projects/Scala/Tests/Scala2.10/AkkaHelloWorld/target/scala-2.10/classes... [info] Running Main hello from Fred 'huh?', said Fred ^C
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |
More Scala Akka Actor examples, coming soon
As I get comfortable with the overall Akka system I’ll share some more Akka Actor examples, but until then, reporting live from sunny (and windy) Boulder, Colorado, this is Alvin Alexander.