2012-01-17 17:04:20 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
|
*/
|
|
|
|
|
|
package akka
|
|
|
|
|
|
|
2012-01-19 15:13:10 +01:00
|
|
|
|
import akka.actor._
|
|
|
|
|
|
import akka.dispatch.{ Future, Promise }
|
|
|
|
|
|
import akka.util.{ Timeout, Duration }
|
|
|
|
|
|
|
2012-01-18 13:26:11 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* == Commonly Used Patterns With Akka ==
|
|
|
|
|
|
*
|
|
|
|
|
|
* This package is used as a collection point for usage patterns which involve
|
|
|
|
|
|
* actors, futures, etc. but are loosely enough coupled to (multiple of) them
|
|
|
|
|
|
* to present them separately from the core implementation. Currently supported
|
|
|
|
|
|
* are:
|
|
|
|
|
|
*
|
|
|
|
|
|
* <ul>
|
|
|
|
|
|
* <li><b>ask:</b> create a temporary one-off actor for receiving a reply to a
|
|
|
|
|
|
* message and complete a [[akka.dispatch.Future]] with it; returns said
|
|
|
|
|
|
* Future.</li>
|
|
|
|
|
|
* <li><b>pipeTo:</b> feed eventually computed value of a future to an actor as
|
|
|
|
|
|
* a message.</li>
|
|
|
|
|
|
* </ul>
|
|
|
|
|
|
*
|
|
|
|
|
|
* In Scala the recommended usage is to import the pattern from the package
|
|
|
|
|
|
* object:
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import akka.pattern.ask
|
|
|
|
|
|
*
|
|
|
|
|
|
* ask(actor, message) // use it directly
|
|
|
|
|
|
* actor ask message // use it by implicit conversion
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* For Java the patterns are available as static methods of the [[akka.pattern.Patterns]]
|
|
|
|
|
|
* class:
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import static akka.pattern.Patterns.ask;
|
|
|
|
|
|
*
|
|
|
|
|
|
* ask(actor, message);
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*/
|
2012-01-18 10:18:51 +01:00
|
|
|
|
package object pattern {
|
2012-01-17 17:04:20 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Import this implicit conversion to gain `?` and `ask` methods on
|
|
|
|
|
|
* [[akka.actor.ActorRef]], which will defer to the
|
|
|
|
|
|
* `ask(actorRef, message)(timeout)` method defined here.
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
2012-01-18 10:18:51 +01:00
|
|
|
|
* import akka.pattern.ask
|
2012-01-17 17:04:20 +01:00
|
|
|
|
*
|
|
|
|
|
|
* val future = actor ? message // => ask(actor, message)
|
|
|
|
|
|
* val future = actor ask message // => ask(actor, message)
|
|
|
|
|
|
* val future = actor.ask(message)(timeout) // => ask(actor, message)(timeout)
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* All of the above use an implicit [[akka.actor.Timeout]].
|
|
|
|
|
|
*/
|
2012-01-18 11:52:35 +01:00
|
|
|
|
implicit def ask(actorRef: ActorRef): AskSupport.AskableActorRef = new AskSupport.AskableActorRef(actorRef)
|
2012-01-17 17:04:20 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 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>Warning:</b>
|
|
|
|
|
|
* When using future callbacks, inside actors you need to carefully avoid closing over
|
|
|
|
|
|
* the containing actor’s 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>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* val f = ask(worker, request)(timeout)
|
|
|
|
|
|
* flow {
|
|
|
|
|
|
* EnrichedRequest(request, f())
|
|
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* [see [[akka.dispatch.Future]] for a description of `flow`]
|
|
|
|
|
|
*/
|
2012-01-23 15:59:18 +01:00
|
|
|
|
def ask(actorRef: ActorRef, message: Any)(implicit timeout: Timeout): Future[Any] = actorRef match {
|
2012-01-19 15:13:10 +01:00
|
|
|
|
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 ⇒
|
2012-01-18 11:52:35 +01:00
|
|
|
|
val provider = ref.provider
|
2012-01-23 15:59:18 +01:00
|
|
|
|
if (timeout.duration.length <= 0) {
|
|
|
|
|
|
actorRef.tell(message)
|
|
|
|
|
|
Promise.failed(new AskTimeoutException("not asking with negative timeout"))(provider.dispatcher)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
val a = AskSupport.createAsker(provider, timeout)
|
|
|
|
|
|
actorRef.tell(message, a)
|
|
|
|
|
|
a.result
|
2012-01-17 17:04:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
case _ ⇒ throw new IllegalArgumentException("incompatible ActorRef " + actorRef)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-18 13:01:24 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Import this implicit conversion to gain the `pipeTo` method on [[akka.dispatch.Future]]:
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* import akka.pattern.pipeTo
|
|
|
|
|
|
*
|
|
|
|
|
|
* Future { doExpensiveCalc() } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*/
|
|
|
|
|
|
implicit def pipeTo[T](future: Future[T]): PipeToSupport.PipeableFuture[T] = new PipeToSupport.PipeableFuture(future)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Register an onComplete callback on this [[akka.dispatch.Future]] to send
|
|
|
|
|
|
* the result to the given actor reference. Returns the original Future to
|
|
|
|
|
|
* allow method chaining.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <b>Recommended usage example:</b>
|
|
|
|
|
|
*
|
|
|
|
|
|
* {{{
|
|
|
|
|
|
* val f = ask(worker, request)(timeout)
|
|
|
|
|
|
* flow {
|
|
|
|
|
|
* EnrichedRequest(request, f())
|
|
|
|
|
|
* } pipeTo nextActor
|
|
|
|
|
|
* }}}
|
|
|
|
|
|
*
|
|
|
|
|
|
* [see [[akka.dispatch.Future]] for a description of `flow`]
|
|
|
|
|
|
*/
|
|
|
|
|
|
def pipeTo[T](future: Future[T], actorRef: ActorRef): Future[T] = {
|
|
|
|
|
|
future onComplete {
|
|
|
|
|
|
case Right(r) ⇒ actorRef ! r
|
2012-01-19 15:13:10 +01:00
|
|
|
|
case Left(f) ⇒ actorRef ! Status.Failure(f)
|
2012-01-18 13:01:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
future
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-01 20:48:03 +01:00
|
|
|
|
/**
|
2012-01-03 11:41:49 +01:00
|
|
|
|
* Returns a [[akka.dispatch.Future]] that will be completed with success (value `true`) when
|
2012-01-01 20:48:03 +01:00
|
|
|
|
* existing messages of the target actor has been processed and the actor has been
|
|
|
|
|
|
* terminated.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Useful when you need to wait for termination or compose ordered termination of several actors.
|
|
|
|
|
|
*
|
|
|
|
|
|
* If the target actor isn't terminated within the timeout the [[akka.dispatch.Future]]
|
2012-01-03 11:41:49 +01:00
|
|
|
|
* is completed with failure [[akka.actor.ActorTimeoutException]].
|
2012-01-01 20:48:03 +01:00
|
|
|
|
*/
|
|
|
|
|
|
def gracefulStop(target: ActorRef, timeout: Duration)(implicit system: ActorSystem): Future[Boolean] = {
|
|
|
|
|
|
if (target.isTerminated) {
|
2012-01-01 21:27:52 +01:00
|
|
|
|
Promise.successful(true)
|
2012-01-01 20:48:03 +01:00
|
|
|
|
} else {
|
2012-01-01 21:27:52 +01:00
|
|
|
|
val result = Promise[Boolean]()
|
2012-01-01 20:48:03 +01:00
|
|
|
|
system.actorOf(Props(new Actor {
|
|
|
|
|
|
// Terminated will be received when target has been stopped
|
|
|
|
|
|
context watch target
|
|
|
|
|
|
target ! PoisonPill
|
|
|
|
|
|
// ReceiveTimeout will be received if nothing else is received within the timeout
|
|
|
|
|
|
context setReceiveTimeout timeout
|
|
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2012-01-01 21:27:52 +01:00
|
|
|
|
case Terminated(a) if a == target ⇒
|
|
|
|
|
|
result success true
|
2012-01-01 21:30:04 +01:00
|
|
|
|
context stop self
|
2012-01-01 20:48:03 +01:00
|
|
|
|
case ReceiveTimeout ⇒
|
2012-01-01 21:27:52 +01:00
|
|
|
|
result failure new ActorTimeoutException(
|
|
|
|
|
|
"Failed to stop [%s] within [%s]".format(target.path, context.receiveTimeout))
|
2012-01-01 21:30:04 +01:00
|
|
|
|
context stop self
|
2012-01-01 20:48:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
}))
|
|
|
|
|
|
result
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-01-17 17:04:20 +01:00
|
|
|
|
}
|