The EJB Deployment and Runtime Environment
|Figure 2. Web to EJB: The figure shows how a client request travels from the client tier to the Application tier. Once the controller servlet receives the request, it converts it to a business service request and invokes the appropriate business service in the Business Service tier. The business service uses one or more entity beans to load data from and save data to resources in the Data tier.|
We will use JBoss 3.0.2 as our deployment and runtime environment for the examples that follow. We will design a simple Web application that allows user accounts to be created and retrieved using a Web browser communicating with a Java servlet, which communicates with a user service, which communicates with an entity EJB (see Figure 2
Writing and Deploying an Entity Bean
The following four steps illustrate the typical process to develop and deploy an entity bean:
- Write the classes and interfaces for your entity bean
- Write a deployment descriptor
- Package the entity bean and associated files inside of a jar file
- Deploy the bean.
An entity bean is composed of a minimum of three classes/interfaces:
The Deployment Descriptor
- The Component InterfaceBecause we are only concerned about accessing our EJB from the same JVM, we will create an interface that extends javax.ejb.EJBLocalObject.
public interface LocalUser extends EJBLocalObject
public String getUserID(); //primary key
public String getFullName();
public String setAddress(String address);
public String getAddress();
public String setCity(String city);
public String getCity();
public String setState(String state);
public String getState();
public String setZip(String zip);
public String getZip();
- The Home InterfaceAgain, because we are only concerned about accessing our EJB from the same JVM, we will create an interface that extends javax.ejb.EJBLocalHome.
public interface LocalUserHome extends EJBLocalHome
public LocalUser create(String userID,
public Collection findByFullName(String fullName)
public LocalUser findByPrimaryKey(String userID)
- The Enterprise Bean ClassThe final class implements either javax.ejb.SessionBean or javax.ejb.EntityBean (see Listing 1).
To deploy an EJB into an EJB container's environment, you must supply a deployment descriptor file to the EJB container. A deployment descriptor file is an XML document, named ejb-jar.xml, that specifies information about the bean such as its persistence type and transaction attributes. You need to package the Java classes and the deployment descriptor into a JAR or EAR file.
In addition to the standard ejb-jar.xml
file, JBoss has an additional deployment descriptor for declaring the CMP persistence properties for enterprise beans, named jaws.xml
. Listing 2
illustrates the ejb-jar.xml
deployment descriptor for UserEJB.
Notice that Listing 2
specifies the type and name of the primary key for the EJB and the fields of the EJB that are persistent. A table is created with a name that is the same as that of the Bean class, which in this case will be named 'UserEJB'. Listing 3
illustrates the jaws.xml
deployment descriptor for UserEJB.
Notice in Listing 3
that each field is declared within a cmp-field element. This will instruct the EJB container to create a column for each field in the new database table and map each field to its particular column.
All you need to do to deploy UserEJB in JBoss is create a jar file with the compiled classes for the EJB and the deployment descriptors for the EJB and place it into the deploy directory of your particular JBoss server.
The EJB Client
The architecture of this application makes it possible for you to access the EJB from within the same JVM. This design simplifies a few things, specifically the type of casting needed in order to obtain the home interface for the EJB. Listing 4
illustrates the UserService
object, which will access the EJB.
Notice that the UserService
in Listing 4
first attempts to find an instance of the EJB by calling the findByPrimaryKey
method and passing a user ID. If the instance is not found, the UserService
calls the create
method on the EJB's home instance in order to create the EJB. The findByPrimaryKey
method is intercepted by the EJB container, which attempts to retrieve a record from the database with a primary key named by the user ID. The create
method will add a new record to the database with a primary key named by the user ID.