Skip to content

Commit

Permalink
Merge branch 'master' into step1
Browse files Browse the repository at this point in the history
  • Loading branch information
vheaffinitech committed Mar 27, 2016
2 parents 99fd725 + 839085d commit 03fe21b
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions step1/main.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import scala.concurrent.Await
import scala.concurrent.Future
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.duration._

case object StopMsg;
case class Msg(text : String)

class HelloActor extends Actor {
def receive = {
case "hello" => println(" hello back at you")
case _ => println(" huh?")
case Msg(t) => {
if (t == "hello") sender ! s" $t back to you"
else sender ! s" does '$t' mean hello ?"
}
case StopMsg => context.stop(self)
case _ => sender ! " huh?"
}
}

object Main extends App {
val system = ActorSystem("HelloSystem")
// default Actor constructor
val helloActor = system.actorOf(Props[HelloActor], name = "helloactor")
helloActor ! "hello"
helloActor ! "bonjour"
helloActor ! "gutentag"

implicit val timeout = Timeout(5 seconds)

val future = helloActor ? Msg("hello")
val result = Await.result(future, timeout.duration).asInstanceOf[String]
println(result)

//another way
val future2: Future[String] = ask(helloActor, Msg("bonjour")).mapTo[String]
val result2 = Await.result(future2, 1 second)
println(result2)

helloActor ! StopMsg

system.shutdown()
}

0 comments on commit 03fe21b

Please sign in to comment.