Reputation: 908
I have seen the following code in a Java EE application deployed on Payara:
public class MyClass {
@Resource
ManagedScheduledExecutorService scheduler;
<...>
}
This was a CDI enabled application if that makes a difference.
In Payara there exists a resource for the ManagedScheduledExecutorService whose JNDI name is concurrent/__defaultManagedScheduledExecutorService
and has a logical JNDI name of java:comp/DefaultManagedScheduledExecutorService
.
The JNDI name of the resource is created from the class name and the field. This will not match the names above.
My understanding of resource injection so far was, that you use the lookup attribute to reference an existing JNDI entry purely by name. But as you can see there is no attribute used and it still works.
Why? Is there some fallback mechanism specific to the container or does some Java spec define this behavior?
Upvotes: 0
Views: 2531
Reputation: 19435
Indeed "some Java spec define this behavior" applies.
In fact, §EE.5.21 Default Concurrency Utilities Objects in the Java EE 7/8 specifications provides this very definition:
The Java EE Product Provider must make the default Concurrency Utilities for Java EE objects accessible to the application under the following JNDI names:
java:comp/DefaultManagedExecutorService
for the preconfigured managed executor servicejava:comp/DefaultManagedScheduledExecutorService
for the preconfigured managed scheduled executor servicejava:comp/DefaultManagedThreadFactory
for the preconfigured managed thread factoryjava:comp/DefaultContextService
for the preconfigured context serviceThe Application Component Provider or Deployer may explicitly bind a resource reference to a default Concurrency Utilities object using the lookup element of the Resource annotation or the lookup-name element of the resource-ref deployment descriptor element. For example,
@Resource(name="myManagedExecutorService, lookup="java:comp/DefaultManagedExecutorService") ManagedExecutorService myManagedExecutorService;`
In the absence of such a binding, the mapping of the reference will default to the product's default managed executor service.
Upvotes: 1