Session EJB and Servlet on Different JBoss Instances: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 16: Line 16:


=EJB Lookup=
=EJB Lookup=


==JNDI==
==JNDI==

Revision as of 20:03, 24 March 2017

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

https://github.com/NovaOrdis/playground/tree/master/jee/ejb/stateless-and-servlet-different-jboss-instances

Business Interface Type Access

The calling servlet needs to business interface type to make the call, so the classes related to the business interface will have to be packaged within the WAR. Since those classes are also needed in the EJB deployment unit, the best strategy is to develop those as part of shared "common-types" Maven module, and pull its artifact both in the WAR and the EJB JAR. The example linked above showcases this approach.

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:

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/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

EJB Invocation

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

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