polish “ask” pattern, see #1581

- move package objects into their respective package.scala file in the
  right directories
- make implicit conversion as well as explicit facility available under
  the same name akka.patterns.ask for easy import
- revert the logic to produce the Promise for the PromiseActorRef within
  the ActorRefProvider; supporting wrapping of external Promises does
  not seem to justify doing needless extra allocations in case of
  failure
- add scaladocs
- factor out “def provider” into trait ActorRefWithProvider, as it
  didn’t feel right attaching this information “by exception” to
  MinimalActorRef
This commit is contained in:
Roland 2012-01-17 17:04:20 +01:00
parent ce1d2f4721
commit 9c762dec20
11 changed files with 268 additions and 176 deletions

View file

@ -13,7 +13,6 @@ import akka.dispatch.Future
import akka.dispatch.Await
import akka.util.duration._
import akka.dispatch.Promise
import akka.Patterns
object FutureDocSpec {
@ -45,9 +44,10 @@ class FutureDocSpec extends AkkaSpec {
val msg = "hello"
//#ask-blocking
import akka.dispatch.Await
import akka.patterns.ask
implicit val timeout = system.settings.ActorTimeout
val future = Patterns.ask(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")
@ -59,8 +59,9 @@ class FutureDocSpec extends AkkaSpec {
implicit val timeout = system.settings.ActorTimeout
//#map-to
import akka.dispatch.Future
import akka.patterns.ask
val future: Future[String] = Patterns.ask(actor, msg).mapTo[String]
val future: Future[String] = ask(actor, msg).mapTo[String]
//#map-to
Await.result(future, timeout.duration) must be("HELLO")
}
@ -148,15 +149,16 @@ class FutureDocSpec extends AkkaSpec {
val msg2 = 2
implicit val timeout = system.settings.ActorTimeout
import akka.dispatch.Await
import akka.patterns.ask
//#composing-wrong
val f1 = Patterns.ask(actor1, msg1)
val f2 = Patterns.ask(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 = Patterns.ask(actor3, (a + b))
val f3 = ask(actor3, (a + b))
val result = Await.result(f3, 1 second).asInstanceOf[Int]
//#composing-wrong
@ -171,15 +173,16 @@ class FutureDocSpec extends AkkaSpec {
val msg2 = 2
implicit val timeout = system.settings.ActorTimeout
import akka.dispatch.Await
import akka.patterns.ask
//#composing
val f1 = Patterns.ask(actor1, msg1)
val f2 = Patterns.ask(actor2, msg2)
val f1 = ask(actor1, msg1)
val f2 = ask(actor2, msg2)
val f3 = for {
a f1.mapTo[Int]
b f2.mapTo[Int]
c Patterns.ask(actor3, (a + b)).mapTo[Int]
c ask(actor3, (a + b)).mapTo[Int]
} yield c
val result = Await.result(f3, 1 second).asInstanceOf[Int]
@ -192,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)(Patterns.ask(oddActor, GetNext).mapTo[Int])
val listOfFutures = List.fill(100)(akka.patterns.ask(oddActor, GetNext).mapTo[Int])
// now we have a Future[List[Int]]
val futureList = Future.sequence(listOfFutures)
@ -240,7 +243,7 @@ class FutureDocSpec extends AkkaSpec {
val actor = system.actorOf(Props[MyActor])
val msg1 = -1
//#recover
val future = Patterns.ask(actor, msg1) recover {
val future = akka.patterns.ask(actor, msg1) recover {
case e: ArithmeticException 0
}
//#recover