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:
<ejb-ref> <description>EJB Reference to stocks EJB</description> <ejb-ref-name>ejb/stocks</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>stocks.StockQuotesHome</home> <remote>stocks.StockQuotes</remote> <ejb-link>StockQuotes</ejb-link> </ejb-ref>
|<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.
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.)
- What are EJBs?
- An Overview of EJBs
- Writing an EJB from scratch
- Deploying the Session Bean
- EJB Handles
- EJB environment variables
- Linked EJBs
- Accessing JDBC from EJBs
- Transaction handling in EJB
- Security in EJB
- EJB Interview Questions
- Deploying and testing Stateless Session Bean
- Why EJB
- EJB3 – The Basics
- EJB3-Create an Entity Bean
- EJB3 Stateless Session Bean
- EJB3 – Creating a test client
- EJB 3.0 Features and Advantages