Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.
Hannah Smalltree, Editorial DirectorWith the release of version 2.0 of the EJB specification, the local interface was introduced to eliminate the network overhead incurred when accessing an EJB in the same JVM. An EJB can be defined as implementing a local interface, a remote interface, or both.
The primary differences between local and remote EJBs are:
1) The home interface for a local EJB must extend javax.ejb.EJBLocalHome and the home interface for a remote EJB must extend javax.ejb.EJBHome.
2) The bean interface for a local EJB must extend javax.ejb.EJBLocalObject and the bean interface for a remote EJB must extend javax.ejb.EJBObject.
3) The lookup mechanism for a remote EJB includes:
a) Properties passed to the InitialContext constructor
b) Requirement to narrow the home object
The lookup steps are compared in the following examples:
Local interface lookup steps:
InitialContext jndiContext = new InitialContext();
MyEJBLocalHome home =
(MyEJBLocalHome)jndiContext.lookup("java:comp/env/ejb/MyEJB");
Remote interface lookup steps:
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY,
"SomeInitialContextFactoryName");
props.put(Context.SECURITY_PRINCIPAL, "jdoe");
props.put(Context.SECURITY_CREDENTIALS, "foobar");
props.put(Context.PROVIDER_URL, "some://provider.url");
InitialContext jndiContext = new InitialContext(props);
Object objref = jndiContext.lookup("java:comp/env/ejb/MyEJB");
MyEJBHome obj =
(MyEJBHome)PortableRemoteObject.narrow(objref,
MyEJBHome.class);
Good application frameworks will usually hide the lookup differences between local and remote EJBs in a factory object and expose similar methods for both, as follows:
public EJBLocalHome lookupLocalEJB(String jndiHomeName)
{
...
}
public EJBHome lookupRemoteEJB(String jndiHomeName)
{
...
}
Your decision to cast to a local interface or a remote interface can now be made in a clean manner within these separate methods. This was first published in October 2003