How to improve client-side EJB lookup
code
Context:
Typical EJB applications make use of a
client layer that invokes one or many EJBs several times. This is true for many
web applications as well.
When a client layer must use a
particular bean multiple times, there needs to be an effort made to improve the
performance of the JNDI call that looks up the bean. In addition, it would be
nice to reduce duplicated code.
Tip :
To improve client performance, create a
utility object that encapsulates the JNDI lookup of EJB home object reference.
In addition to performing home lookups, the utility object can cache the home
reference for reuse.
This tip contains an example – a utility
object that looks up the home object for an EJB:
public class
SomeBeanHomeCache {
private
static SomeBeanHome _home = null;
public
static EquityHome getHome() throws NamingException {
if
(_home == null) {
try
{
InitialContext
context = new InitialContext();
_home = (SomeBeanHome) PortableRemoteObject.narrow(
context.lookup(“test.someBean”),
SomeBeanHome.class);
}
catch (Exception e) {
e.printStackTrace();
// handle error
}
}
return
_home;
}
}