Session EJB and Servlet as Different EAR Modules, JNDI Lookup: Difference between revisions
(2 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
=Business Interface Type Access= | =Business Interface Type Access= | ||
The EJB code and the calling servlet component are deployed as part of two different EAR modules, so visibility of types amongst those two modules is subject to EAR module isolation configuration in the application server. In EAP 7, the modules are NOT isolated by default, so they have type visibility into each other. | The EJB code and the calling servlet component are deployed as part of two different EAR modules, so visibility of types amongst those two modules is subject to EAR module isolation configuration in the application server. In EAP 7, the modules are NOT isolated by default, so they have type visibility into each other. No special configuration is necessary for the example to work. | ||
EAR sub-deployment isolation can be changed at the application server level, or per deployment in [[Jboss-deployment-structure.xml|jboss-deployment-structure.xml]]. | |||
More details on JEE module isolation in JBoss are available here: | More details on JEE module isolation in JBoss are available here: | ||
* [[WildFly_ee_Subsystem_Configuration#ear-subdeployments-isolated|ee Subsystem Configuration - ear-subdeployments-isolated]] | * [[WildFly_ee_Subsystem_Configuration#ear-subdeployments-isolated|ee Subsystem Configuration - ear-subdeployments-isolated]] | ||
* [[WildFly_Modules#EAR_Deployments|WildFly Modules - EAR Deployments]] | * [[WildFly_Modules#EAR_Deployments|WildFly Modules - EAR Deployments]] | ||
* [[Jboss-deployment-structure.xml|jboss-deployment-structure.xml]] | |||
=EJB Lookup= | =EJB Lookup= | ||
Line 23: | Line 26: | ||
The EJB reference is looked up in JNDI using the portable JNDI EJB naming scheme. | 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 | In this case, the EJB is named SimpleStatelessBean, it is deployed as part of the wrapper-servlet.war web module, which is packaged within the "stateless-ejb-and-servlet" EAR. The simplest possible portable JNDI name the servlet can use to look up the EJB, given the fact they're different modules of the same JEE application, is "java:app/stateless-ejb-example/SimpleStatelessBean". | ||
For more details on the JNDI EJB naming scheme, see: | For more details on the JNDI EJB naming scheme, see: | ||
Line 39: | Line 42: | ||
InitialContext ic = new InitialContext(); | InitialContext ic = new InitialContext(); | ||
SimpleStateless bean = (SimpleStateless)ic.lookup("java: | SimpleStateless bean = (SimpleStateless)ic.lookup("java:app/stateless-ejb-example/SimpleStatelessBean"); | ||
} | } | ||
catch(Exception e) { | catch(Exception e) { |
Latest revision as of 18:32, 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
Business Interface Type Access
The EJB code and the calling servlet component are deployed as part of two different EAR modules, so visibility of types amongst those two modules is subject to EAR module isolation configuration in the application server. In EAP 7, the modules are NOT isolated by default, so they have type visibility into each other. No special configuration is necessary for the example to work.
EAR sub-deployment isolation can be changed at the application server level, or per deployment in jboss-deployment-structure.xml.
More details on JEE module isolation in JBoss are available here:
- ee Subsystem Configuration - ear-subdeployments-isolated
- WildFly Modules - EAR Deployments
- jboss-deployment-structure.xml
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 web module, which is packaged within the "stateless-ejb-and-servlet" EAR. The simplest possible portable JNDI name the servlet can use to look up the EJB, given the fact they're different modules of the same JEE application, is "java:app/stateless-ejb-example/SimpleStatelessBean".
For more details on the JNDI EJB naming scheme, see:
The code that does the JNDI lookup is similar to:
SimpleStateless bean = null; ... try { InitialContext ic = new InitialContext(); SimpleStateless bean = (SimpleStateless)ic.lookup("java:app/stateless-ejb-example/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");