diff --git a/vertx-core/src/main/java/io/vertx/core/Future.java b/vertx-core/src/main/java/io/vertx/core/Future.java index b5398445a20..9486305a04a 100644 --- a/vertx-core/src/main/java/io/vertx/core/Future.java +++ b/vertx-core/src/main/java/io/vertx/core/Future.java @@ -11,6 +11,7 @@ package io.vertx.core; +import io.vertx.codegen.annotations.GenIgnore; import io.vertx.core.impl.WorkerExecutor; import io.vertx.core.internal.ContextInternal; import io.vertx.core.impl.Utils; @@ -18,6 +19,7 @@ import io.vertx.core.impl.future.FailedFuture; import io.vertx.core.impl.future.SucceededFuture; +import java.time.Duration; import java.util.List; import java.util.Objects; import java.util.concurrent.*; @@ -631,6 +633,17 @@ default Future andThen(Completable handler) { */ Future timeout(long delay, TimeUnit unit); + /** + * Like {@link #timeout(long, TimeUnit)}. + * + * @param delay the delay + * @return the timeout future + */ + @GenIgnore(GenIgnore.PERMITTED_TYPE) + default Future timeout(Duration delay) { + return timeout(delay.toMillis(), TimeUnit.MILLISECONDS); + } + /** * Bridges this Vert.x future to a {@link CompletionStage} instance. *

@@ -746,6 +759,19 @@ default T await() { return getOrFail(); } + /** + * Like {@link #await()} but with a timeout. + * + * @param timeout the timeout + * @return the result + * @throws TimeoutException when the timeout fires before the future completes + * @throws IllegalStateException when called from a vertx event-loop or worker thread + */ + @GenIgnore(GenIgnore.PERMITTED_TYPE) + default T await(Duration timeout) throws TimeoutException { + return await(timeout.toMillis(), TimeUnit.MILLISECONDS); + } + /** * Like {@link #await()} but with a timeout. * diff --git a/vertx-core/src/main/java/io/vertx/core/Vertx.java b/vertx-core/src/main/java/io/vertx/core/Vertx.java index 2b4e189e2eb..532dfb998a6 100644 --- a/vertx-core/src/main/java/io/vertx/core/Vertx.java +++ b/vertx-core/src/main/java/io/vertx/core/Vertx.java @@ -48,6 +48,7 @@ import io.vertx.core.spi.cluster.ClusterManager; import io.vertx.core.transport.Transport; +import java.time.Duration; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @@ -596,6 +597,17 @@ default Timer timer(long delay, TimeUnit unit) { return ctx.timer(delay, unit); } + /** + * Like {@link #timer(long, TimeUnit)}. + * + * @param delay the delay + * @return the timer object + */ + @GenIgnore(GenIgnore.PERMITTED_TYPE) + default Timer timer(Duration delay) { + return timer(delay.toMillis(), TimeUnit.MILLISECONDS); + } + /** * Set a one-shot timer to fire after {@code delay} milliseconds, at which point {@code handler} will be called with * the id of the timer.