akka.patterns.ask everywhere
This commit is contained in:
parent
a342bb93ea
commit
ce1d2f4721
53 changed files with 78 additions and 27 deletions
|
|
@ -8,7 +8,7 @@ import akka.actor.Actor
|
|||
import akka.actor.Props
|
||||
import akka.event.Logging
|
||||
import akka.dispatch.Future
|
||||
import akka.dispatch.Futures
|
||||
import akka.Patterns
|
||||
|
||||
//#imports1
|
||||
|
||||
|
|
@ -230,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 = Futures.ask(myActor, "hello")
|
||||
val future = Patterns.ask(myActor, "hello")
|
||||
for (x ← future) println(x) //Prints "hello"
|
||||
|
||||
val result: Future[Int] = for (x ← Futures.ask(myActor, 3).mapTo[Int]) yield { 2 * x }
|
||||
val result: Future[Int] = for (x ← Patterns.ask(myActor, 3).mapTo[Int]) yield { 2 * x }
|
||||
//#using-ask
|
||||
|
||||
system.stop(myActor)
|
||||
|
|
@ -244,6 +244,7 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|||
//#using-implicit-timeout
|
||||
import akka.util.duration._
|
||||
import akka.util.Timeout
|
||||
import akka.patterns.ask
|
||||
implicit val timeout = Timeout(500 millis)
|
||||
val future = myActor ? "hello"
|
||||
//#using-implicit-timeout
|
||||
|
|
@ -255,6 +256,7 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|||
val myActor = system.actorOf(Props(new FirstActor))
|
||||
//#using-explicit-timeout
|
||||
import akka.util.duration._
|
||||
import akka.patterns.ask
|
||||
val future = myActor ? ("hello", timeout = 500 millis)
|
||||
//#using-explicit-timeout
|
||||
Await.result(future, 500 millis) must be("hello")
|
||||
|
|
|
|||
|
|
@ -10,10 +10,10 @@ 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
|
||||
import akka.Patterns
|
||||
|
||||
object FutureDocSpec {
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
|
||||
implicit val timeout = system.settings.ActorTimeout
|
||||
val future = Futures.ask(actor, msg)
|
||||
val future = Patterns.ask(actor, msg)
|
||||
val result = Await.result(future, timeout.duration).asInstanceOf[String]
|
||||
//#ask-blocking
|
||||
result must be("HELLO")
|
||||
|
|
@ -60,7 +60,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
//#map-to
|
||||
import akka.dispatch.Future
|
||||
|
||||
val future: Future[String] = Futures.ask(actor, msg).mapTo[String]
|
||||
val future: Future[String] = Patterns.ask(actor, msg).mapTo[String]
|
||||
//#map-to
|
||||
Await.result(future, timeout.duration) must be("HELLO")
|
||||
}
|
||||
|
|
@ -150,13 +150,13 @@ class FutureDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
//#composing-wrong
|
||||
|
||||
val f1 = Futures.ask(actor1, msg1)
|
||||
val f2 = Futures.ask(actor2, msg2)
|
||||
val f1 = Patterns.ask(actor1, msg1)
|
||||
val f2 = Patterns.ask(actor2, msg2)
|
||||
|
||||
val a = Await.result(f1, 1 second).asInstanceOf[Int]
|
||||
val b = Await.result(f2, 1 second).asInstanceOf[Int]
|
||||
|
||||
val f3 = Futures.ask(actor3, (a + b))
|
||||
val f3 = Patterns.ask(actor3, (a + b))
|
||||
|
||||
val result = Await.result(f3, 1 second).asInstanceOf[Int]
|
||||
//#composing-wrong
|
||||
|
|
@ -173,13 +173,13 @@ class FutureDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
//#composing
|
||||
|
||||
val f1 = Futures.ask(actor1, msg1)
|
||||
val f2 = Futures.ask(actor2, msg2)
|
||||
val f1 = Patterns.ask(actor1, msg1)
|
||||
val f2 = Patterns.ask(actor2, msg2)
|
||||
|
||||
val f3 = for {
|
||||
a ← f1.mapTo[Int]
|
||||
b ← f2.mapTo[Int]
|
||||
c ← Futures.ask(actor3, (a + b)).mapTo[Int]
|
||||
c ← Patterns.ask(actor3, (a + b)).mapTo[Int]
|
||||
} yield c
|
||||
|
||||
val result = Await.result(f3, 1 second).asInstanceOf[Int]
|
||||
|
|
@ -192,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)(Futures.ask(oddActor, GetNext).mapTo[Int])
|
||||
val listOfFutures = List.fill(100)(Patterns.ask(oddActor, GetNext).mapTo[Int])
|
||||
|
||||
// now we have a Future[List[Int]]
|
||||
val futureList = Future.sequence(listOfFutures)
|
||||
|
|
@ -240,7 +240,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val actor = system.actorOf(Props[MyActor])
|
||||
val msg1 = -1
|
||||
//#recover
|
||||
val future = Futures.ask(actor, msg1) recover {
|
||||
val future = Patterns.ask(actor, msg1) recover {
|
||||
case e: ArithmeticException ⇒ 0
|
||||
}
|
||||
//#recover
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ import akka.routing.{ ScatterGatherFirstCompletedRouter, BroadcastRouter, Random
|
|||
import annotation.tailrec
|
||||
import akka.actor.{ Props, Actor }
|
||||
import akka.util.duration._
|
||||
import akka.dispatch.{ Futures, Await }
|
||||
import akka.dispatch.Await
|
||||
import akka.patterns.ask
|
||||
|
||||
case class FibonacciNumber(nbr: Int)
|
||||
|
||||
|
|
@ -71,7 +72,7 @@ class ParentActor extends Actor {
|
|||
Props[FibonacciActor].withRouter(ScatterGatherFirstCompletedRouter(within = 2 seconds)),
|
||||
"router")
|
||||
implicit val timeout = context.system.settings.ActorTimeout
|
||||
val futureResult = Futures.ask(scatterGatherFirstCompletedRouter, FibonacciNumber(10))
|
||||
val futureResult = scatterGatherFirstCompletedRouter ? FibonacciNumber(10)
|
||||
val result = Await.result(futureResult, timeout.duration)
|
||||
//#scatterGatherFirstCompletedRouter
|
||||
println("The result of calculating Fibonacci for 10 is %d".format(result))
|
||||
|
|
|
|||
|
|
@ -120,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.patterns.ask
|
||||
|
||||
val actorRef = TestActorRef(new MyActor)
|
||||
// hypothetical message stimulating a '42' answer
|
||||
|
|
@ -203,9 +204,10 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|||
"demonstrate probe reply" in {
|
||||
import akka.testkit.TestProbe
|
||||
import akka.util.duration._
|
||||
import akka.patterns.ask
|
||||
//#test-probe-reply
|
||||
val probe = TestProbe()
|
||||
val future = Futures.ask(probe.ref, "hello")
|
||||
val future = probe.ref ? "hello"
|
||||
probe.expectMsg(0 millis, "hello") // TestActor runs on CallingThreadDispatcher
|
||||
probe.sender ! "world"
|
||||
assert(future.isCompleted && future.value == Some(Right("world")))
|
||||
|
|
|
|||
|
|
@ -141,6 +141,7 @@ class TransactorDocSpec extends AkkaSpec {
|
|||
import akka.dispatch.Await
|
||||
import akka.util.duration._
|
||||
import akka.util.Timeout
|
||||
import akka.patterns.ask
|
||||
|
||||
val system = ActorSystem("app")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue