diff --git a/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java b/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java
index 77933c7d9a..b66732bb16 100644
--- a/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java
+++ b/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java
@@ -19,11 +19,9 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.scalatest.junit.JUnitSuite;
-import scala.concurrent.duration.Duration;
-import scala.util.control.NoStackTrace;
import java.util.*;
-import java.util.concurrent.TimeUnit;
+import java.time.Duration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -145,7 +143,7 @@ public class LoggingAdapterTest extends JUnitSuite {
}
void expectLog(final Object level, final String message, final Throwable cause, final String mdc) {
- expectMsgPF(Duration.create(3, TimeUnit.SECONDS), "LogEvent", event -> {
+ expectMsgPF(Duration.ofSeconds(3), "LogEvent", event -> {
LogEvent log = (LogEvent) event;
assertEquals(message, log.message());
assertEquals(level, log.level());
diff --git a/akka-actor-tests/src/test/java/akka/pattern/PatternsTest.java b/akka-actor-tests/src/test/java/akka/pattern/PatternsTest.java
index d31d5a7dba..b27cedc176 100644
--- a/akka-actor-tests/src/test/java/akka/pattern/PatternsTest.java
+++ b/akka-actor-tests/src/test/java/akka/pattern/PatternsTest.java
@@ -119,7 +119,7 @@ public class PatternsTest extends JUnitSuite {
.askWithReplyTo(
echo,
replyTo -> new ExplicitAskTestActor.Message(expected, replyTo),
- Timeout.apply(3, SECONDS))
+ Duration.ofSeconds(3))
.thenApply(o -> (String)o);
final String actual = response.toCompletableFuture().get(3, SECONDS);
@@ -136,7 +136,8 @@ public class PatternsTest extends JUnitSuite {
.askWithReplyTo(
echo,
replyTo -> new ExplicitAskTestActor.Message(expected, replyTo),
- 3000)
+ 3000
+ )
.thenApply(o -> (String)o);
final String actual = response.toCompletableFuture().get(3, SECONDS);
@@ -153,7 +154,8 @@ public class PatternsTest extends JUnitSuite {
.askWithReplyTo(
selection,
replyTo -> new ExplicitAskTestActor.Message(expected, replyTo),
- 3000)
+ 3000
+ )
.thenApply(o -> (String)o);
final String actual = response.toCompletableFuture().get(3, SECONDS);
diff --git a/akka-actor/src/main/scala/akka/pattern/Patterns.scala b/akka-actor/src/main/scala/akka/pattern/Patterns.scala
index e4ba1616d5..d7fc651153 100644
--- a/akka-actor/src/main/scala/akka/pattern/Patterns.scala
+++ b/akka-actor/src/main/scala/akka/pattern/Patterns.scala
@@ -318,9 +318,38 @@ object PatternsCS {
* f.thenRun(result -> nextActor.tell(new EnrichedResult(request, result)));
* }}}
*/
+ @deprecated("Use the overloaded one which accepts java.time.Duration instead.", since = "2.5.15")
def ask(actor: ActorRef, message: Any, timeout: Timeout): CompletionStage[AnyRef] =
scalaAsk(actor, message)(timeout).toJava.asInstanceOf[CompletionStage[AnyRef]]
+ /**
+ * Java API for `akka.pattern.ask`:
+ * Sends a message asynchronously and returns a [[java.util.concurrent.CompletionStage]]
+ * holding the eventual reply message; this means that the target actor
+ * needs to send the result to the `sender` reference provided. The CompletionStage
+ * will be completed with an [[akka.pattern.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)`).
+ *
+ * Warning:
+ * 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.
+ *
+ * Recommended usage:
+ *
+ * {{{
+ * final CompletionStage