From 68dc288b089196db0c5debc6abc593f561d324fa Mon Sep 17 00:00:00 2001 From: kerr Date: Fri, 30 Nov 2018 16:20:37 +0800 Subject: [PATCH] Change ask timeout from Timeout to Duration in typed javadsl. (#25975) * !typ Change the ActorContext#ask in javadsl to accept a Duration instead of Timeout. * !typ Change the ActorContext#setReceiveTimeout's parameter name from d to receiveTimeout. --- .../java/akka/actor/typed/javadsl/ActorContextAskTest.java | 3 ++- .../src/test/java/akka/actor/typed/javadsl/WatchTest.java | 3 +-- .../akka/actor/typed/receptionist/ReceptionistApiTest.java | 5 ++--- .../java/jdocs/akka/typed/InteractionPatternsTest.java | 5 ++--- .../test/java/jdocs/akka/typed/SpawnProtocolDocTest.java | 2 +- .../scala/akka/actor/typed/internal/ActorContextImpl.scala | 6 +++--- .../main/scala/akka/actor/typed/javadsl/ActorContext.scala | 4 ++-- .../actor/typed/javadsl/{Ask.scala => AskPattern.scala} | 7 ++++--- .../scala/akka/actor/typed/scaladsl/ActorContext.scala | 2 +- .../typed/javadsl/PersistentActorCompileOnlyTest.java | 5 ++--- 10 files changed, 20 insertions(+), 22 deletions(-) rename akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/{Ask.scala => AskPattern.scala} (84%) diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java index ab15d7eb9c..792de261f9 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/ActorContextAskTest.java @@ -14,6 +14,7 @@ import org.junit.ClassRule; import org.junit.Test; import org.scalatest.junit.JUnitSuite; +import java.time.Duration; import java.util.concurrent.TimeUnit; public class ActorContextAskTest extends JUnitSuite { @@ -42,7 +43,7 @@ public class ActorContextAskTest extends JUnitSuite { final Behavior snitch = Behaviors.setup((ActorContext context) -> { context.ask(Pong.class, pingPong, - new Timeout(3, TimeUnit.SECONDS), + Duration.ofSeconds(3), (ActorRef ref) -> new Ping(ref), (pong, exception) -> { if (pong != null) return pong; diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java index 89773fa687..96cd6ca43f 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/javadsl/WatchTest.java @@ -11,7 +11,6 @@ import akka.Done; import akka.actor.testkit.typed.javadsl.TestKitJunitResource; import org.junit.ClassRule; import org.scalatest.junit.JUnitSuite; -import akka.util.Timeout; import org.junit.Test; import akka.actor.typed.*; @@ -42,7 +41,7 @@ public class WatchTest extends JUnitSuite { static final class CustomTerminationMessage implements Message { } - final Timeout timeout = Timeout.create(Duration.ofSeconds(5)); + final Duration timeout = Duration.ofSeconds(5); final Behavior exitingActor = receive((context, message) -> { System.out.println("Stopping!"); diff --git a/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java b/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java index 7e985475cb..843df2ffe0 100644 --- a/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java +++ b/akka-actor-typed-tests/src/test/java/akka/actor/typed/receptionist/ReceptionistApiTest.java @@ -8,17 +8,16 @@ import akka.actor.typed.ActorRef; import akka.actor.typed.ActorSystem; import akka.actor.typed.javadsl.AskPattern; import akka.actor.typed.javadsl.Behaviors; -import akka.util.Timeout; +import java.time.Duration; import java.util.Set; import java.util.concurrent.CompletionStage; -import java.util.concurrent.TimeUnit; public class ReceptionistApiTest { public void compileOnlyApiTest() { // some dummy prerequisites - final Timeout timeout = Timeout.apply(3, TimeUnit.SECONDS); + final Duration timeout = Duration.ofSeconds(3); final ActorRef service = null; final ServiceKey key = ServiceKey.create(String.class, "id"); final ActorSystem system = null; diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java index 206a6d6b7d..e803c3c3ba 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/InteractionPatternsTest.java @@ -10,7 +10,6 @@ import akka.actor.typed.Behavior; import akka.actor.typed.Props; import akka.actor.typed.javadsl.*; import akka.actor.testkit.typed.javadsl.TestProbe; -import akka.util.Timeout; import org.junit.Test; import org.scalatest.junit.JUnitSuite; import scala.concurrent.Await; @@ -382,7 +381,7 @@ public class InteractionPatternsTest extends JUnitSuite { // asking someone requires a timeout, if the timeout hits without response // the ask is failed with a TimeoutException - final Timeout timeout = Timeout.apply(3, TimeUnit.SECONDS); + final Duration timeout = Duration.ofSeconds(3); context.ask( HalResponse.class, @@ -448,7 +447,7 @@ public class InteractionPatternsTest extends JUnitSuite { GiveMeCookies::new, // asking someone requires a timeout and a scheduler, if the timeout hits without response // the ask is failed with a TimeoutException - Timeout.apply(3, TimeUnit.SECONDS), + Duration.ofSeconds(3), system.scheduler()); result.whenComplete((cookies, failure) -> { diff --git a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/SpawnProtocolDocTest.java b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/SpawnProtocolDocTest.java index 5aeea6202b..f0772a33e3 100644 --- a/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/SpawnProtocolDocTest.java +++ b/akka-actor-typed-tests/src/test/java/jdocs/akka/typed/SpawnProtocolDocTest.java @@ -46,7 +46,7 @@ public class SpawnProtocolDocTest { //#system-spawn final ActorSystem system = ActorSystem.create(HelloWorldMain.main, "hello"); - final Timeout timeout = Timeout.create(Duration.ofSeconds(3)); + final Duration timeout = Duration.ofSeconds(3); CompletionStage> greeter = AskPattern.ask( system, diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/ActorContextImpl.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/ActorContextImpl.scala index c433b9f463..a9f2e70948 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/internal/ActorContextImpl.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/internal/ActorContextImpl.scala @@ -5,6 +5,7 @@ package akka.actor.typed package internal +import java.time.Duration import java.util.function.{ Function ⇒ JFunction } import java.util.ArrayList import java.util.Optional @@ -16,7 +17,6 @@ import scala.reflect.ClassTag import scala.util.Failure import scala.util.Success import scala.util.Try - import akka.annotation.InternalApi import akka.util.OptionVal import akka.util.Timeout @@ -90,11 +90,11 @@ import akka.util.JavaDurationConverters._ } // Java API impl - def ask[Req, Res](resClass: Class[Res], target: RecipientRef[Req], responseTimeout: Timeout, createRequest: function.Function[ActorRef[Res], Req], applyToResponse: BiFunction[Res, Throwable, T]): Unit = { + def ask[Req, Res](resClass: Class[Res], target: RecipientRef[Req], responseTimeout: Duration, createRequest: function.Function[ActorRef[Res], Req], applyToResponse: BiFunction[Res, Throwable, T]): Unit = { this.ask(target)(createRequest.apply) { case Success(message) ⇒ applyToResponse.apply(message, null) case Failure(ex) ⇒ applyToResponse.apply(null.asInstanceOf[Res], ex) - }(responseTimeout, ClassTag[Res](resClass)) + }(responseTimeout.asScala, ClassTag[Res](resClass)) } private[akka] override def spawnMessageAdapter[U](f: U ⇒ T, name: String): ActorRef[U] = diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ActorContext.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ActorContext.scala index e346b82c1f..bc7a7eff84 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ActorContext.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/ActorContext.scala @@ -186,7 +186,7 @@ trait ActorContext[T] extends akka.actor.typed.ActorContext[T] { * *Warning*: This method is not thread-safe and must not be accessed from threads other * than the ordinary actor message processing thread, such as [[java.util.concurrent.CompletionStage]] callbacks. */ - def setReceiveTimeout(d: Duration, msg: T): Unit + def setReceiveTimeout(timeout: Duration, msg: T): Unit /** * Cancel the sending of receive timeout notifications. @@ -275,7 +275,7 @@ trait ActorContext[T] extends akka.actor.typed.ActorContext[T] { def ask[Req, Res]( resClass: Class[Res], target: RecipientRef[Req], - responseTimeout: Timeout, + responseTimeout: Duration, createRequest: java.util.function.Function[ActorRef[Res], Req], applyToResponse: BiFunction[Res, Throwable, T]): Unit diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Ask.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/AskPattern.scala similarity index 84% rename from akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Ask.scala rename to akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/AskPattern.scala index 5003c0a884..1121fa08ec 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/Ask.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/javadsl/AskPattern.scala @@ -5,12 +5,13 @@ package akka.actor.typed package javadsl +import java.time.Duration import java.util.concurrent.CompletionStage import akka.actor.Scheduler import akka.actor.typed.scaladsl.AskPattern._ import akka.japi.function.{ Function ⇒ JFunction } -import akka.util.Timeout +import akka.util.JavaDurationConverters._ import scala.compat.java8.FutureConverters._ @@ -29,6 +30,6 @@ import scala.compat.java8.FutureConverters._ * */ object AskPattern { - def ask[T, U](actor: ActorRef[T], message: JFunction[ActorRef[U], T], timeout: Timeout, scheduler: Scheduler): CompletionStage[U] = - (actor.?(message.apply)(timeout, scheduler)).toJava + def ask[T, U](actor: ActorRef[T], message: JFunction[ActorRef[U], T], timeout: Duration, scheduler: Scheduler): CompletionStage[U] = + (actor.?(message.apply)(timeout.asScala, scheduler)).toJava } diff --git a/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/ActorContext.scala b/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/ActorContext.scala index 44ea0f0579..44f05b29bd 100644 --- a/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/ActorContext.scala +++ b/akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl/ActorContext.scala @@ -167,7 +167,7 @@ trait ActorContext[T] extends akka.actor.typed.ActorContext[T] { this: akka.acto * *Warning*: This method is not thread-safe and must not be accessed from threads other * than the ordinary actor message processing thread, such as [[scala.concurrent.Future]] callbacks. */ - def setReceiveTimeout(d: FiniteDuration, msg: T): Unit + def setReceiveTimeout(timeout: FiniteDuration, msg: T): Unit /** * Cancel the sending of receive timeout notifications. diff --git a/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java b/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java index f62f94972f..de2ed847fe 100644 --- a/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java +++ b/akka-persistence-typed/src/test/java/akka/persistence/typed/javadsl/PersistentActorCompileOnlyTest.java @@ -13,11 +13,10 @@ import akka.persistence.typed.EventAdapter; import akka.actor.testkit.typed.javadsl.TestInbox; import akka.persistence.typed.PersistenceId; import akka.persistence.typed.SideEffect; -import akka.util.Timeout; +import java.time.Duration; import java.util.*; import java.util.concurrent.CompletionStage; -import java.util.concurrent.TimeUnit; import static akka.actor.typed.javadsl.AskPattern.ask; @@ -266,7 +265,7 @@ public class PersistentActorCompileOnlyTest { } static ActorRef sideEffectProcessor = TestInbox.create().getRef(); - static Timeout timeout = new Timeout(1, TimeUnit.SECONDS); + static Duration timeout = Duration.ofSeconds(1); private static void performSideEffect(ActorRef sender, int correlationId, String data, Scheduler scheduler) { CompletionStage what = ask(sideEffectProcessor, (ActorRef ar) -> new Request(correlationId, data, ar), timeout, scheduler);