Overview of EJB technology

The EJB technology is the server-side component architecture for J2EE. The EJB technology is emerging as the technology for the development and deployment of business logic within a larger enterprise application. It is predominantly used in the middle tier of an N-tier architecture. This middle tier provides communication between client components of the client tier and Enterprise Information Systems (EISs) of the server tier, as Figure 2 shows.

Note:
This section provides a high-level overview of the Enterprise JavaBeans specification 1.1. For a more complete discussion, see the Sun Java Web site at http://java.sun.com/products/ejb/docs.html.

Figure 2. Example of N-tier architecture

The figure shows a flow chart of N-tier architecture. There are 3 labelled tiers, from top to bottom: Client tier, Middle tier and Server tier. The Client tier has 2 nodes: Java client and Servlet or other web-based client. These 2 nodes flow into the Middle tier where there are various EJB instances. Each of these EJB instances flows into the Server tier into one of several EIS systems - many EJB instances can flow to the same EIS system.

The EJB technology allows users to isolate their business logic in the middle tier, away from the actual presentation and data layers (the client and server tiers, respectively). As Figure 2 shows, this middle tier is made up of the following components:

The following sections describe each of these components in more detail.

Application server

In the context of an enterprise bean, an application server provides basic resource-allocation services to enterprise beans, which access EISs. It provides support for many middleware services, including the following:

The benefit of accessing an EIS through an application server is that the client component does not need to know the details of connection management, security management, and transaction management. As Figure 3 shows, the client component is a client-side module that communicates with an application server to access various components (such as EJBs) that the application server manages.

An application server interacts with an EJB instance through an EJB container (see Figure 3). This container is a Java component that the application server implements. It manages the execution of the EJB instance by providing its run-time environment. In most cases, the same vendor provides both the application server and an implementation of an EJB container that executes within the application server.

Though the EJB container manages interactions between the EJB instance and the client component, it is not visible to the client component. Instead, the client component communicates with the EJB instance through a pair of interfaces listed in Table 1.

Table 1. EJB interfaces for client component access to an EJB

EJB interface for client component Description EJB interface
Home interface Used by the client component to create and remove an EJB instance.
EJBHome

Remote interface Used by the client component to request execution of a business method of the enterprise bean. A business method encapsulates particular business logic that the client component needs the EJB to perform.
EJBObject

Figure 3 shows the EJB architecture, which uses an EJB container within an application server to manage communication between a client component and an EJB instance.

Figure 3. Accessing an EIS through an EJB

The figure shows the workflow of accessing an EIS through an EJB. On the left of the image is a node labelled "Client component", from this node two flow lines pass into an area titled "Application server", at the interface to the Application server area the flow lines pass through one node each labelled Remote Interface and Home Interface respectively. These 2 nodes overlap a subset of the Application server area called the EJB container, the flow lines continue from the interface nodes and end at a node contained within the EJB container which is labelled EJB client.

Enterprise bean

An enterprise bean is a server-side component in the J2EE architecture. It enables client components to communicate with an EIS. The client component sends a request for information in an EIS through the enterprise bean. The enterprise bean communicates directly with the EIS, returning any requested information to the client component. The client component and enterprise bean communicate with each other through the home and remote interfaces (see Figure 3), which are implemented by the EJB container. The Enterprise JavaBeans specification 1.1 provides standards for these home and remote interfaces.

The Enterprise JavaBeans specification 1.1 also defines two kinds of enterprise beans, each of which is implemented as a particular Java interface, as Table 2 shows.

Table 2. Types of enterprise beans

Type of enterprise bean Description EJB interface
Entity bean Represents a business concept; usually is persistent.
EntityBean
Session bean Responsible for managing a process or task; usually exists only for the duration of a session.

SessionBean

Note:
Because Server Access for EJB implements its enterprise bean as a session bean, the remainder of this section concentrates on describing a session bean rather than an entity bean. For more information on an entity bean, consult the Enterprise JavaBeans specification 1.1.

A session bean can be one of two types:

A session bean must provide the following information to an application server for the bean's deployment within an EJB container:

