Package io.helidon.common.context
Class Contexts
- java.lang.Object
-
- io.helidon.common.context.Contexts
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Optional<Context>context()Get context registry associated with current thread.static voidrunInContext(Context context, Runnable runnable)Run the runnable in the provided context.static <T> TrunInContext(Context context, Callable<T> callable)Run the callable in the provided context.static <T> TrunInContextWithThrow(Context context, Callable<T> callable)Run the callable in the provided context throwing any exception from its execution.static ExecutorServicewrap(ExecutorService toWrap)Wrap an executor service to correctly propagate context to its threads.static ScheduledExecutorServicewrap(ScheduledExecutorService toWrap)Wrap a scheduled executor service to correctly propagate context to its threads.
-
-
-
Method Detail
-
context
public static Optional<Context> context()
Get context registry associated with current thread.- Returns:
- context that is associated with current thread or empty if none is
-
wrap
public static ExecutorService wrap(ExecutorService toWrap)
Wrap an executor service to correctly propagate context to its threads.- Parameters:
toWrap- executor service- Returns:
- a new executor service wrapping the provided one
-
wrap
public static ScheduledExecutorService wrap(ScheduledExecutorService toWrap)
Wrap a scheduled executor service to correctly propagate context to its threads. Note that all scheduled methods are going to run in context of the thread scheduling the tasks.- Parameters:
toWrap- executor service- Returns:
- a new executor service wrapping the provided one
-
runInContext
public static void runInContext(Context context, Runnable runnable)
Run the runnable in the provided context. The runnable can usecontext()to retrieve the context.- Parameters:
context- context to run inrunnable- runnable to execute in context
-
runInContext
public static <T> T runInContext(Context context, Callable<T> callable)
Run the callable in the provided context. The callable can usecontext()to retrieve the context.- Type Parameters:
T- return type of the callable- Parameters:
context- context to run incallable- callable to execute in context- Returns:
- the result of the callable
- Throws:
RuntimeException- in case theCallable.call()threw a runtime exception
-
runInContextWithThrow
public static <T> T runInContextWithThrow(Context context, Callable<T> callable) throws Exception
Run the callable in the provided context throwing any exception from its execution. The callable can usecontext()to retrieve the context.- Type Parameters:
T- return type of the callable- Parameters:
context- context to run incallable- callable to execute in context- Returns:
- the result of the callable
- Throws:
Exception- If thrown inCallable.call()
-
-