Annotation Interface ContextServiceDefinition


@Repeatable(ContextServiceDefinition.List.class) @Retention(RUNTIME) @Target(TYPE) public @interface ContextServiceDefinition

Defines a ContextService to be injected into ContextService injection points including any required Qualifier annotations specified by qualifiers() and registered in JNDI by the container under the JNDI name that is specified in the name() attribute.

Application components can refer to this JNDI name in the lookup attribute of a Resource annotation,

@ContextServiceDefinition(
     name = "java:app/concurrent/MyContext",
     qualifiers = MyQualifier.class,
     propagated = APPLICATION,
     unchanged = TRANSACTION,
     cleared = ALL_REMAINING)
 public class MyServlet extends HttpServlet {
     @Inject
     @MyQualifier
     ConetxtService appContextSvc1;

     @Resource(lookup = "java:app/concurrent/MyContext",
               name = "java:app/concurrent/env/MyContextRef")
     ContextService appContextSvc2;
     ...

 @Qualifier
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE })
 public @interface MyQualifier {}
 

Resource environment references in a deployment descriptor can similarly specify the lookup-name,

 <resource-env-ref>
    <resource-env-ref-name>java:app/env/concurrent/MyContextRef</resource-env-ref-name>
    <resource-env-ref-type>jakarta.enterprise.concurrent.ContextService</resource-env-ref-type>
    <lookup-name>java:app/concurrent/MyContext</lookup-name>
 </resource-env-ref>
 

The cleared(), propagated(), and unchanged() attributes enable the application to configure how thread context is applied to tasks and actions that are contextualized by the ContextService. Constants are provided on this class for context types that are defined by the Jakarta EE Concurrency specification. In addition to those constants, a Jakarta EE product provider may choose to accept additional vendor-specific context types. Usage of vendor-specific types will make applications non-portable.

Overlap of the same context type across multiple lists is an error and prevents the ContextService instance from being created. If ALL_REMAINING is not present in any of the lists, it is implicitly appended to the cleared() context types.

You can also define a ContextService with the <context-service> deployment descriptor element. For example,
 <context-service>
    <name>java:app/concurrent/MyContext</name>
    <cleared>Security</cleared>
    <cleared>Transaction</cleared>
    <propagated>Application</propagated>
    <unchanged>Remaining</unchanged>
 </context-service>
 
If a context-service and ContextServiceDefinition have the same name, their attributes are merged to define a single ContextService definition, with each attribute that is specified in the context-service deployment descriptor entry taking precedence over the corresponding attribute of the annotation. If any qualifier elements are specified, the set of qualifier elements replaces the qualifiers attribute of the annotation.
Since:
3.0