remove use of akka.actor.timeout property for ask

This commit is contained in:
Roland 2012-01-23 15:59:18 +01:00
parent 71d58c5338
commit 3a30f915c3
8 changed files with 25 additions and 56 deletions

View file

@ -37,7 +37,7 @@ class AskableActorRef(val actorRef: ActorRef) {
*
* [see the [[akka.dispatch.Future]] companion object for a description of `flow`]
*/
def ask(message: Any)(implicit timeout: Timeout = null): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
def ask(message: Any)(implicit timeout: Timeout): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
/**
* Sends a message asynchronously and returns a [[akka.dispatch.Future]]

View file

@ -4,6 +4,7 @@
package akka.pattern
import akka.testkit.AkkaSpec
import akka.util.duration._
class AskSpec extends AkkaSpec {
@ -11,7 +12,7 @@ class AskSpec extends AkkaSpec {
"return broken promises on DeadLetters" in {
val dead = system.actorFor("/system/deadLetters")
val f = dead ask 42
val f = dead.ask(42)(1 second)
f.isCompleted must be(true)
f.value.get match {
case Left(_: AskTimeoutException)

View file

@ -51,7 +51,7 @@ object AskSupport {
*
* [see the [[akka.dispatch.Future]] companion object for a description of `flow`]
*/
def ask(message: Any)(implicit timeout: Timeout = null): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
def ask(message: Any)(implicit timeout: Timeout): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
/**
* Sends a message asynchronously and returns a [[akka.dispatch.Future]]
@ -81,7 +81,7 @@ object AskSupport {
*
* [see the [[akka.dispatch.Future]] companion object for a description of `flow`]
*/
def ?(message: Any)(implicit timeout: Timeout = null): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
def ?(message: Any)(implicit timeout: Timeout): Future[Any] = akka.pattern.ask(actorRef, message)(timeout)
}
/**

View file

@ -9,39 +9,6 @@ object Patterns {
import akka.pattern.{ ask scalaAsk }
import akka.util.{ Timeout, Duration }
/**
* <i>Java API for `akka.pattern.ask`:</i>
* Sends a message asynchronously and returns a [[akka.dispatch.Future]]
* holding the eventual reply message; this means that the target actor
* needs to send the result to the `sender` reference provided. The Future
* will be completed with an [[akka.actor.AskTimeoutException]] after the
* given timeout has expired; this is independent from any timeout applied
* while awaiting a result for this future (i.e. in
* `Await.result(..., timeout)`).
*
* <b>This variant will use the `akka.actor.timeout` from the configuration.</b>
*
* <b>Warning:</b>
* When using future callbacks, inside actors you need to carefully avoid closing over
* the containing actors object, i.e. do not call methods or access mutable state
* on the enclosing actor from within the callback. This would break the actor
* encapsulation and may introduce synchronization bugs and race conditions because
* the callback will be scheduled concurrently to the enclosing actor. Unfortunately
* there is not yet a way to detect these illegal accesses at compile time.
*
* <b>Recommended usage:</b>
*
* {{{
* final Future<Object> f = Patterns.ask(worker, request, timeout);
* f.onSuccess(new Procedure<Object>() {
* public void apply(Object o) {
* nextActor.tell(new EnrichedResult(request, o));
* }
* });
* }}}
*/
def ask(actor: ActorRef, message: Any): Future[AnyRef] = scalaAsk(actor, message).asInstanceOf[Future[AnyRef]]
/**
* <i>Java API for `akka.pattern.ask`:</i>
* Sends a message asynchronously and returns a [[akka.dispatch.Future]]

View file

@ -87,18 +87,17 @@ package object pattern {
*
* [see [[akka.dispatch.Future]] for a description of `flow`]
*/
def ask(actorRef: ActorRef, message: Any)(implicit timeout: Timeout = null): Future[Any] = actorRef match {
def ask(actorRef: ActorRef, message: Any)(implicit timeout: Timeout): Future[Any] = actorRef match {
case ref: InternalActorRef if ref.isTerminated
actorRef.tell(message)
Promise.failed(new AskTimeoutException("sending to terminated ref breaks promises"))(ref.provider.dispatcher)
case ref: InternalActorRef
val provider = ref.provider
(if (timeout == null) provider.settings.ActorTimeout else timeout) match {
case t if t.duration.length <= 0
if (timeout.duration.length <= 0) {
actorRef.tell(message)
Promise.failed(new AskTimeoutException("not asking with negative timeout"))(provider.dispatcher)
case t
val a = AskSupport.createAsker(provider, t)
} else {
val a = AskSupport.createAsker(provider, timeout)
actorRef.tell(message, a)
a.result
}

View file

@ -231,8 +231,8 @@ public class UntypedActorDocTestBase {
final Timeout t = new Timeout(Duration.create(5, TimeUnit.SECONDS));
final ArrayList<Future<Object>> futures = new ArrayList<Future<Object>>();
futures.add(ask(actorA, "request")); // using `akka.actor.timeout` from config
futures.add(ask(actorB, "reqeest", t)); // using explicit timeout from above
futures.add(ask(actorA, "request", 1000)); // using 1000ms timeout
futures.add(ask(actorB, "reqeest", t)); // using timeout from above
final Future<Iterable<Object>> aggregate = Futures.sequence(futures, system.dispatcher());

View file

@ -207,6 +207,9 @@ reply to be received; it is independent of the timeout applied when awaiting
completion of the :class:`Future`, however, the actor will complete the
:class:`Future` with an :class:`AskTimeoutException` when it stops itself.
Since there is no good library default value for the ask-timeout, specification
of a timeout is required for all usages as shown below.
Also, since the ``ask`` feature is coupling futures and actors, it is no longer
offered on the :class:`ActorRef` itself, but instead as a use pattern to be
imported. While Scalas implicit conversions enable transparent replacement,
@ -221,15 +224,17 @@ v2.0 (Scala)::
import akka.pattern.ask
actorRef ? message
ask(actorRef, message) // will use `akka.actor.timeout` or implicit Timeout
ask(actorRef, message)(timeout)
implicit val timeout: Timeout = ...
actorRef ? message // uses implicit timeout
actorRef ask message // uses implicit timeout
actorRef.ask(message)(timeout) // uses explicit timeout
ask(actorRef, message) // uses implicit timeout
ask(actorRef, message)(timeout) // uses explicit timeout
v2.0 (Java)::
import akka.pattern.Patterns;
Patterns.ask(actorRef, message) // will use `akka.actor.timeout`
Patterns.ask(actorRef, message, timeout)
Documentation:
@ -358,7 +363,7 @@ v2.0::
import akka.event.Logging
val log = Logging(context.system, this) // will include system name in message source
val log = Logging(system.eventStream, this) // will not include system name
val log = Logging(system.eventStream, getClass.getName) // will not include system name
log.error(exception, message)
log.warning(message)
log.info(message)

View file

@ -389,9 +389,6 @@ taken from one of the following locations in order of precedence:
.. includecode:: code/akka/docs/actor/ActorDocSpec.scala#using-implicit-timeout
3. actor systems default value from ``akka.actor.timeout`` setting for
:meth:`ask` methods
See :ref:`futures-scala` for more information on how to await or query a
future.