-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |