Package io.micronaut.core.propagation
Interface PropagatedContext
public interface PropagatedContext
The propagation across different threads using the context which is immutable and can be extended/reduced by different elements.
Each element can be a simple data structure carrying its state across the threads, or it can implement
ThreadPropagatedContextElement
for use-cases when thread-local values needs to be updated.- Since:
- 4.0.0
- Author:
- Denis Stepanov
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Context propagationAutoCloseable
to be used in try-resource block. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull PropagatedContext
empty()
Returns an empty context.static boolean
exists()
Check if there is a context associated.static @NonNull Optional<PropagatedContext>
find()
Returns an optional context.<T extends PropagatedContextElement>
Optional<T>Finds optional element of type.<T extends PropagatedContextElement>
Stream<T>Find all elements of type.static @NonNull PropagatedContext
get()
Returns the current context or throws an exception otherwise.<T extends PropagatedContextElement>
TGets element of type.Gets all elements.static @NonNull PropagatedContext
Returns the current context or an empty one.minus
(@NonNull PropagatedContextElement element) Creates a new context without the provided element.plus
(@NonNull PropagatedContextElement element) Creates a new element with added element.Propagate the context using try-resource block.default <V> V
Propagate the context for the supplier.replace
(@NonNull PropagatedContextElement oldElement, @NonNull PropagatedContextElement newElement) Creates a new context with replaced the provided element.Wrap runnable for this context to be propagated in.Wrap callable for this context to be propagated in.Wrap supplier for this context to be propagated in.wrapCurrent
(@NonNull Runnable runnable) Wrap runnable for this context to be propagated in.wrapCurrent
(@NonNull Callable<V> callable) Wrap callable for this context to be propagated in.wrapCurrent
(@NonNull Supplier<V> supplier) Wrap supplier for this context to be propagated in.
-
Method Details
-
empty
Returns an empty context.- Returns:
- the empty context
-
getOrEmpty
Returns the current context or an empty one.- Returns:
- the current context or an empty one
-
get
Returns the current context or throws an exception otherwise.- Returns:
- the current context
-
find
Returns an optional context.- Returns:
- the current optional context
-
wrapCurrent
Wrap runnable for this context to be propagated in.- Parameters:
runnable
- The runnable- Returns:
- new runnable or existing if the context is missing
-
wrapCurrent
Wrap callable for this context to be propagated in.- Type Parameters:
V
- The callable type- Parameters:
callable
- The callable- Returns:
- new callable or existing if the context is missing
-
wrapCurrent
Wrap supplier for this context to be propagated in.- Type Parameters:
V
- The supplier type- Parameters:
supplier
- The supplier- Returns:
- new supplier or existing if the context is missing
-
exists
static boolean exists()Check if there is a context associated.- Returns:
- true if the context exists
-
plus
Creates a new element with added element.NOTE: The new element needs to be propagated.
- Parameters:
element
- The element element to be added- Returns:
- new element
-
minus
Creates a new context without the provided element.NOTE: The new context needs to be propagated.
- Parameters:
element
- The context element to be removed- Returns:
- new context
-
replace
@NonNull @NonNull PropagatedContext replace(@NonNull @NonNull PropagatedContextElement oldElement, @NonNull @NonNull PropagatedContextElement newElement) Creates a new context with replaced the provided element.NOTE: The new context needs to be propagated.
- Parameters:
oldElement
- The context element to be replacednewElement
- The context element to be replaced with- Returns:
- new context
-
find
Finds optional element of type. In a case of multiple element of the same type the last one will be returned.- Type Parameters:
T
- The element's type- Parameters:
elementType
- The element type- Returns:
- optional element
-
findAll
Find all elements of type. The first element processed by stream will be the last one added.- Type Parameters:
T
- The element's type- Parameters:
elementType
- The element type- Returns:
- stream of elements of type
-
get
Gets element of type.- Type Parameters:
T
- The element's type- Parameters:
elementType
- The element type- Returns:
- an element or exception
-
getAllElements
List<PropagatedContextElement> getAllElements()Gets all elements.- Returns:
- all elements.
-
propagate
Propagate the context using try-resource block.- Returns:
- auto-closeable block to be used in try-resource block.
-
wrap
Wrap runnable for this context to be propagated in.- Parameters:
runnable
- The runnable- Returns:
- new runnable
-
wrap
Wrap callable for this context to be propagated in.- Type Parameters:
V
- The callable return type- Parameters:
callable
- The callable- Returns:
- new callable
-
wrap
Wrap supplier for this context to be propagated in.- Type Parameters:
V
- The supplier return type- Parameters:
supplier
- The supplier- Returns:
- new supplier
-
propagate
Propagate the context for the supplier.- Type Parameters:
V
- The supplier return type- Parameters:
supplier
- The supplier- Returns:
- new supplier
-