Merge branch 'wip-1581-patterns-ask'
This commit is contained in:
commit
2a0c4ca145
126 changed files with 980 additions and 415 deletions
|
|
@ -212,31 +212,12 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|||
system.stop(myActor)
|
||||
}
|
||||
|
||||
"using ask" in {
|
||||
//#using-ask
|
||||
class MyActor extends Actor {
|
||||
def receive = {
|
||||
case x: String ⇒ sender ! x.toUpperCase
|
||||
case n: Int ⇒ sender ! (n + 1)
|
||||
}
|
||||
}
|
||||
|
||||
val myActor = system.actorOf(Props(new MyActor), name = "myactor")
|
||||
implicit val timeout = system.settings.ActorTimeout
|
||||
val future = myActor ? "hello"
|
||||
for (x ← future) println(x) //Prints "hello"
|
||||
|
||||
val result: Future[Int] = for (x ← (myActor ? 3).mapTo[Int]) yield { 2 * x }
|
||||
//#using-ask
|
||||
|
||||
system.stop(myActor)
|
||||
}
|
||||
|
||||
"using implicit timeout" in {
|
||||
val myActor = system.actorOf(Props(new FirstActor))
|
||||
//#using-implicit-timeout
|
||||
import akka.util.duration._
|
||||
import akka.util.Timeout
|
||||
import akka.pattern.ask
|
||||
implicit val timeout = Timeout(500 millis)
|
||||
val future = myActor ? "hello"
|
||||
//#using-implicit-timeout
|
||||
|
|
@ -248,7 +229,8 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|||
val myActor = system.actorOf(Props(new FirstActor))
|
||||
//#using-explicit-timeout
|
||||
import akka.util.duration._
|
||||
val future = myActor ? ("hello", timeout = 500 millis)
|
||||
import akka.pattern.ask
|
||||
val future = myActor.ask("hello")(500 millis)
|
||||
//#using-explicit-timeout
|
||||
Await.result(future, 500 millis) must be("hello")
|
||||
}
|
||||
|
|
@ -327,6 +309,28 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|||
case e: ActorTimeoutException ⇒ // the actor wasn't stopped within 5 seconds
|
||||
}
|
||||
//#gracefulStop
|
||||
|
||||
}
|
||||
|
||||
"using pattern ask / pipeTo" in {
|
||||
val actorA, actorB, actorC, actorD = system.actorOf(Props.empty)
|
||||
//#ask-pipeTo
|
||||
import akka.pattern.{ ask, pipeTo }
|
||||
|
||||
case class Result(x: Int, s: String, d: Double)
|
||||
case object Request
|
||||
|
||||
implicit val timeout = Timeout(5 seconds) // needed for `?` below
|
||||
|
||||
val f: Future[Result] =
|
||||
for {
|
||||
x ← ask(actorA, Request).mapTo[Int] // call pattern directly
|
||||
s ← actorB ask Request mapTo manifest[String] // call by implicit conversion
|
||||
d ← actorC ? Request mapTo manifest[Double] // call by symbolic name
|
||||
} yield Result(x, s, d)
|
||||
|
||||
f pipeTo actorD // .. or ..
|
||||
pipeTo(f, actorD)
|
||||
//#ask-pipeTo
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,9 +44,10 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val msg = "hello"
|
||||
//#ask-blocking
|
||||
import akka.dispatch.Await
|
||||
import akka.pattern.ask
|
||||
|
||||
implicit val timeout = system.settings.ActorTimeout
|
||||
val future = actor ? msg
|
||||
val future = actor ? msg // enabled by the “ask” import
|
||||
val result = Await.result(future, timeout.duration).asInstanceOf[String]
|
||||
//#ask-blocking
|
||||
result must be("HELLO")
|
||||
|
|
@ -58,8 +59,9 @@ class FutureDocSpec extends AkkaSpec {
|
|||
implicit val timeout = system.settings.ActorTimeout
|
||||
//#map-to
|
||||
import akka.dispatch.Future
|
||||
import akka.pattern.ask
|
||||
|
||||
val future: Future[String] = (actor ? msg).mapTo[String]
|
||||
val future: Future[String] = ask(actor, msg).mapTo[String]
|
||||
//#map-to
|
||||
Await.result(future, timeout.duration) must be("HELLO")
|
||||
}
|
||||
|
|
@ -147,15 +149,16 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val msg2 = 2
|
||||
implicit val timeout = system.settings.ActorTimeout
|
||||
import akka.dispatch.Await
|
||||
import akka.pattern.ask
|
||||
//#composing-wrong
|
||||
|
||||
val f1 = actor1 ? msg1
|
||||
val f2 = actor2 ? msg2
|
||||
val f1 = ask(actor1, msg1)
|
||||
val f2 = 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 = ask(actor3, (a + b))
|
||||
|
||||
val result = Await.result(f3, 1 second).asInstanceOf[Int]
|
||||
//#composing-wrong
|
||||
|
|
@ -170,15 +173,16 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val msg2 = 2
|
||||
implicit val timeout = system.settings.ActorTimeout
|
||||
import akka.dispatch.Await
|
||||
import akka.pattern.ask
|
||||
//#composing
|
||||
|
||||
val f1 = actor1 ? msg1
|
||||
val f2 = actor2 ? msg2
|
||||
val f1 = ask(actor1, msg1)
|
||||
val f2 = ask(actor2, msg2)
|
||||
|
||||
val f3 = for {
|
||||
a ← f1.mapTo[Int]
|
||||
b ← f2.mapTo[Int]
|
||||
c ← (actor3 ? (a + b)).mapTo[Int]
|
||||
c ← ask(actor3, (a + b)).mapTo[Int]
|
||||
} yield c
|
||||
|
||||
val result = Await.result(f3, 1 second).asInstanceOf[Int]
|
||||
|
|
@ -191,7 +195,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)(akka.pattern.ask(oddActor, GetNext).mapTo[Int])
|
||||
|
||||
// now we have a Future[List[Int]]
|
||||
val futureList = Future.sequence(listOfFutures)
|
||||
|
|
@ -239,7 +243,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val actor = system.actorOf(Props[MyActor])
|
||||
val msg1 = -1
|
||||
//#recover
|
||||
val future = actor ? msg1 recover {
|
||||
val future = akka.pattern.ask(actor, msg1) recover {
|
||||
case e: ArithmeticException ⇒ 0
|
||||
}
|
||||
//#recover
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import annotation.tailrec
|
|||
import akka.actor.{ Props, Actor }
|
||||
import akka.util.duration._
|
||||
import akka.dispatch.Await
|
||||
import akka.pattern.ask
|
||||
import akka.routing.SmallestMailboxRouter
|
||||
|
||||
case class FibonacciNumber(nbr: Int)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
@ -119,6 +120,7 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|||
import akka.testkit.TestActorRef
|
||||
import akka.util.duration._
|
||||
import akka.dispatch.Await
|
||||
import akka.pattern.ask
|
||||
|
||||
val actorRef = TestActorRef(new MyActor)
|
||||
// hypothetical message stimulating a '42' answer
|
||||
|
|
@ -202,6 +204,7 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|||
"demonstrate probe reply" in {
|
||||
import akka.testkit.TestProbe
|
||||
import akka.util.duration._
|
||||
import akka.pattern.ask
|
||||
//#test-probe-reply
|
||||
val probe = TestProbe()
|
||||
val future = probe.ref ? "hello"
|
||||
|
|
|
|||
|
|
@ -141,6 +141,7 @@ class TransactorDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
import akka.util.duration._
|
||||
import akka.util.Timeout
|
||||
import akka.pattern.ask
|
||||
|
||||
val system = ActorSystem("app")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue