Package io.servicetalk.concurrent.api
Class AsyncContext
- java.lang.Object
-
- io.servicetalk.concurrent.api.AsyncContext
-
public final class AsyncContext extends java.lang.ObjectPresents 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 voidclear()Convenience method to clear all the key/value pairs from the current context.static booleancontainsKey(AsyncContextMap.Key<?> key)Convenience method to determine if the current context contains a key/value entry corresponding tokey.static AsyncContextMapcurrent()Get the currentAsyncContextMap.static voiddisable()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> Tget(AsyncContextMap.Key<T> key)Convenience method to get the value associated withkeyfrom the current context.static booleanisDisabled()Determine ifdisable()has been previously called.static booleanisEmpty()Convenience method to determine if there are no key/value pairs in the current context.static <T> voidput(AsyncContextMap.Key<T> key, T value)Convenience method for adding a value to the current context.static voidputAll(java.util.Map<AsyncContextMap.Key<?>,java.lang.Object> map)Convenience method for to put all the key/value pairs into the current context.static voidremove(AsyncContextMap.Key<?> key)Convenience method to remove a key/value pair from the current context.static voidremoveAll(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 aBiFunctionto ensure it is able to trackAsyncContextcorrectly.static <T,U,V>
java.util.function.BiFunction<T,U,V>wrapBiFunction(java.util.function.BiFunction<T,U,V> func)Wrap aBiFunctionto ensure it is able to trackAsyncContextcorrectly.static <T> java.util.function.Consumer<T>wrapConsumer(java.util.function.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>
java.util.function.Function<T,U>wrapFunction(java.util.function.Function<T,U> func)Wrap aFunctionto ensure it is able to trackAsyncContextcorrectly.static java.util.concurrent.ExecutorwrapJdkExecutor(java.util.concurrent.Executor executor)Wrap anExecutorto ensure it is able to trackAsyncContextcorrectly.static java.util.concurrent.ExecutorServicewrapJdkExecutorService(java.util.concurrent.ExecutorService executor)Wrap anExecutorServiceto ensure it is able to trackAsyncContextcorrectly.static java.util.concurrent.ScheduledExecutorServicewrapJdkScheduledExecutorService(java.util.concurrent.ScheduledExecutorService executor)Wrap aScheduledExecutorServiceto ensure it is able to trackAsyncContextcorrectly.static java.lang.RunnablewrapRunnable(java.lang.Runnable runnable)Wrap aRunnableto ensure it is able to trackAsyncContextcorrectly.
-
-
-
Method Detail
-
current
public static AsyncContextMap current()
Get the currentAsyncContextMap.- Returns:
- the current
AsyncContextMap
-
put
public static <T> void put(AsyncContextMap.Key<T> key, @Nullable 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.- Throws:
java.lang.NullPointerException- ifkeyorvalueisnulland the underlyingAsyncContextMapimplementation doesn't supportnullkeys or values.java.lang.UnsupportedOperationException- if this method is not supported by the underlyingAsyncContextMapimplementation.- 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.- Throws:
java.util.ConcurrentModificationException- done on a best effort basis ifentriesis detected to be modified while attempting to put all entries.java.lang.NullPointerException- ifkeyorvalueisnulland the underlyingAsyncContextMapimplementation doesn't supportnullkeys or values.java.lang.UnsupportedOperationException- if this method is not supported by the underlyingAsyncContextMapimplementation.- 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.- Throws:
java.lang.UnsupportedOperationException- if this method is not supported by the underlyingAsyncContextMapimplementation.- 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- AIterablewhich contains all the keys to remove.- Throws:
java.lang.UnsupportedOperationException- if this method is not supported by the underlyingAsyncContextMapimplementation.- See Also:
AsyncContextMap.removeAll(Iterable)
-
clear
public static void clear()
Convenience method to clear all the key/value pairs from the current context.- Throws:
java.lang.UnsupportedOperationException- if this method is not supported by the underlyingAsyncContextMapimplementation.- See Also:
AsyncContextMap.clear()
-
get
@Nullable public static <T> T get(AsyncContextMap.Key<T> key)
Convenience method to get the value associated withkeyfrom 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, ornullif no value is associated.nullcan also indicate the value associated withkeyisnull(ifnullvalues are supported by the underlyingAsyncContextMapimplementation). - Throws:
java.lang.NullPointerException- (optional behavior) ifkeyisnulland the underlyingAsyncContextMapimplementation doesn't supportnullkeys or values.- 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:
trueif the current context contains a key/value entry corresponding tokey.falseotherwise.- Throws:
java.lang.NullPointerException- (optional behavior) ifkeyisnulland the underlyingAsyncContextMapimplementation doesn't supportnullkeys or values.- 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:
trueif 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. Returnstrueif the consumer wants to keep iterating orfalseto stop iteration at the current key/value pair.- Returns:
nullifconsumeriterated through all key/value pairs or theAsyncContextMap.Keyat which the iteration stopped.- Throws:
java.lang.NullPointerException- ifconsumeris null.- See Also:
AsyncContextMap.forEach(BiPredicate)
-
wrapJdkExecutor
public static java.util.concurrent.Executor wrapJdkExecutor(java.util.concurrent.Executor executor)
Wrap anExecutorto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapExecutor
public static Executor wrapExecutor(Executor executor)
Wrap anExecutorto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkExecutorService
public static java.util.concurrent.ExecutorService wrapJdkExecutorService(java.util.concurrent.ExecutorService executor)
Wrap anExecutorServiceto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapJdkScheduledExecutorService
public static java.util.concurrent.ScheduledExecutorService wrapJdkScheduledExecutorService(java.util.concurrent.ScheduledExecutorService executor)
Wrap aScheduledExecutorServiceto ensure it is able to trackAsyncContextcorrectly.- Parameters:
executor- The executor to wrap.- Returns:
- The wrapped executor.
-
wrapRunnable
public static java.lang.Runnable wrapRunnable(java.lang.Runnable runnable)
Wrap aRunnableto ensure it is able to trackAsyncContextcorrectly.- 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 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
public static <T,U> java.util.function.Function<T,U> wrapFunction(java.util.function.Function<T,U> func)
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
public static <T,U> java.util.function.BiConsumer<T,U> wrapBiConsume(java.util.function.BiConsumer<T,U> consumer)
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
public static <T,U,V> java.util.function.BiFunction<T,U,V> wrapBiFunction(java.util.function.BiFunction<T,U,V> func)
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.
-
-