Annotation Type ThreadContextConfig


  • @Retention(RUNTIME)
    @Target({FIELD,METHOD,TYPE,PARAMETER})
    public @interface ThreadContextConfig

    Annotates a CDI injection point for a ThreadContext such that the container creates a new instance, which is identified within an application by its unique name. The unique name is generated as the fully qualified class name (with each component delimited by .) and the injection point's field name or method name and parameter position, all delimited by /, unless annotated with the NamedInstance qualifier, in which case the unique name is specified by the value attribute of that qualifier.

    For example, the following injection points share a single ThreadContext instance,

      @Inject @NamedInstance("tc1") @ThreadContextConfig(propagated = { ThreadContext.CDI, ThreadContext.APPLICATION })
     ThreadContext threadContext1;
    
     @Inject
     void setTask(@NamedInstance("tc1") ThreadContext contextPropagator) {
         task = contextPropagator.contextualTask(...);
     }
    
     @Inject
     void setContextSnapshot(@NamedInstance("tc1") ThreadContext contextPropagator) {
         contextSnapshot = contextPropagator.currentContextExecutor();
     }
     
     

    Alternatively, the following injection points each represent a distinct ThreadContext instance,

      @Inject @ThreadContextConfig(propagated = { ThreadContext.SECURITY, ThreadContext.APPLICATION })
     ThreadContext tc2;
    
     @Inject @ThreadContextConfig(cleared = ThreadContext.SECURITY, unchanged = ThreadContext.TRANSACTION)
     ThreadContext tc3;
     
     

    A ThreadContext will fail to inject, raising DefinitionException on application startup, if multiple injection points are annotated to create instances with the same name.

    A ThreadContext must fail to inject, raising DeploymentException on application startup, if more than one provider provides the same thread context type.

    Author:
    Matej Novotny
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      String[] cleared
      Defines the set of thread context types to clear from the thread where the action or task executes.
      String[] propagated
      Defines the set of thread context types to capture from the thread that contextualizes an action or task.
      String[] unchanged
      Defines a set of thread context types that are essentially ignored, in that they are neither captured nor are they propagated or cleared from thread(s) that execute the action or task.
    • Element Detail

      • cleared

        String[] cleared

        Defines the set of thread context types to clear from the thread where the action or task executes. The previous context is resumed on the thread after the action or task ends.

        By default, no context is cleared/suspended from execution thread.

        ThreadContext.ALL_REMAINING is automatically appended to the set of cleared context if neither the propagated() set nor the unchanged() set include ThreadContext.ALL_REMAINING.

        Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

        A ThreadContext must fail to inject, raising DefinitionException on application startup, if a context type specified within this set is unavailable or if the propagated() and/or unchanged() set includes one or more of the same types as this set.

        Returns:
        an array of strings of thread context types to clear.
        Default:
        {}
      • propagated

        String[] propagated

        Defines the set of thread context types to capture from the thread that contextualizes an action or task. This context is later re-established on the thread(s) where the action or task executes.

        The default set of propagated thread context types is ThreadContext.ALL_REMAINING, which includes all available thread context types that support capture and propagation to other threads, except for those that are explicitly cleared.

        Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

        Thread context types which are not otherwise included in this set or in the unchanged() set are cleared from the thread of execution for the duration of the action or task.

        A ThreadContext must fail to inject, raising DefinitionException on application startup, if a context type specified within this set is unavailable or if the cleared() and/or unchanged() set includes one or more of the same types as this set.

        Returns:
        an array of strings of thread context types to propagate.
        Default:
        {"Remaining"}
      • unchanged

        String[] unchanged

        Defines a set of thread context types that are essentially ignored, in that they are neither captured nor are they propagated or cleared from thread(s) that execute the action or task.

        Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

        The configuration unchanged context is provided for advanced patterns where it is desirable to leave certain context types on the executing thread.

        For example, to run as the current application, but under the transaction of the thread where the task executes:

          @Inject @ThreadContextConfig(unchanged = ThreadContext.TRANSACTION,
                                      propagated = ThreadContext.APPLICATION,
                                      cleared = ThreadContext.ALL_REMAINING)
         ThreadContext threadContext;
         ...
         task = threadContext.contextualRunnable(new MyTransactionalTask());
         ...
         // on another thread,
         tx.begin();
         ...
         task.run(); // runs under the transaction due to 'unchanged'
         tx.commit();
         
         

        A ThreadContext must fail to inject, raising DefinitionException on application startup, if a context type specified within this set is unavailable or if the cleared() and/or propagated() set includes one or more of the same types as this set.

        Returns:
        an array of strings of thread context types to be ignored.
        Default:
        {}