ask-2.0
This commit is contained in:
parent
774584642e
commit
a44da38e2b
13 changed files with 122 additions and 120 deletions
|
|
@ -8,6 +8,7 @@ import akka.actor.Actor
|
|||
import akka.actor.Props
|
||||
import akka.event.Logging
|
||||
import akka.dispatch.Future
|
||||
import akka.dispatch.Futures
|
||||
|
||||
//#imports1
|
||||
|
||||
|
|
@ -229,10 +230,10 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|||
|
||||
val myActor = system.actorOf(Props(new MyActor), name = "myactor")
|
||||
implicit val timeout = system.settings.ActorTimeout
|
||||
val future = myActor ? "hello"
|
||||
val future = Futures.ask(myActor, "hello")
|
||||
for (x ← future) println(x) //Prints "hello"
|
||||
|
||||
val result: Future[Int] = for (x ← (myActor ? 3).mapTo[Int]) yield { 2 * x }
|
||||
val result: Future[Int] = for (x ← Futures.ask(myActor, 3).mapTo[Int]) yield { 2 * x }
|
||||
//#using-ask
|
||||
|
||||
system.stop(myActor)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import akka.actor.Actor
|
|||
import akka.actor.Props
|
||||
import akka.actor.Status.Failure
|
||||
import akka.dispatch.Future
|
||||
import akka.dispatch.Futures
|
||||
import akka.dispatch.Await
|
||||
import akka.util.duration._
|
||||
import akka.dispatch.Promise
|
||||
|
|
@ -46,7 +47,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
|
||||
implicit val timeout = system.settings.ActorTimeout
|
||||
val future = actor ? msg
|
||||
val future = Futures.ask(actor, msg)
|
||||
val result = Await.result(future, timeout.duration).asInstanceOf[String]
|
||||
//#ask-blocking
|
||||
result must be("HELLO")
|
||||
|
|
@ -59,7 +60,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
//#map-to
|
||||
import akka.dispatch.Future
|
||||
|
||||
val future: Future[String] = (actor ? msg).mapTo[String]
|
||||
val future: Future[String] = Futures.ask(actor, msg).mapTo[String]
|
||||
//#map-to
|
||||
Await.result(future, timeout.duration) must be("HELLO")
|
||||
}
|
||||
|
|
@ -149,13 +150,13 @@ class FutureDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
//#composing-wrong
|
||||
|
||||
val f1 = actor1 ? msg1
|
||||
val f2 = actor2 ? msg2
|
||||
val f1 = Futures.ask(actor1, msg1)
|
||||
val f2 = Futures.ask(actor2, msg2)
|
||||
|
||||
val a = Await.result(f1, 1 second).asInstanceOf[Int]
|
||||
val b = Await.result(f2, 1 second).asInstanceOf[Int]
|
||||
|
||||
val f3 = actor3 ? (a + b)
|
||||
val f3 = Futures.ask(actor3, (a + b))
|
||||
|
||||
val result = Await.result(f3, 1 second).asInstanceOf[Int]
|
||||
//#composing-wrong
|
||||
|
|
@ -172,13 +173,13 @@ class FutureDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
//#composing
|
||||
|
||||
val f1 = actor1 ? msg1
|
||||
val f2 = actor2 ? msg2
|
||||
val f1 = Futures.ask(actor1, msg1)
|
||||
val f2 = Futures.ask(actor2, msg2)
|
||||
|
||||
val f3 = for {
|
||||
a ← f1.mapTo[Int]
|
||||
b ← f2.mapTo[Int]
|
||||
c ← (actor3 ? (a + b)).mapTo[Int]
|
||||
c ← Futures.ask(actor3, (a + b)).mapTo[Int]
|
||||
} yield c
|
||||
|
||||
val result = Await.result(f3, 1 second).asInstanceOf[Int]
|
||||
|
|
@ -191,7 +192,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val oddActor = system.actorOf(Props[OddActor])
|
||||
//#sequence-ask
|
||||
// oddActor returns odd numbers sequentially from 1 as a List[Future[Int]]
|
||||
val listOfFutures = List.fill(100)((oddActor ? GetNext).mapTo[Int])
|
||||
val listOfFutures = List.fill(100)(Futures.ask(oddActor, GetNext).mapTo[Int])
|
||||
|
||||
// now we have a Future[List[Int]]
|
||||
val futureList = Future.sequence(listOfFutures)
|
||||
|
|
@ -239,7 +240,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val actor = system.actorOf(Props[MyActor])
|
||||
val msg1 = -1
|
||||
//#recover
|
||||
val future = actor ? msg1 recover {
|
||||
val future = Futures.ask(actor, msg1) recover {
|
||||
case e: ArithmeticException ⇒ 0
|
||||
}
|
||||
//#recover
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import akka.routing.{ ScatterGatherFirstCompletedRouter, BroadcastRouter, Random
|
|||
import annotation.tailrec
|
||||
import akka.actor.{ Props, Actor }
|
||||
import akka.util.duration._
|
||||
import akka.dispatch.Await
|
||||
import akka.dispatch.{ Futures, Await }
|
||||
|
||||
case class FibonacciNumber(nbr: Int)
|
||||
|
||||
|
|
@ -71,7 +71,7 @@ class ParentActor extends Actor {
|
|||
Props[FibonacciActor].withRouter(ScatterGatherFirstCompletedRouter(within = 2 seconds)),
|
||||
"router")
|
||||
implicit val timeout = context.system.settings.ActorTimeout
|
||||
val futureResult = scatterGatherFirstCompletedRouter ? FibonacciNumber(10)
|
||||
val futureResult = Futures.ask(scatterGatherFirstCompletedRouter, FibonacciNumber(10))
|
||||
val result = Await.result(futureResult, timeout.duration)
|
||||
//#scatterGatherFirstCompletedRouter
|
||||
println("The result of calculating Fibonacci for 10 is %d".format(result))
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ package akka.docs.testkit
|
|||
import akka.testkit.TestProbe
|
||||
import akka.util.duration._
|
||||
import akka.actor._
|
||||
import akka.dispatch.Futures
|
||||
|
||||
//#imports-test-probe
|
||||
|
||||
|
|
@ -204,7 +205,7 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|||
import akka.util.duration._
|
||||
//#test-probe-reply
|
||||
val probe = TestProbe()
|
||||
val future = probe.ref ? "hello"
|
||||
val future = Futures.ask(probe.ref, "hello")
|
||||
probe.expectMsg(0 millis, "hello") // TestActor runs on CallingThreadDispatcher
|
||||
probe.sender ! "world"
|
||||
assert(future.isCompleted && future.value == Some(Right("world")))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue