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.
Figure 2. Example of N-tier architecture
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.
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
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
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
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:
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.
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
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 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.
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.