Interface ThreadContextProvider
-
public interface ThreadContextProviderThird party providers of thread context implement this interface to participate in thread context capture and propagation.Application code must never access the classes within this
spipackage. Instead, application code uses the various interfaces that are defined by the Jakarta Concurrency specification, such asManagedExecutorServiceandContextService.The
ThreadContextProviderimplementation and related classes are packaged within the third party provider's JAR file. The implementation is made discoverable via theServiceLoadermechanism. The JAR file that packages it must include a file with the following name and location,META-INF/services/jakarta.enterprise.concurrent.spi.ThreadContextProviderThe content of the aforementioned file must be one or more lines, each specifying the fully qualified name of a
ThreadContextProviderimplementation that is provided within the JAR file.The Jakarta EE Product Provider must use the
ServiceLoaderto identify all available implementations ofThreadContextProviderthat can participate in thread context capture and propagation and must invoke them either to capture current thread context or establish default thread context per the configuration of theContextServiceDefinition, or vendor-specific configuration, and execution properties such asManagedTask.TRANSACTIONthat override context propagation configuration.- Since:
- 3.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description ThreadContextSnapshotclearedContext(Map<String,String> props)Returns empty/cleared context of the provided type.ThreadContextSnapshotcurrentContext(Map<String,String> props)Captures from the current thread a snapshot of the provided thread context type.StringgetThreadContextType()Returns a human readable identifier for the type of thread context that is captured by thisThreadContextProviderimplementation.
-
-
-
Method Detail
-
currentContext
ThreadContextSnapshot currentContext(Map<String,String> props)
Captures from the current thread a snapshot of the provided thread context type.- Parameters:
props- execution properties, which are optionally provided by some types of tasks and contextual proxies. Thread context providers that do not supply or use execution properties can ignore this parameter.- Returns:
- immutable snapshot of the provided type of context, captured from the current thread.
-
clearedContext
ThreadContextSnapshot clearedContext(Map<String,String> props)
Returns empty/cleared context of the provided type. This context is not captured from the current thread, but instead represents the behavior that you get for this context type when no particular context has been applied to the thread.This is used in cases where the provided type of thread context should not be propagated from the requesting thread or inherited from the thread of execution, in which case it is necessary to establish an empty/cleared context in its place, so that an action does not unintentionally inherit context of the thread that happens to run it.
For example, a security context provider's empty/cleared context ensures there is no authenticated user on the thread. A transaction context provider's empty/cleared context ensures that any active transaction is suspended. And so forth.
- Parameters:
props- execution properties, which are optionally provided by some types of tasks and contextual proxies. Thread context providers that do not supply or use execution properties can ignore this parameter.- Returns:
- immutable empty/default context of the provided type.
-
getThreadContextType
String getThreadContextType()
Returns a human readable identifier for the type of thread context that is captured by thisThreadContextProviderimplementation.To ensure portability of applications, this is typically be a keyword that is defined by the same specification that defines the thread context type.
ContextServiceDefinitiondefines identifiers for built-in thread context types, includingApplication,Security, andTransaction, as well as theRemainingidentifer which covers all remaining context. These identifiers must not be returned from this method.Applications use a combination of built-in identifiers and those that are defined by other specifications and third-party context types when configuring a
ContextServiceDefinitionto capture and propagate only specific types of thread context.For example:
@ContextServiceDefinition(name = "java:module/concurrent/MyCustomContext", propagated = MyCustomContextProvider.CONTEXT_NAME, cleared = { ContextServiceDefinition.SECURITY, ContextServiceDefinition.TRANSACTION }, unchanged = ContextServiceDefinition.ALL_REMAINING)It is an error for multiple thread context providers of an identical type to be simultaneously available.
- Returns:
- identifier for the provided type of thread context.
-
-