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 void
clear()
Convenience method to clear all the entries from the current context.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 thiskey
or is mapped tonull
.static <T> boolean
contains
(ContextMap.Key<T> key, T value) Convenience method to determine if this context contains an entry matching the passedkey
andvalue
.static boolean
containsKey
(ContextMap.Key<?> key) Convenience method to determine if the current context contains an entry corresponding to thekey
.static boolean
containsValue
(Object value) Convenience method to determine if the current context contains an entry with the specifiedvalue
.static ContextMap
context()
Get the currentContextMap
.static void
disable()
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> T
get
(ContextMap.Key<T> key) Convenience method to get the value associated with thekey
from the current context.static <T> T
getOrDefault
(ContextMap.Key<T> key, T defaultValue) Convenience method to get the value associated withkey
from the current context, ordefaultValue
if no value is associated.static boolean
Determine ifdisable()
has been previously called.static boolean
isEmpty()
Convenience method to determine if there are no entries in the current context.static <T> T
put
(ContextMap.Key<T> key, T value) Convenience method to put a new entry to the current context.static void
putAll
(ContextMap map) Convenience method to put all the entries into the current context from anotherContextMap
.static void
putAll
(Map<ContextMap.Key<?>, Object> map) Convenience method to put all the entries into the current context.static void
putAllFromMap
(Map<ContextMap.Key<?>, Object> map) Deprecated.UseputAll(Map)
static <T> T
putIfAbsent
(ContextMap.Key<T> key, T value) Convenience method to put a new entry to the current context if this map does not already contain thiskey
or is mapped tonull
.static <T> T
remove
(ContextMap.Key<T> key) Convenience method to remove an entry from the current context.static boolean
removeAll
(Iterable<ContextMap.Key<?>> keys) Convenience method to remove all entries from the current context associated with the keys from the passedIterable
.static boolean
removeAllEntries
(Iterable<ContextMap.Key<?>> keys) Deprecated.static int
size()
Convenience method to determine the number of entries in the current context.static <T,
U> BiConsumer<T, U> wrapBiConsume
(BiConsumer<T, U> consumer) Wrap aBiFunction
to ensure it is able to trackAsyncContext
correctly.static <T,
U, V> BiFunction<T, U, V> wrapBiFunction
(BiFunction<T, U, V> func) Wrap aBiFunction
to ensure it is able to trackAsyncContext
correctly.static <V> Callable<V>
wrapCallable
(Callable<V> callable) Wrap aCallable
to ensure it is able to trackAsyncContext
correctly.static <T> Consumer<T>
wrapConsumer
(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> Function<T, U> wrapFunction
(Function<T, U> func) Wrap aFunction
to ensure it is able to trackAsyncContext
correctly.static Executor
wrapJdkExecutor
(Executor executor) Wrap anExecutor
to ensure it is able to trackAsyncContext
correctly.static ExecutorService
wrapJdkExecutorService
(ExecutorService executor) Wrap anExecutorService
to ensure it is able to trackAsyncContext
correctly.static ScheduledExecutorService
Wrap aScheduledExecutorService
to ensure it is able to trackAsyncContext
correctly.static Runnable
wrapRunnable
(Runnable runnable) Wrap aRunnable
to ensure it is able to trackAsyncContext
correctly.
-
Method Details
-
context
Get the currentContextMap
.- Returns:
- the current
ContextMap
-
put
Convenience method to put a new entry to the current context.- Type Parameters:
T
- The type of object associated withkey
.- Parameters:
key
- TheContextMap.Key
used to index thevalue
.value
- the value to put.- Returns:
- The previous value associated with the
key
, ornull
if there was none.null
can also indicate the value associated withkey
isnull
(ifnull
values are supported by the implementation). - Throws:
NullPointerException
- (optional behavior) ifkey
orvalue
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- See Also:
-
putIfAbsent
Convenience method to put a new entry to the current context if this map does not already contain thiskey
or is mapped tonull
.- Type Parameters:
T
- The type of object associated withkey
.- Parameters:
key
- TheContextMap.Key
used to index thevalue
.value
- the value to put.- Returns:
- The previous value associated with the
key
, ornull
if there was none.null
can also indicate the value associated withkey
isnull
(ifnull
values are supported by the implementation). - Throws:
NullPointerException
- (optional behavior) ifkey
orvalue
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- 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 thiskey
or is mapped tonull
.- Type Parameters:
T
- The type of object associated withkey
.- Parameters:
key
- TheContextMap.Key
used 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
, ornull
if there was none.null
can also indicate the value associated withkey
isnull
(ifnull
values are supported by the implementation). - Throws:
NullPointerException
- (optional behavior) ifkey
or computedvalue
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- 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 passedmap
is detected to be modified while attempting to put all entries.NullPointerException
- (optional behavior) if any of themap
entries has anull
key
orvalue
and the underlyingContextMap
implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- 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 passedmap
is detected to be modified while attempting to put all entries.NullPointerException
- (optional behavior) if any of themap
entries has anull
key
orvalue
and the underlyingContextMap
implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- 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 passedmap
is detected to be modified while attempting to put all entries.NullPointerException
- (optional behavior) if any of themap
entries has anull
key
orvalue
and the underlyingContextMap
implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- 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.Key
which identifies an entry for removal.- Returns:
- the previous value associated with
key
, ornull
if there was none. Anull
value may also indicate there was a previous value which wasnull
. - Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- See Also:
-
removeAll
Convenience method to remove all entries from the current context associated with the keys from the passedIterable
.- Parameters:
keys
- TheContextMap.Key
s that identify entries for removal.- Returns:
true
if this map has changed as a result of this operation.- Throws:
NullPointerException
- (optional behavior) if any of thekeys
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- See Also:
-
removeAllEntries
Deprecated.Convenience method to remove all entries from the current context associated with the keys from the passedIterable
.- Parameters:
keys
- TheContextMap.Key
s that identify entries for removal.- Returns:
true
if this map has changed as a result of this operation.- Throws:
NullPointerException
- (optional behavior) if any of thekeys
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys.UnsupportedOperationException
- if this method is not supported by the underlyingContextMap
implementation.- 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 underlyingContextMap
implementation.- See Also:
-
get
Convenience method to get the value associated with thekey
from the current context.- Type Parameters:
T
- The anticipated type of object associated withkey
.- Parameters:
key
- TheContextMap.Key
to lookup.- Returns:
- the value associated with the
key
, ornull
if no value is associated.null
can also indicate the value associated withkey
isnull
(ifnull
values are supported by the underlyingContextMap
implementation). - Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys or values.- See Also:
-
getOrDefault
Convenience method to get the value associated withkey
from the current context, ordefaultValue
if no value is associated.- Type Parameters:
T
- The anticipated type of object associated withkey
.- Parameters:
key
- TheContextMap.Key
to lookup.defaultValue
- The value to return if no value is associated with thekey
.- Returns:
- The value associated with the
key
(can returnnull
ifnull
values are supported by the underlyingContextMap
implementation), ordefaultValue
if no value is associated. - Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys or values.- See Also:
-
containsKey
Convenience method to determine if the current context contains an entry corresponding to thekey
.- Parameters:
key
- TheContextMap.Key
to lookup.- Returns:
true
if the current context contains an entry corresponding to thekey
,false
otherwise.- Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys.- See Also:
-
containsValue
Convenience method to determine if the current context contains an entry with the specifiedvalue
.- Parameters:
value
- the value to lookup.- Returns:
true
if this context contains one or more entries with the specifiedvalue
,false
otherwise.- Throws:
NullPointerException
- (optional behavior) ifvalue
isnull
and the underlyingContextMap
implementation doesn't supportnull
values.- See Also:
-
contains
Convenience method to determine if this context contains an entry matching the passedkey
andvalue
.- Type Parameters:
T
- The anticipated type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
to lookup.value
- The value to match.- Returns:
true
if this context contains an entry matching the passedkey
andvalue
,false
otherwise.- Throws:
NullPointerException
- (optional behavior) ifkey
orvalue
isnull
and the underlyingContextMap
implementation doesn't supportnull
keys 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:
true
if 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 returntrue
if it wants to keep iterating orfalse
to stop iteration at the current entry.- Returns:
null
ifconsumer
iterated through all entries or theContextMap.Key
at which the iteration stopped.- Throws:
NullPointerException
- ifconsumer
is 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 returntrue
if it wants to keep iterating orfalse
to stop iteration at the current entry.- Returns:
null
ifconsumer
iterated through all entries or theContextMap.Key
at which the iteration stopped.- Throws:
NullPointerException
- ifconsumer
is null.- See Also:
-
wrapJdkExecutor
Wrap anExecutor
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapExecutor
Wrap anExecutor
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkExecutorService
Wrap anExecutorService
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkScheduledExecutorService
public static ScheduledExecutorService wrapJdkScheduledExecutorService(ScheduledExecutorService executor) Wrap aScheduledExecutorService
to ensure it is able to trackAsyncContext
correctly.- Parameters:
executor
- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapRunnable
Wrap aRunnable
to ensure it is able to trackAsyncContext
correctly.- Parameters:
runnable
- The runnable to wrap.- Returns:
- The wrapped
Runnable
.
-
wrapCallable
Wrap aCallable
to ensure it is able to trackAsyncContext
correctly.- Type Parameters:
V
- The type of data returned bycallable
.- Parameters:
callable
- The callable to wrap.- Returns:
- The wrapped
Callable
.
-
wrapConsumer
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
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
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
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. -
isDisabled
public static boolean isDisabled()Determine ifdisable()
has been previously called.- Returns:
true
ifdisable()
has been previously called.
-
forEach(BiPredicate)