Created
November 13, 2013 21:43
-
-
Save ya-pulser/7456939 to your computer and use it in GitHub Desktop.
Execute long running actor message in "other" thread using future and receive report when long running task is done.
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
01:35:12:632 Humming ... | |
01:35:12:633 manager: flush sent | |
01:35:12:633 worker: got command | |
01:35:12:633 manager alive | |
01:35:12:633 manager alive | |
01:35:12:633 manager alive | |
01:35:12:660 worker: started | |
01:35:12:662 worker: alive | |
01:35:12:662 manager: resource allocated | |
01:35:12:662 worker: alive | |
01:35:12:662 worker: alive | |
01:35:13:661 worker: done | |
01:35:13:663 manager: work is done | |
01:35:17:633 Shutdown! |
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
import akka.actor.{Props, ActorSystem, ActorRef, Actor} | |
import com.typesafe.config.ConfigFactory | |
import java.text.SimpleDateFormat | |
import java.util.Date | |
import scala.concurrent._ | |
import ExecutionContext.Implicits.global | |
object Sample { | |
private val fmt = new SimpleDateFormat("HH:mm:ss:SSS") | |
def printWithTime(msg: String) = { | |
println(fmt.format(new Date()) + " " + msg) | |
} | |
class WorkerActor extends Actor { | |
protected def receive = { | |
case "now" => | |
val commander = sender | |
printWithTime("worker: got command") | |
future { | |
printWithTime("worker: started") | |
Thread.sleep(1000) | |
printWithTime("worker: done") | |
}(ExecutionContext.Implicits.global) onSuccess { | |
case _ => commander ! "done" | |
} | |
commander ! "working" | |
case "alive?" => | |
printWithTime("worker: alive") | |
} | |
} | |
class ManagerActor(worker: ActorRef) extends Actor { | |
protected def receive = { | |
case "do" => | |
worker ! "now" | |
printWithTime("manager: flush sent") | |
case "working" => | |
printWithTime("manager: resource allocated") | |
case "done" => | |
printWithTime("manager: work is done") | |
case "alive?" => | |
printWithTime("manager alive") | |
worker ! "alive?" | |
} | |
} | |
def main(args: Array[String]) { | |
val config = ConfigFactory.parseString("" + | |
"akka.loglevel=DEBUG\n" + | |
"akka.debug.lifecycle=on\n" + | |
"akka.debug.receive=on\n" + | |
"akka.debug.event-stream=on\n" + | |
"akka.debug.unhandled=on\n" + | |
"" | |
) | |
val system = ActorSystem("mine", config) | |
val actor1 = system.actorOf(Props[WorkerActor], "worker") | |
val actor2 = system.actorOf(Props(new ManagerActor(actor1)), "manager") | |
actor2 ! "do" | |
actor2 ! "alive?" | |
actor2 ! "alive?" | |
actor2 ! "alive?" | |
printWithTime("Humming ...") | |
Thread.sleep(5000) | |
printWithTime("Shutdown!") | |
system.shutdown() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment