Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is java:comp/env scope rules?

Tags:

java

I understand that java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component (a webapp, or an EJB), and also I know that each EJB has its own component environment ,also there is java:global and java:app and a java:module depending on that I have some questions

  1. when I use Context envContext = (Context)initContext.lookup("java:comp/env"); to get initContext what Context exactly I get (global,app,module,webApp or EJB Context)?
  2. Is there is certain rules applied for searching different scopes?
  3. Let's say I have web application with many EJBs,does this means that I have many Initial Contexts (one for webApp and one for each EJB) or all of these resources are somehow collected under one context java:comp/env?

Many Thanks.

like image 344
Melad Basilius Avatar asked May 13 '15 08:05

Melad Basilius


2 Answers

  1. when I use Context envContext = (Context)initContext.lookup("java:comp/env"); to get initContext what Context exactly I get (global,app,module,webApp or EJB Context)?

Quoting from TomEE documentation http://tomee.apache.org/lookup-of-other-ejbs-example.html

In a webapp, the java:comp/env namespace is shared by all servlets. This is essentially equivalent to the java:module namespace in Java EE 6. Understand there is a conflict in definition here and that for EJBs, java:comp is scoped at the component (the EJB itself) not the module as with webapps.

  1. Is there is certain rules applied for searching different scopes?

Quoting from JavaEE 6 Tutorial http://docs.oracle.com/cd/E19798-01/821-1841/girgn/index.html

The java:global JNDI namespace is the portable way of finding remote enterprise beans using JNDI lookups. The java:module namespace is used to look up local enterprise beans within the same module. The java:app namespace is used to look up local enterprise beans packaged within the same application. That is, the enterprise bean is packaged within an EAR file containing multiple Java EE modules.

  1. Let's say I have web application with many EJBs,does this means that I have many Initial Contexts (one for webApp and one for each EJB) or all of these resources are somehow collected under one context java:comp/env?

Based on above links, you will have not have many contexts.

like image 50
CuriousMind Avatar answered Oct 03 '22 22:10

CuriousMind


Regarding question 3 on the scope of java:comp/env in presence of EJBs.

Traditionally (before JEE 6) java:comp/env was module level for war modules and EJB level for EJBs in jar modules. In that model, one has to define environment entries (via resource-ref and env-entry in ejb-jar.xml, or for resource-refs since JEE5 one could use @Resource at the class level) for every single EJB.

<ejb-jar>
  <enterprise-beans>
    <session>
      <ejb-name>B1</ejb-name>
      <ejb-class>p1.B1</ejb-class> <!-- ejb-class should be skipped if bean is already defined via annotation -->
      <env-entry>
        <env-entry-name>entry1</env-entry-name>        
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>value1</env-entry-value
      </env-entry>
    </session>
  </enterprise-beans>
</ejb-jar>

Since JEE 6, one can deploy EJBs as part of war module. Whether war module is deployed directly or is part or ear module, it has a single java:comp/env namespace shared between all servlets, EJBs and any other code within that module. One can define environment entries in web.xml:

<web-app>
  <env-entry>
    <env-entry-name>entry1</env-entry-name>        
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>value1</env-entry-value
  </env-entry>
</web-app>

In this war based model, one could still have ejb-jar.xml to configure other aspects of EJBs, but env-entry for a given bean in ejb-jar.xml would end up injecting the environment value for all other beans in the war.

So, I would always use war archives for everything (possibly packaged in ear).

like image 41
Ivan Avatar answered Oct 03 '22 20:10

Ivan