Akka : der richtige Einsatz von `bitten` Muster?

Ich versuche zu verstehen Futures und bitten Muster in akka.

So, ich mache zwei Akteure, und man fragt, eine andere, ihn zurück zu senden a message. Gut, nach akka ist Futures Dokumentation, Schauspieler sollte Fragen(?) für die Meldung und es wird ihm eine Future instanse. Dann Schauspieler blockieren sollte (mit Await) Future Ergebnisse.

Gut, bekomme ich nie meine Zukunft gemacht. Warum ist das so?

Code:

package head_thrash

import akka.actor._
import akka.util.Timeout
import scala.concurrent.Await
import scala.concurrent.duration._

object Main extends App {

  val system = ActorSystem("actors")

  val actor1 = system.actorOf(Props[MyActor], "node_1")
  val actor2 = system.actorOf(Props[MyActor], "node_2")

  actor2 ! "ping_other"

  system.awaitTermination()

  Console.println("Bye!")
}

class MyActor extends Actor with ActorLogging {
  import akka.pattern.ask

  implicit val timeout = Timeout(100.days)

  def receive = {
    case "ping_other" => {
      val selection = context.actorSelection("../node_1")
      log.info("Sending ping to node_1")
      val result = Await.result(selection ? "ping", Duration.Inf) //<-- Blocks here forever!
      log.info("Got result " + result)
    }
    case "ping" => {
      log.info("Sending back pong!")
      sender ! "pong"
    }
  }
}

Wenn ich Duration.Inf zu 5.seconds, dann Schauspieler, wartet 5 Sekunden, berichtet, dass meine Zukunft ist Timeouted (durch das werfen TimeoutException), und dann die anderen Schauspieler schließlich antwortet mit der Nachricht benötigt. Also, keine async passiert. Warum? 🙁

Wie soll ich richtig realisieren, dass das Muster? Danke.

InformationsquelleAutor head_thrash | 2013-11-21

Schreibe einen Kommentar