Annotation Type ContextServiceDefinition
-
@Repeatable(List.class) @Retention(RUNTIME) @Target(TYPE) public @interface ContextServiceDefinition
Defines a
ContextServiceto be injected intoContextServiceinjection points including any requiredQualifierannotations specified byqualifiers()and registered in JNDI by the container under the JNDI name that is specified in thename()attribute.Application components can refer to this JNDI name in the
lookupattribute of aResourceannotation,@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(), andunchanged()attributes enable the application to configure how thread context is applied to tasks and actions that are contextualized by theContextService. 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
You can also define aContextServiceinstance from being created. IfALL_REMAININGis not present in any of the lists, it is implicitly appended to thecleared()context types.ContextServicewith 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 acontext-serviceandContextServiceDefinitionhave the same name, their attributes are merged to define a singleContextServicedefinition, with each attribute that is specified in thecontext-servicedeployment 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
-
-
Field Summary
Fields Modifier and Type Fields Description static java.lang.StringALL_REMAININGAll available thread context types that are not specified elsewhere.static java.lang.StringAPPLICATIONContext pertaining to the application component or module, including its Jakarta EE namespace (such asjava:comp/env/) and thread context class loader.static java.lang.StringSECURITYContext that controls the credentials that are associated with the thread, including the caller subject and invocation/RunAs subject.static java.lang.StringTRANSACTIONContext that controls the transaction that is associated with the thread.
-
Required Element Summary
Required Elements Modifier and Type Required Element Description java.lang.StringnameJNDI name of theContextServiceinstance being defined.
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.String[]clearedTypes of context to clear whenever a thread runs the contextual task or action.java.lang.String[]propagatedTypes of context to capture from the requesting thread and propagate to a thread that runs the contextual task or action.java.lang.Class<?>[]qualifiersList of requiredqualifier annotations.java.lang.String[]unchangedTypes of context that are left alone when a thread runs the contextual task or action.
-
-
-
Field Detail
-
ALL_REMAINING
static final java.lang.String ALL_REMAINING
All available thread context types that are not specified elsewhere. This includes thread context types from custom
ThreadContextProvidersthat are not specified elsewhere.For example, to define a
ContextServicethat propagatesSECURITYcontext, leavesTRANSACTIONcontext alone, and clears every other context type:@ContextServiceDefinition( name = "java:module/concurrent/SecurityContext", propagated = SECURITY, unchanged = TRANSACTION, cleared = ALL_REMAINING) public class MyServlet extends HttpServlet ...
-
-
-
APPLICATION
static final java.lang.String APPLICATION
Context pertaining to the application component or module, including its Jakarta EE namespace (such as
java:comp/env/) and thread context class loader.A cleared application context means that the thread is not associated with any application component and lacks access to the Jakarta EE namespace and thread context class loader of the application.
-
-
-
TRANSACTION
static final java.lang.String TRANSACTION
Context that controls the transaction that is associated with the thread.
When cleared transaction context is applied to a thread, any global transaction that was previously present there is first suspended such that the contextual task or action can begin and manage, as permitted by the container, its own new
jakarta.transaction.UserTransaction. After the contextual task or action completes, the prior transaction is resumed on the thread. This is equivalent to the execution property,ManagedTask.TRANSACTIONwith a value ofManagedTask.SUSPEND.The execution property,
ManagedTask.TRANSACTION, if specified, takes precedence over the behavior for transaction context that is specified on the resource definition annotations.Jakarta EE providers need not support the propagation of transactions to other threads and can reject resource definition annotations that include transaction as a propagated context.
-
-
Element Detail
-
name
java.lang.String name
JNDI name of the
ContextServiceinstance being defined. The JNDI name must be in a valid Jakarta EE namespace, such as,- java:comp
- java:module
- java:app
- java:global
- Returns:
ContextServiceJNDI name.
-
-
-
qualifiers
java.lang.Class<?>[] qualifiers
List of required
qualifier annotations.A
ContextServiceinjection point with these qualifier annotations injects a bean that is produced by thisContextServiceDefinition.The default value is an empty list, indicating that this
ContextServiceDefinitiondoes not automatically produce bean instances for any injection points.When the qualifiers list is non-empty, the container creates a
ContextServiceinstance and registers anApplicationScopedbean for it with the specified required qualifiers and required type ofContextService. The life cycle of the bean aligns with the life cycle of the application and the bean is not accessible from outside of the application. Applications must not configure ajava:globalnameif also configuring a non-empty list of qualifiers.Applications can define their own
ProducersforContextServiceinjection points as long as the qualifier annotations on the producer do not conflict with the non-emptyqualifiers()list of aContextServiceDefinition.- Returns:
- list of qualifiers.
- Since:
- 3.1
- Default:
- {}
-
-
-
cleared
java.lang.String[] cleared
Types of context to clear whenever a thread runs the contextual task or action. The thread's previous context is restored afterward.
Constants are provided on this class for the context types that are defined by the Jakarta EE Concurrency specification.
- Returns:
- context types to clear.
- Default:
- {"Transaction"}
-
-
-
propagated
java.lang.String[] propagated
Types of context to capture from the requesting thread and propagate to a thread that runs the contextual task or action. The captured context is re-established when threads run the contextual task or action, with the respective thread's previous context being restored afterward.
Constants are provided on this class for the context types that are defined by the Jakarta EE Concurrency specification.
- Returns:
- context types to capture and propagate.
- Default:
- {"Remaining"}
-
-
-
unchanged
java.lang.String[] unchanged
Types of context that are left alone when a thread runs the contextual task or action.
For example, with
unchanged = TRANSACTIONif a transaction is started after a function is contextualized, but before the function is run on the same thread, the transaction will be active in the contextual function:Consumer<String, Integer> updateDB = contextService.contextualConsumer(fn); // later, on another thread tx.begin(); updateDB.accept("java:comp/env/jdbc/ds1"); //...additional transactional work tx.commit();Constants are provided on this class for the context types that are defined by the Jakarta EE Concurrency specification.
- Returns:
- context types to leave unchanged.
- Default:
- {}
-
-