简体   繁体   中英

JNDI Lookup with @Resource without name or lookup attribute

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?

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 service
  • java:comp/DefaultManagedScheduledExecutorService for the preconfigured managed scheduled executor service
  • java:comp/DefaultManagedThreadFactory for the preconfigured managed thread factory
  • java:comp/DefaultContextService for the preconfigured context service

The 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM