Package io.servicetalk.concurrent.api
Class AsyncContext
- java.lang.Object
-
- io.servicetalk.concurrent.api.AsyncContext
-
public final class AsyncContext extends java.lang.Object
Presents a static interface to retain state in an asynchronous environment.This should not be used as a "catch all" to avoid designing APIs which accommodate for your needs. This should be used as a last resort (e.g. for low level framework or infrastructure like tasks) because there maybe non-trivial overhead required to maintain this context.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
clear()
Convenience method to clear all the key/value pairs from the current context.static boolean
containsKey(AsyncContextMap.Key<?> key)
Convenience method to determine if the current context contains a key/value entry corresponding tokey
.static AsyncContextMap
current()
Get the currentAsyncContextMap
.static void
disable()
Disable AsyncContext.static AsyncContextMap.Key<?>
forEach(java.util.function.BiPredicate<AsyncContextMap.Key<?>,java.lang.Object> consumer)
Convenience method to iterate over the key/value pairs contained in the current context.static <T> T
get(AsyncContextMap.Key<T> key)
Convenience method to get the value associated withkey
from the current context.static boolean
isDisabled()
Determine ifdisable()
has been previously called.static boolean
isEmpty()
Convenience method to determine if there are no key/value pairs in the current context.static <T> void
put(AsyncContextMap.Key<T> key, T value)
Convenience method for adding a value to the current context.static void
putAll(java.util.Map<AsyncContextMap.Key<?>,java.lang.Object> map)
Convenience method for to put all the key/value pairs into the current context.static void
remove(AsyncContextMap.Key<?> key)
Convenience method to remove a key/value pair from the current context.static void
removeAll(java.lang.Iterable<AsyncContextMap.Key<?>> entries)
Convenience method to remove all the key/value pairs from the current context.static <T,U>
java.util.function.BiConsumer<T,U>wrapBiConsume(java.util.function.BiConsumer<T,U> consumer)
Wrap aBiFunction
to ensure it is able to trackAsyncContext
correctly.static <T,U,V>
java.util.function.BiFunction<T,U,V>wrapBiFunction(java.util.function.BiFunction<T,U,V> func)
Wrap aBiFunction
to ensure it is able to trackAsyncContext
correctly.static <T> java.util.function.Consumer<T>
wrapConsumer(java.util.function.Consumer<T> consumer)
Wrap aConsumer
to ensure it is able to trackAsyncContext
correctly.static Executor
wrapExecutor(Executor executor)
Wrap anExecutor
to ensure it is able to trackAsyncContext
correctly.static <T,U>
java.util.function.Function<T,U>wrapFunction(java.util.function.Function<T,U> func)
Wrap aFunction
to ensure it is able to trackAsyncContext
correctly.static java.util.concurrent.Executor
wrapJdkExecutor(java.util.concurrent.Executor executor)
Wrap anExecutor
to ensure it is able to trackAsyncContext
correctly.static java.util.concurrent.ExecutorService
wrapJdkExecutorService(java.util.concurrent.ExecutorService executor)
Wrap anExecutorService
to ensure it is able to trackAsyncContext
correctly.static java.util.concurrent.ScheduledExecutorService
wrapJdkScheduledExecutorService(java.util.concurrent.ScheduledExecutorService executor)
Wrap aScheduledExecutorService
to ensure it is able to trackAsyncContext
correctly.static java.lang.Runnable
wrapRunnable(java.lang.Runnable runnable)
Wrap aRunnable
to ensure it is able to trackAsyncContext
correctly.
-
-
-
Method Detail
-
current
public static AsyncContextMap current()
Get the currentAsyncContextMap
.- Returns:
- the current
AsyncContextMap
-
put
public static <T> void put(AsyncContextMap.Key<T> key, T value)
Convenience method for adding a value to the current context.- Type Parameters:
T
- The type of object associated withkey
.- Parameters:
key
- the key used to indexvalue
. Cannot benull
.value
- the value to put.- See Also:
AsyncContextMap.put(AsyncContextMap.Key, Object)
-
putAll
public static void putAll(java.util.Map<AsyncContextMap.Key<?>,java.lang.Object> map)
Convenience method for to put all the key/value pairs into the current context.- Parameters:
map
- contains the key/value pairs that will be added.- See Also:
AsyncContextMap.putAll(Map)
-
remove
public static void remove(AsyncContextMap.Key<?> key)
Convenience method to remove a key/value pair from the current context.- Parameters:
key
- The key to remove.- See Also:
AsyncContextMap.remove(AsyncContextMap.Key)
-
removeAll
public static void removeAll(java.lang.Iterable<AsyncContextMap.Key<?>> entries)
Convenience method to remove all the key/value pairs from the current context.- Parameters:
entries
- AIterable
which contains all the keys to remove.- See Also:
AsyncContextMap.removeAll(Iterable)
-
clear
public static void clear()
Convenience method to clear all the key/value pairs from the current context.- See Also:
AsyncContextMap.clear()
-
get
@Nullable public static <T> T get(AsyncContextMap.Key<T> key)
Convenience method to get the value associated withkey
from the current context.- Type Parameters:
T
- The anticipated type of object associated withkey
.- Parameters:
key
- the key to lookup.- Returns:
- the value associated with
key
, ornull
if no value is associated. - See Also:
AsyncContextMap.get(AsyncContextMap.Key)
-
containsKey
public static boolean containsKey(AsyncContextMap.Key<?> key)
Convenience method to determine if the current context contains a key/value entry corresponding tokey
.- Parameters:
key
- the key to lookup.- Returns:
true
if the current context contains a key/value entry corresponding tokey
.false
otherwise.- See Also:
AsyncContextMap.containsKey(AsyncContextMap.Key)
-
isEmpty
public static boolean isEmpty()
Convenience method to determine if there are no key/value pairs in the current context.- Returns:
true
if there are no key/value pairs in the current context.- See Also:
AsyncContextMap.isEmpty()
-
forEach
@Nullable public static AsyncContextMap.Key<?> forEach(java.util.function.BiPredicate<AsyncContextMap.Key<?>,java.lang.Object> consumer)
Convenience method to iterate over the key/value pairs contained in the current context.- Parameters:
consumer
- Each key/value pair will be passed as arguments to thisBiPredicate
. Returnstrue
if the consumer wants to keep iterating orfalse
to stop iteration at the current key/value pair.- Returns:
null
ifconsumer
iterated through all key/value pairs or theAsyncContextMap.Key
at which the iteration stopped.- See Also:
AsyncContextMap.forEach(BiPredicate)
-
wrapJdkExecutor
public static java.util.concurrent.Executor wrapJdkExecutor(java.util.concurrent.Executor executor)
Wrap anExecutor
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapExecutor
public static Executor wrapExecutor(Executor executor)
Wrap anExecutor
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkExecutorService
public static java.util.concurrent.ExecutorService wrapJdkExecutorService(java.util.concurrent.ExecutorService executor)
Wrap anExecutorService
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkScheduledExecutorService
public static java.util.concurrent.ScheduledExecutorService wrapJdkScheduledExecutorService(java.util.concurrent.ScheduledExecutorService executor)
Wrap aScheduledExecutorService
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapRunnable
public static java.lang.Runnable wrapRunnable(java.lang.Runnable runnable)
Wrap aRunnable
to ensure it is able to trackAsyncContext
correctly.- Parameters:
runnable
- The runnable to wrap.- Returns:
- The wrapped
Runnable
.
-
wrapConsumer
public static <T> java.util.function.Consumer<T> wrapConsumer(java.util.function.Consumer<T> consumer)
Wrap aConsumer
to ensure it is able to trackAsyncContext
correctly.- Type Parameters:
T
- The type of data consumed byconsumer
.- Parameters:
consumer
- The consumer to wrap.- Returns:
- The wrapped
Consumer
.
-
wrapFunction
public static <T,U> java.util.function.Function<T,U> wrapFunction(java.util.function.Function<T,U> func)
Wrap aFunction
to ensure it is able to trackAsyncContext
correctly.- Type Parameters:
T
- The type of data consumed byfunc
.U
- The type of data returned byfunc
.- Parameters:
func
- The function to wrap.- Returns:
- The wrapped
Function
.
-
wrapBiConsume
public static <T,U> java.util.function.BiConsumer<T,U> wrapBiConsume(java.util.function.BiConsumer<T,U> consumer)
Wrap aBiFunction
to ensure it is able to trackAsyncContext
correctly.- Type Parameters:
T
- The type of data consumed byfunc
.U
- The type of data consumed byfunc
.- Parameters:
consumer
- The consumer to wrap.- Returns:
- The wrapped
BiConsumer
.
-
wrapBiFunction
public static <T,U,V> java.util.function.BiFunction<T,U,V> wrapBiFunction(java.util.function.BiFunction<T,U,V> func)
Wrap aBiFunction
to ensure it is able to trackAsyncContext
correctly.- Type Parameters:
T
- The type of data consumed byfunc
.U
- The type of data consumed byfunc
.V
- The type of data returned byfunc
.- Parameters:
func
- The function to wrap.- Returns:
- The wrapped
BiFunction
.
-
disable
public static void disable()
Disable AsyncContext. It is assumed the application will call this in a well orchestrated fashion. For example the behavior of in flight AsyncContext is undefined, objects that are already initialized with AsyncContext enabled may continue to preserve AsyncContext in an unreliable fashion. and also how this behaves relative to concurrent invocation is undefined. External synchronization should be used to ensure this change is visible to other threads.
-
-