A locally deployed EJB may be looked-up as follows:
// Obtain a handle to the home factory component
LocalHomeFactory homeFactory = (LocalHomeFactory)
Lookup.getInstance().fetchComponent("/ejb/LocalHomeFactory");
// Perform the EJB lookup
AccountHome acctHome = (AccountHome)
homeFactory.lookup("org.examples.accounts.Account");
// Derive the remote interface from the home interface
// Invoke business methods as appropriate
A remotely deployed EJB may be looked-up as follows:
// Obtain a handle to the home factory component
RemoteHomeFactory homeFactory = (RemoteHomeFactory)
Lookup.getInstance().fetchComponent("/ejb/RemoteHomeFactory");
// Perform the EJB lookup
AccountManagerHome acctMgrHome = (AccountManagerHome)
homeFactory.lookup("org.examples.accounts.AccountManager");
// Derive the remote interface from the home interface
// Invoke business methods as appropriate
Additionally, the EJB Service provides APIs to perform JNDI lookups using environment parameters not reflected in a Configuration, but specified at runtime. More specfically, the functional interface contains two discovery methods are that allow the client to either pass a fully-specified javax.naming.InitialContext object or merely override the security pricinpal and credentials specified in the Configuration. This means of lookup might typically be used in an EJB-to-EJB scenario wherein it makes sense to maintain the current environment, as opposed to referencing that specified in the Configuration, as illustrated in the example below.
// From within an EJB implementation class...
// Obtain a handle to the home factory component
RemoteHomeFactory homeFactory = (RemoteHomeFactory)
Lookup.getInstance().fetchComponent("/ejb/RemoteHomeFactory");
// Perform the EJB lookup while maintaining the current security context
AccountManagerHome acctMgrHome = (AccountManagerHome)
homeFactory.lookup("org.examples.accounts.AccountManager", new InitialContext());
// Derive the remote interface from the home interface
// Invoke business methods as appropriate