From e8a955cdef979b19bc7d6fbc87cb66568109f60a Mon Sep 17 00:00:00 2001 From: Song Kun Date: Fri, 4 May 2018 21:17:17 +0800 Subject: [PATCH] Define ! and ? in EntityRef. ticket #24922 --- .../typed/scaladsl/ClusterSharding.scala | 68 +++++++++++-------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala b/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala index 89143e982e..bb0d7252ae 100644 --- a/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala +++ b/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala @@ -253,9 +253,45 @@ object EntityTypeKey { /** * Send a message to the entity referenced by this EntityRef using *at-most-once* * messaging semantics. + * + * Example usage: + * {{{ + * val target: EntityRef[String] = ... + * target.tell("Hello") + * }}} */ def tell(msg: A): Unit + /** + * Send a message to the entity referenced by this EntityRef using *at-most-once* + * messaging semantics. + * + * Example usage: + * {{{ + * val target: EntityRef[String] = ... + * target ! "Hello" + * }}} + */ + def !(msg: A): Unit = this.tell(msg) + + /** + * Allows to "ask" the [[EntityRef]] for a reply. + * See [[akka.actor.typed.scaladsl.AskPattern]] for a complete write-up of this pattern + * + * Example usage: + * {{{ + * case class Request(msg: String, replyTo: ActorRef[Reply]) + * case class Reply(msg: String) + * + * implicit val timeout = Timeout(3.seconds) + * val target: EntityRef[Request] = ... + * val f: Future[Reply] = target.ask(Request("hello", _)) + * }}} + * + * Please note that an implicit [[akka.util.Timeout]] and [[akka.actor.Scheduler]] must be available to use this pattern. + */ + def ask[U](f: ActorRef[U] ⇒ A)(implicit timeout: Timeout, scheduler: Scheduler): Future[U] + /** * Allows to "ask" the [[EntityRef]] for a reply. * See [[akka.actor.typed.scaladsl.AskPattern]] for a complete write-up of this pattern @@ -272,39 +308,11 @@ object EntityTypeKey { * * Please note that an implicit [[akka.util.Timeout]] and [[akka.actor.Scheduler]] must be available to use this pattern. */ - def ask[U](f: ActorRef[U] ⇒ A)(implicit timeout: Timeout, scheduler: Scheduler): Future[U] + def ?[U](message: ActorRef[U] ⇒ A)(implicit timeout: Timeout, scheduler: Scheduler): Future[U] = + this.ask(message)(timeout, scheduler) } -object EntityRef { - implicit final class EntityRefOps[A](val ref: EntityRef[A]) extends AnyVal { - /** - * Send a message to the Actor referenced by this ActorRef using *at-most-once* - * messaging semantics. - */ - def !(msg: A): Unit = ref.tell(msg) - - /** - * Allows to "ask" the [[EntityRef]] for a reply. - * See [[akka.actor.typed.scaladsl.AskPattern]] for a complete write-up of this pattern - * - * Example usage: - * {{{ - * case class Request(msg: String, replyTo: ActorRef[Reply]) - * case class Reply(msg: String) - * - * implicit val timeout = Timeout(3.seconds) - * val target: EntityRef[Request] = ... - * val f: Future[Reply] = target ? (Request("hello", _)) - * }}} - * - * Please note that an implicit [[akka.util.Timeout]] and [[akka.actor.Scheduler]] must be available to use this pattern. - */ - def ?[U](message: ActorRef[U] ⇒ A)(implicit timeout: Timeout, scheduler: Scheduler): Future[U] = - ref.ask(message)(timeout, scheduler) - } -} - object ClusterShardingSetup { def apply[T <: Extension](createExtension: ActorSystem[_] ⇒ ClusterSharding): ClusterShardingSetup = new ClusterShardingSetup(new java.util.function.Function[ActorSystem[_], ClusterSharding] {