Session EJB and Servlet in Different Deployment Units on Same JBoss Instance: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
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. No special configuration is necessary for the example to work.  
The EJB code and the calling servlet component are deployed as part of completely separate deployment units, so the servlet won't have by default visibility into the types available in the EJB deployment unit. It needs this visibility because it uses the business interface type, so the WAR must be explicitly configured to depend on the module created for the EJB deployment unit. This is done by declaring this dependency in jboss-deployment-structure.xml:


EAR sub-deployment isolation can be changed at the application server level, or per deployment in [[Jboss-deployment-structure.xml|jboss-deployment-structure.xml]].
<pre>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <dependencies>
            <module name="deployment.stateless-ejb-example.jar"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>
</pre>


More details on JEE module isolation in JBoss are available here:
For more details on JEE deployments and modules, see [[WildFly_Modules#JEE_Deployment_Modules|JEE deployments and modules]]. For more details on jboss-deployment-structure.xml, see [[Jboss-deployment-structure.xml|jboss-deployment-structure.xml]].
* [[WildFly_ee_Subsystem_Configuration#ear-subdeployments-isolated|ee Subsystem Configuration - ear-subdeployments-isolated]]
* [[WildFly_Modules#EAR_Deployments|WildFly Modules - EAR Deployments]]
* [[Jboss-deployment-structure.xml|jboss-deployment-structure.xml]]


=EJB Lookup=
=EJB Lookup=

Revision as of 19:10, 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 as part of completely separate deployment units, a JAR and a WAR, but within the same JBoss instance. The lookup is done via JNDI.

GitHub Example

https://github.com/NovaOrdis/playground/tree/master/jee/ejb/stateless-and-servlet-different-deployment-units-same-as-instance-jndi

Business Interface Type Access

The EJB code and the calling servlet component are deployed as part of completely separate deployment units, so the servlet won't have by default visibility into the types available in the EJB deployment unit. It needs this visibility because it uses the business interface type, so the WAR must be explicitly configured to depend on the module created for the EJB deployment unit. This is done by declaring this dependency in jboss-deployment-structure.xml:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <dependencies>
            <module name="deployment.stateless-ejb-example.jar"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

For more details on JEE deployments and modules, see JEE deployments and modules. For more details on jboss-deployment-structure.xml, see 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:

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: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");