Package org.eclipse.hono.util
Class Futures
java.lang.Object
org.eclipse.hono.util.Futures
Helper class working with Java and vert.x futures.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Interface representing blocking code to be executed. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> io.vertx.core.Future<T>
create
(Supplier<CompletionStage<T>> futureSupplier) Create a completion stage and map the outcome to a vert.x future.static <T> io.vertx.core.Future<T>
executeBlocking
(io.vertx.core.Vertx vertx, Futures.BlockingCode<T> blocking) UseVertx.executeBlocking(Handler, Handler)
with Futures.static <T> io.vertx.core.Future<T>
executeOnContextWithSameRoot
(io.vertx.core.Context requiredContextOrContextRoot, io.vertx.core.Handler<io.vertx.core.Promise<T>> codeToRun) Executes some code, ensuring it is run on the given context or a duplicated context of the given context.static <T> io.vertx.core.Handler<io.vertx.core.AsyncResult<T>>
onCurrentContextCompletionHandler
(io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> handlerToComplete) Gets a handler that completes the given handler on the current Vert.x context (if set).static <T> void
tryHandleResult
(io.vertx.core.Promise<T> promise, io.vertx.core.AsyncResult<T> asyncResult) Applies the given result on the given promise usingPromise.tryComplete(Object)
orPromise.tryFail(Throwable)
.
-
Method Details
-
create
Create a completion stage and map the outcome to a vert.x future.If the supplier fails providing a future, then the failure will be reported on the returned future as well.
- Type Parameters:
T
- The result data type.- Parameters:
futureSupplier
- The supplier of the future.- Returns:
- A vert.x future, never returns
null
. - Throws:
NullPointerException
- if futureSupplier isnull
.
-
executeBlocking
public static <T> io.vertx.core.Future<T> executeBlocking(io.vertx.core.Vertx vertx, Futures.BlockingCode<T> blocking) UseVertx.executeBlocking(Handler, Handler)
with Futures.- Type Parameters:
T
- The type of the result.- Parameters:
vertx
- The vertx context.blocking
- The blocking code.- Returns:
- The future, reporting the result.
- Throws:
NullPointerException
- if any of the parameters isnull
.
-
executeOnContextWithSameRoot
public static <T> io.vertx.core.Future<T> executeOnContextWithSameRoot(io.vertx.core.Context requiredContextOrContextRoot, io.vertx.core.Handler<io.vertx.core.Promise<T>> codeToRun) Executes some code, ensuring it is run on the given context or a duplicated context of the given context.If invoked from a context that is either equal to the given context or a duplicated context of it, the code is run directly. Otherwise it is scheduled to be run asynchronously on the given context.
- Type Parameters:
T
- The type of the result that the code produces.- Parameters:
requiredContextOrContextRoot
- The context to run the code on.codeToRun
- The code to execute. The code is required to either complete or fail the promise that is passed into the handler.- Returns:
- The future containing the result of the promise passed in to the handler for executing the code. The future thus indicates the outcome of executing the code.
- Throws:
NullPointerException
- If any of the parameters isnull
.
-
tryHandleResult
public static <T> void tryHandleResult(io.vertx.core.Promise<T> promise, io.vertx.core.AsyncResult<T> asyncResult) Applies the given result on the given promise usingPromise.tryComplete(Object)
orPromise.tryFail(Throwable)
.Similar to
Promise.handle(AsyncResult)
but does nothing if the promise is already complete.- Type Parameters:
T
- The promise and result type.- Parameters:
promise
- The promise to complete or fail.asyncResult
- The result to apply.- Throws:
NullPointerException
- if any of the parameters isnull
.
-
onCurrentContextCompletionHandler
public static <T> io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> onCurrentContextCompletionHandler(io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> handlerToComplete) Gets a handler that completes the given handler on the current Vert.x context (if set).- Type Parameters:
T
- The type of the result.- Parameters:
handlerToComplete
- The handler to apply the result of the returned handler on.- Returns:
- The handler that will wrap the given handler so that it is handled on the original context.
- Throws:
NullPointerException
- if handlerToComplete isnull
.
-