Home and remote interfaces

The home and remote interfaces provide the EJB methods that are externalized to client components, as follows:

The EJB provider must provide classes that define the home and remote interfaces of the EJB. When the enterprise bean is deployed, the deployment tools of the application server use these definitions to generate the implementations of the home and remote interfaces for the EJB container. The client component uses these implementations when it originates a request for the enterprise bean. In this way, all interactions with the enterprise bean go through the EJB container, which routes them to the enterprise bean.

Note:
In Java, an interface provides a specification of the behavior of an object but not the actual behavior. There is an important distinction between defining an interface (which provides only the names of the interface and its methods) and implementing an interface (which provides the actual code to implement the interface methods). The EJB provider provides the definitions of the home and remote interfaces while the EJB container provides the implementations of these interfaces using information in the deployment descriptor.

Figure 4 shows how information is provided for the home and remote interfaces of a sample enterprise bean called sessionBean. This enterprise bean provides two create()methods in its home interface; the client component can create an instance of this session bean with either of these methods. The bean also provides two business methods in its remote interface; the client component can interact with this session-bean instance with these two business methods.

Figure 4. Providing the home and remote interfaces of a session bean

The figure shows how information is provided for the home and remote interfaces of a sample enterprise bean. The screen is split int two by a vertical line. The left half is: "What the enterprise bean provides" and the right: "What the EJB container provides". On the left there are two nodes labelled "remote interface (class that defines the bean's remote interface)" and "Home interface (class that defines the bean's home interface)". The remote interface node contains the following pseudo-code: "Public Interface sessionObject extends EJBObject." In this example two methods are returned by this interface "businessMethod" 1 and 2. The client component can interact with the enterprise bean instances with these two business methods. The Home interface node has the following text inside: "Public Interface sessionHome extends EJBHome" and then lists the 2 "create" methods associated with this sample enterprise bean. On the right half, there are again two nodes labelled: "sessionObject class (class that implements the sessionObject interface" and "sessionHome class (Class that implements the sessionHome interface)". The sessionObject class node lists the 2 business methods for the sample bean (businessMethod's 1 and 2). The sessionHome class node contains the 2 "create" methods for the bean.

SessionBean class

For an EJB container to communicate with the session bean, an EJB provider must provide a Java class that implements the SessionBean interface. This class contains implementations of the following methods:

When the EJB container implements the methods of the home and remote interface, it includes in these methods calls to the corresponding methods of the SessionBean class, as Figure 5 shows.

Figure 5. Calling methods of the SessionBean class

The figure depicts the fact that when the EJB container implements the methods of the home and remote interfaces it includes calls to the corresponding methods of the sessionbean class in these methods. On the left side of the diagram are the sessionObject class and sessionHome class nodes of the EJB container. Each node is split between the 2 methods associated with it (businessMethod 1 and 2 for the sessionObject class and create methods 1 and 2 for the sessionHome class). From each of these subdivisions a unidirectional arrow points to a similarly titled box within a sessionBean class (which is the class that implements the sessionBean interface) node (in fact the create nodes map to ejbCreate methods in the sessionBean class). The remaining nodes within the sessionBean class are the sessionBen() method itself and nodes for other standard methods and other optional methods.

The SessionBean methods use the EIS-specific API to communicate directly with the EIS. By isolating the EIS-specific API calls to the session bean, neither client components nor the application server need to know this API. Instead, the client component uses calls in the home and remote calls to request EIS services through the session bean.

Deployment descriptor

An enterprise bean is deployed within an EJB container. At deployment of the enterprise bean, the container generates implementations for both the home and remote interfaces of the enterprise bean. The container reads a deployment descriptor to obtain the EJB-specific information it needs. This deployment descriptor, called ejb-jar.xml, is an XML file that the EJB provider initializes with information about its enterprise bean. This information includes the names of the Java interfaces that define the home and remote interfaces. In this way, the EJB container can build the custom interfaces that the client component needs to access the enterprise bean.

Copyright IBM Corp. 1997, 2004