In everything we have done so far, the model has been that the EJB runs on a server machine and its clients run elsewhere.  (As a special case, the “elsewhere” may happen to be the same machine.)
But in an application designed using EJBs, it would be frequently necessary for one EJB to use the services provided by another.  This case the client of an EJB being another EJB is handled specially in EJBs, because the EJB technology provides extra features for co-operating EJBs.
If EJB-A is a client of EJB-B, the deployment descriptor of EJB-A (the client EJB) should reference EJB-B.  This is done by using the <ejb-ref> XML node.  In entity beans, this node goes at the same level and after the <reentrant> and also after <cmp-field>s, <primkey-field>s, and/or <env-entry>s, but before any <security-role-ref>s or <resource-ref>s.  (Many of these are optional, so you will not see them in all ejb-jar.xml files.  But if present, the order mentioned above should be preserved.)
In session beans, the <ejb-ref> node goes at the same level and after <transaction-type> and after any <env-entry>s, but before any <security-role-ref>s or <resource-ref>s.
The ejb-ref contains the following nodes, in this order:

A complete ejb-ref node might look as follows:

 <description>EJB Reference to stocks EJB</description>
<description>This is optional.
<description>Link to stocks EJB</description>
<ejb-ref-name>The name by which the linked EJB will be referenced.  This should start with “ejb/” e.g.
<ejb-ref-type>This must be “Session” or “Entity“.
<home>The fully qualified name of the linked EJB’s home interface.
<remote>The fully qualified name of the linked EJB’s remote interface.

Optional.  This is container dependent, and is used to point to the name of the linked EJB as known to the container.

READ  What are EJBs?

Once an ejb-ref has been added, the client ejb can use code like the following:

Context ctx = new InitialContext();
 StockQuotesHome = (StockQuotesHome)
 ctx.lookup( "java:comp/env/ejb/stocks" );

This code will return the linked EJB’s home object, which can then be used to create EJBs, find them, etc.
The “java:comp/env/” prefix is added to all JNDI lookups which have been made available in the EJB’s environment.
Exercise:  Link the stocks EJB to your account’s EJB.  Add a function in your account EJB that calls the stock EJB.  Build both EJBs and write a client program to test your new function. (Note:  Make sure to add the stock-ejb’s JAR file to the classpath before starting Blazix EJB server.)