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 the "Hello, world" variety.
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:
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.
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 our simple "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
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.
Good start
Nice explanation. It would be nice to understand why a default constructor takes the string name in the first place (even though I looked it in other site, it would be more complete to explain every detail for a newbie like me) Keep up!
Post new comment