Session EJB and Servlet on Different JBoss Instances
Internal
Overview
This example describes how a client component (servlet) deployed on a different JBoss instance looks up an EJB and makes a remote invocation.
GitHub Example
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
JNDI
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 stateless-ejb-example.jar deployment unit (JEE module). Considering the fact there is no JEE application (EAR), the simplest possible portable JNDI name the servlet can use to look up the EJB is "java:global/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:global/stateless-ejb-example/SimpleStatelessBean"); } catch(Exception e) { ... }
@EJB
The equivalent @EJB injection would be:
@EJB(mappedName = "java:global/stateless-ejb-example/SimpleStatelessBean") private SimpleStateless bean;
For more details, see
EJB Invocation
Once the EJB reference is obtained from JNDI, business interface methods can be called on its reference:
... bean.methodOne("something from servlet");