Package io.servicetalk.concurrent.api
Class AsyncContext
java.lang.Object
io.servicetalk.concurrent.api.AsyncContext
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
Modifier and TypeMethodDescriptionstatic CapturedContextGrab a reference of the current context.static voidclear()Convenience method to clear all the entries from the current context.static <T> TcomputeIfAbsent(ContextMap.Key<T> key, Function<ContextMap.Key<T>, T> computeFunction) Convenience method to compute a new entry for the current context if this map does not already contain thiskeyor is mapped tonull.static <T> booleancontains(ContextMap.Key<T> key, T value) Convenience method to determine if this context contains an entry matching the passedkeyandvalue.static booleancontainsKey(ContextMap.Key<?> key) Convenience method to determine if the current context contains an entry corresponding to thekey.static booleancontainsValue(Object value) Convenience method to determine if the current context contains an entry with the specifiedvalue.static ContextMapcontext()Get the currentContextMap.static voiddisable()Disable AsyncContext.static ContextMap.Key<?>forEach(BiPredicate<ContextMap.Key<?>, Object> consumer) Convenience method to iterate over the entries contained in the current context.static ContextMap.Key<?>forEachEntry(BiPredicate<ContextMap.Key<?>, Object> consumer) Deprecated.static <T> Tget(ContextMap.Key<T> key) Convenience method to get the value associated with thekeyfrom the current context.static <T> TgetOrDefault(ContextMap.Key<T> key, T defaultValue) Convenience method to get the value associated withkeyfrom the current context, ordefaultValueif no value is associated.static booleanDetermine ifdisable()has been previously called.static booleanisEmpty()Convenience method to determine if there are no entries in the current context.static <T> Tput(ContextMap.Key<T> key, T value) Convenience method to put a new entry to the current context.static voidputAll(ContextMap map) Convenience method to put all the entries into the current context from anotherContextMap.static voidputAll(Map<ContextMap.Key<?>, Object> map) Convenience method to put all the entries into the current context.static voidputAllFromMap(Map<ContextMap.Key<?>, Object> map) Deprecated.UseputAll(Map)static <T> TputIfAbsent(ContextMap.Key<T> key, T value) Convenience method to put a new entry to the current context if this map does not already contain thiskeyor is mapped tonull.static <T> Tremove(ContextMap.Key<T> key) Convenience method to remove an entry from the current context.static booleanremoveAll(Iterable<ContextMap.Key<?>> keys) Convenience method to remove all entries from the current context associated with the keys from the passedIterable.static booleanremoveAllEntries(Iterable<ContextMap.Key<?>> keys) Deprecated.static intsize()Convenience method to determine the number of entries in the current context.static <T,U> BiConsumer<T, U> wrapBiConsume(BiConsumer<T, U> consumer) Wrap aBiFunctionto ensure it is able to trackAsyncContextcorrectly.static <T,U, V> BiFunction<T, U, V> wrapBiFunction(BiFunction<T, U, V> func) Wrap aBiFunctionto ensure it is able to trackAsyncContextcorrectly.static <V> Callable<V>wrapCallable(Callable<V> callable) Wrap aCallableto ensure it is able to trackAsyncContextcorrectly.static <T> Consumer<T>wrapConsumer(Consumer<T> consumer) Wrap aConsumerto ensure it is able to trackAsyncContextcorrectly.static ExecutorwrapExecutor(Executor executor) Wrap anExecutorto ensure it is able to trackAsyncContextcorrectly.static <T,U> Function<T, U> wrapFunction(Function<T, U> func) Wrap aFunctionto ensure it is able to trackAsyncContextcorrectly.static ExecutorwrapJdkExecutor(Executor executor) Wrap anExecutorto ensure it is able to trackAsyncContextcorrectly.static ExecutorServicewrapJdkExecutorService(ExecutorService executor) Wrap anExecutorServiceto ensure it is able to trackAsyncContextcorrectly.static ScheduledExecutorServiceWrap aScheduledExecutorServiceto ensure it is able to trackAsyncContextcorrectly.static RunnablewrapRunnable(Runnable runnable) Wrap aRunnableto ensure it is able to trackAsyncContextcorrectly.
-
Method Details
-
context
Get the currentContextMap.- Returns:
- the current
ContextMap
-
captureContext
Grab a reference of the current context.- Returns:
- the current context.
-
put
Convenience method to put a new entry to the current context.- Type Parameters:
T- The type of object associated withkey.- Parameters:
key- TheContextMap.Keyused to index thevalue.value- the value to put.- Returns:
- The previous value associated with the
key, ornullif there was none.nullcan also indicate the value associated withkeyisnull(ifnullvalues are supported by the implementation). - Throws:
NullPointerException- (optional behavior) ifkeyorvalueisnulland the underlyingContextMapimplementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
putIfAbsent
Convenience method to put a new entry to the current context if this map does not already contain thiskeyor is mapped tonull.- Type Parameters:
T- The type of object associated withkey.- Parameters:
key- TheContextMap.Keyused to index thevalue.value- the value to put.- Returns:
- The previous value associated with the
key, ornullif there was none.nullcan also indicate the value associated withkeyisnull(ifnullvalues are supported by the implementation). - Throws:
NullPointerException- (optional behavior) ifkeyorvalueisnulland the underlyingContextMapimplementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
computeIfAbsent
@Nullable public static <T> T computeIfAbsent(ContextMap.Key<T> key, Function<ContextMap.Key<T>, T> computeFunction) Convenience method to compute a new entry for the current context if this map does not already contain thiskeyor is mapped tonull.- Type Parameters:
T- The type of object associated withkey.- Parameters:
key- TheContextMap.Keyused to index thevalue.computeFunction- The function to compute a new value. Implementation may invoke this function multiple times if concurrent threads attempt modifying this context map, result is expected to be idempotent.- Returns:
- The previous value associated with the
key, ornullif there was none.nullcan also indicate the value associated withkeyisnull(ifnullvalues are supported by the implementation). - Throws:
NullPointerException- (optional behavior) ifkeyor computedvalueisnulland the underlyingContextMapimplementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
putAll
Convenience method to put all the entries into the current context from anotherContextMap.- Parameters:
map- contains the entries that will be added.- Throws:
ConcurrentModificationException- done on a best effort basis if the passedmapis detected to be modified while attempting to put all entries.NullPointerException- (optional behavior) if any of themapentries has anullkeyorvalueand the underlyingContextMapimplementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
putAll
Convenience method to put all the entries into the current context.- Parameters:
map- contains the entries that will be added.- Throws:
ConcurrentModificationException- done on a best effort basis if the passedmapis detected to be modified while attempting to put all entries.NullPointerException- (optional behavior) if any of themapentries has anullkeyorvalueand the underlyingContextMapimplementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
putAllFromMap
Deprecated.UseputAll(Map)Convenience method to put all the entries into the current context.- Parameters:
map- contains the entries that will be added.- Throws:
ConcurrentModificationException- done on a best effort basis if the passedmapis detected to be modified while attempting to put all entries.NullPointerException- (optional behavior) if any of themapentries has anullkeyorvalueand the underlyingContextMapimplementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
remove
Convenience method to remove an entry from the current context.- Type Parameters:
T- The type of object associated withkey.- Parameters:
key- TheContextMap.Keywhich identifies an entry for removal.- Returns:
- the previous value associated with
key, ornullif there was none. Anullvalue may also indicate there was a previous value which wasnull. - Throws:
NullPointerException- (optional behavior) ifkeyisnulland the underlyingContextMapimplementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
removeAll
Convenience method to remove all entries from the current context associated with the keys from the passedIterable.- Parameters:
keys- TheContextMap.Keys that identify entries for removal.- Returns:
trueif this map has changed as a result of this operation.- Throws:
NullPointerException- (optional behavior) if any of thekeysisnulland the underlyingContextMapimplementation doesn't supportnullkeys.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
removeAllEntries
Deprecated.Convenience method to remove all entries from the current context associated with the keys from the passedIterable.- Parameters:
keys- TheContextMap.Keys that identify entries for removal.- Returns:
trueif this map has changed as a result of this operation.- Throws:
NullPointerException- (optional behavior) if any of thekeysisnulland the underlyingContextMapimplementation doesn't supportnullkeys.UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
clear
public static void clear()Convenience method to clear all the entries from the current context.- Throws:
UnsupportedOperationException- if this method is not supported by the underlyingContextMapimplementation.- See Also:
-
get
Convenience method to get the value associated with thekeyfrom the current context.- Type Parameters:
T- The anticipated type of object associated withkey.- Parameters:
key- TheContextMap.Keyto lookup.- Returns:
- the value associated with the
key, ornullif no value is associated.nullcan also indicate the value associated withkeyisnull(ifnullvalues are supported by the underlyingContextMapimplementation). - Throws:
NullPointerException- (optional behavior) ifkeyisnulland the underlyingContextMapimplementation doesn't supportnullkeys or values.- See Also:
-
getOrDefault
Convenience method to get the value associated withkeyfrom the current context, ordefaultValueif no value is associated.- Type Parameters:
T- The anticipated type of object associated withkey.- Parameters:
key- TheContextMap.Keyto lookup.defaultValue- The value to return if no value is associated with thekey.- Returns:
- The value associated with the
key(can returnnullifnullvalues are supported by the underlyingContextMapimplementation), ordefaultValueif no value is associated. - Throws:
NullPointerException- (optional behavior) ifkeyisnulland the underlyingContextMapimplementation doesn't supportnullkeys or values.- See Also:
-
containsKey
Convenience method to determine if the current context contains an entry corresponding to thekey.- Parameters:
key- TheContextMap.Keyto lookup.- Returns:
trueif the current context contains an entry corresponding to thekey,falseotherwise.- Throws:
NullPointerException- (optional behavior) ifkeyisnulland the underlyingContextMapimplementation doesn't supportnullkeys.- See Also:
-
containsValue
Convenience method to determine if the current context contains an entry with the specifiedvalue.- Parameters:
value- the value to lookup.- Returns:
trueif this context contains one or more entries with the specifiedvalue,falseotherwise.- Throws:
NullPointerException- (optional behavior) ifvalueisnulland the underlyingContextMapimplementation doesn't supportnullvalues.- See Also:
-
contains
Convenience method to determine if this context contains an entry matching the passedkeyandvalue.- Type Parameters:
T- The anticipated type of object associated with thekey.- Parameters:
key- TheContextMap.Keyto lookup.value- The value to match.- Returns:
trueif this context contains an entry matching the passedkeyandvalue,falseotherwise.- Throws:
NullPointerException- (optional behavior) ifkeyorvalueisnulland the underlyingContextMapimplementation doesn't supportnullkeys or values.
-
size
public static int size()Convenience method to determine the number of entries in the current context.- Returns:
- the number of entries in the current context.
- See Also:
-
isEmpty
public static boolean isEmpty()Convenience method to determine if there are no entries in the current context.- Returns:
trueif there are no entries in the current context.- See Also:
-
forEach
Convenience method to iterate over the entries contained in the current context.- Parameters:
consumer- Each entry will be passed as key and value arguments to thisBiPredicate. A consumer predicate should returntrueif it wants to keep iterating orfalseto stop iteration at the current entry.- Returns:
nullifconsumeriterated through all entries or theContextMap.Keyat which the iteration stopped.- Throws:
NullPointerException- ifconsumeris null.- See Also:
-
forEachEntry
@Deprecated @Nullable public static ContextMap.Key<?> forEachEntry(BiPredicate<ContextMap.Key<?>, Object> consumer) Deprecated.Convenience method to iterate over the entries contained in the current context.- Parameters:
consumer- Each entry will be passed as key and value arguments to thisBiPredicate. A consumer predicate should returntrueif it wants to keep iterating orfalseto stop iteration at the current entry.- Returns:
nullifconsumeriterated through all entries or theContextMap.Keyat which the iteration stopped.- Throws:
NullPointerException- ifconsumeris null.- See Also:
-
wrapJdkExecutor
Wrap anExecutorto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapExecutor
Wrap anExecutorto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkExecutorService
Wrap anExecutorServiceto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkScheduledExecutorService
public static ScheduledExecutorService wrapJdkScheduledExecutorService(ScheduledExecutorService executor) Wrap aScheduledExecutorServiceto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapRunnable
Wrap aRunnableto ensure it is able to trackAsyncContextcorrectly.- Parameters:
runnable- The runnable to wrap.- Returns:
- The wrapped
Runnable.
-
wrapCallable
Wrap aCallableto ensure it is able to trackAsyncContextcorrectly.- Type Parameters:
V- The type of data returned bycallable.- Parameters:
callable- The callable to wrap.- Returns:
- The wrapped
Callable.
-
wrapConsumer
Wrap aConsumerto ensure it is able to trackAsyncContextcorrectly.- Type Parameters:
T- The type of data consumed byconsumer.- Parameters:
consumer- The consumer to wrap.- Returns:
- The wrapped
Consumer.
-
wrapFunction
Wrap aFunctionto ensure it is able to trackAsyncContextcorrectly.- 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
Wrap aBiFunctionto ensure it is able to trackAsyncContextcorrectly.- 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
Wrap aBiFunctionto ensure it is able to trackAsyncContextcorrectly.- 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. -
isDisabled
public static boolean isDisabled()Determine ifdisable()has been previously called.- Returns:
trueifdisable()has been previously called.
-
forEach(BiPredicate)