Session EJB and Servlet as Different EAR Modules, JNDI Lookup: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 13: Line 13:
=Business Interface Type Access=
=Business Interface Type Access=


The EJB code and the calling component code are part of the same deployment unit, so the calling component has implicit access to the business interface class. No special configuration is needed here.  
The EJB code and the calling servlet component are deployed as part of two different EAR modules.  
 
 
 
 
 


=EJB Lookup=
=EJB Lookup=


The EJB reference is injected by the container upon encountering the @EJB annotation, no JNDI lookups are involved:
The EJB reference is looked up in JNDI using the portable JNDI EJB naming scheme.
 
In this case, the EJB is named SimpleStatelessBean, it is deployed as part of the wrapper-servlet.war, and implements only a single business interface, so the simplest possible JNDI name is "java:module/SimpleStatelessBean". "java:app/wrapper-servlet/SimpleStatelessBean" or "java:global/wrapper-servlet/SimpleStatelessBean" would also work.
 
For more details on the JNDI EJB naming scheme, see:
 
{{Internal|EJB_Concepts#EJB_and_JNDI|EJB Concepts - EJB and JNDI}}
 
The code that does the JNDI lookup is similar to:


<pre>
<pre>
SimpleStateless bean = null;
...
...
@EJB
 
private SimpleStateless bean;
try {
...
 
  InitialContext  ic = new InitialContext();
  SimpleStateless bean = (SimpleStateless)ic.lookup("java:global/wrapper-servlet/SimpleStatelessBean");
}
catch(Exception e) {
    ...
}
</pre>
</pre>



Revision as of 18:13, 24 March 2017

Internal

Overview

This example describes the look up of an EJB and subsequent invocation, when the EJB and the calling component (a servlet, in this case) are deployed in different modules, but part of the same EAR. The lookup is done via JNDI.

GitHub Example

https://github.com/NovaOrdis/playground/tree/master/jee/ejb/stateless-and-servlet-within-ear-jndi

Business Interface Type Access

The EJB code and the calling servlet component are deployed as part of two different EAR modules.




EJB Lookup

The EJB reference is looked up in JNDI using the portable JNDI EJB naming scheme.

In this case, the EJB is named SimpleStatelessBean, it is deployed as part of the wrapper-servlet.war, and implements only a single business interface, so the simplest possible JNDI name is "java:module/SimpleStatelessBean". "java:app/wrapper-servlet/SimpleStatelessBean" or "java:global/wrapper-servlet/SimpleStatelessBean" would also work.

For more details on the JNDI EJB naming scheme, see:

EJB Concepts - EJB and JNDI

The code that does the JNDI lookup is similar to:

SimpleStateless bean = null;

...

try {

   InitialContext  ic = new InitialContext();
   SimpleStateless bean = (SimpleStateless)ic.lookup("java:global/wrapper-servlet/SimpleStatelessBean");
}
catch(Exception e) {
    ...
}

EJB Invocation

Once the EJB reference is obtained from JNDI, business interface methods can be called on its reference:

...
bean.methodOne("something from servlet");