Interface ContextLocal<T>
-
public interface ContextLocal<T>
A local storage for arbitrary data attached to a
Context
.Local storage should be registered before creating a
Vertx
instance, once registered a local storage cannot be unregistered.It is recommended to initialize local storage as static fields of a
VertxServiceProvider
, since providers are discovered before the capture of known local storages.public class CustomLocal implements VertxServiceProvider { public static final ContextLocal<CustomLocal> KEY = ContextLocal.registerLocal(CustomLocal.class); ... }
Such provider must then be declared as a Java service provider in
META/INF/services/io.vertx.core.spi.VertxServiceProvider
and optionally in amodule-info.java
.Context local can be used from a Vert.x
Context
with the following methods.Context.getLocal(ContextLocal)
Context.getLocal(ContextLocal, Supplier)
Context.putLocal(ContextLocal, Object)
Context.removeLocal(ContextLocal)
Context.getLocal(ContextLocal, AccessMode)
Context.getLocal(ContextLocal, AccessMode, Supplier)
Context.putLocal(ContextLocal, AccessMode, Object)
Context.removeLocal(ContextLocal, AccessMode)
context.putLocal(CustomLocal.KEY, new CustomLocal(...));
- Author:
- Julien Viet
-
-
Method Summary
All Methods Static Methods Instance Methods Default Methods Modifier and Type Method Description default T
get(Context context)
Get the local data from thecontext
.default T
get(Context context, AccessMode accessMode)
Likeget(Context)
but with anaccessMode
.default T
get(Context context, AccessMode accessMode, Supplier<? extends T> initialValueSupplier)
Likeget(Context, Supplier)
but with anaccessMode
.default T
get(Context context, Supplier<? extends T> initialValueSupplier)
Get the local data from thecontext
, when it does not exist then callinitialValueSupplier
to obtain the initial value.default void
put(Context context, AccessMode accessMode, T value)
Like#put(Context, T)
but with anaccessMode
.default void
put(Context context, T data)
Put local data in thecontext
.static <T> ContextLocal<T>
registerLocal(Class<T> type)
Registers a context local storage.static <T> ContextLocal<T>
registerLocal(Class<T> type, Function<T,T> duplicator)
Registers a context local storage.default void
remove(Context context)
Remove the local data from the context.default void
remove(Context context, AccessMode accessMode)
Likeremove(Context)
but with anaccessMode
.
-
-
-
Method Detail
-
registerLocal
static <T> ContextLocal<T> registerLocal(Class<T> type)
Registers a context local storage.- Returns:
- the context local storage
-
registerLocal
static <T> ContextLocal<T> registerLocal(Class<T> type, Function<T,T> duplicator)
Registers a context local storage.- Returns:
- the context local storage
-
get
default T get(Context context, Supplier<? extends T> initialValueSupplier)
Get the local data from thecontext
, when it does not exist then callinitialValueSupplier
to obtain the initial value. The supplier can be called multiple times when several threads call this method concurrently.- Parameters:
initialValueSupplier
- the supplier of the initial value- Returns:
- the local data
-
put
default void put(Context context, T data)
Put local data in thecontext
.- Parameters:
data
- the data
-
remove
default void remove(Context context)
Remove the local data from the context.
-
get
default T get(Context context, AccessMode accessMode)
Likeget(Context)
but with anaccessMode
.
-
get
default T get(Context context, AccessMode accessMode, Supplier<? extends T> initialValueSupplier)
Likeget(Context, Supplier)
but with anaccessMode
.
-
put
default void put(Context context, AccessMode accessMode, T value)
Like#put(Context, T)
but with anaccessMode
.
-
remove
default void remove(Context context, AccessMode accessMode)
Likeremove(Context)
but with anaccessMode
.
-
-