O'Reilly logo

Building Applications with Scala by Diego Pacheco

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Coding actors with Akka

Let's take a look at the following Actor code using the Akka framework and Scala:

 import akka.actor._ case object HelloMessage class HelloWorldActor extends Actor { def receive = { case HelloMessage => sender() ! "Hello World" case a:Any => sender() ! "I don't know: " + a + " - Sorry!" } } object SimpleActorMainApp extends App{ val system = ActorSystem("SimpleActorSystem") val actor = system.actorOf(Props[HelloWorldActor]) import scala.concurrent.duration._ import akka.util.Timeout import akka.pattern.ask import scala.concurrent.Await implicit val timeout = Timeout(20 seconds) val future = actor ? HelloMessage val result = Await.result(future, timeout.duration).asInstanceOf[String] println("Actor says: " + result ) val ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required