CONTENTS | PREV | NEXT | INDEX Designing Enterprise Applications
with the J2EETM Platform, Second Edition



5.2 Enterprise Beans as J2EE Business Objects

As discussed in the previous section, business objects need to provide some generic services to clients, such as support for transactions, security, and remote access. These common services are complex in nature and are outside the domain of the business logic required to implement an application. To simplify development, enterprise applications need a standard server-side infrastructure that can provide such services.

The EJB tier of the J2EE platform provides a standard server-side distributed component model that greatly simplifies the task of writing business logic. In the EJB architecture, system experts provide the framework for delivering system-level services, and application domain experts provide the components that hold business-specific knowledge. The J2EE platform enables enterprise developers to concentrate on solving the problems of the enterprise instead of expending their efforts on system-level issues.

The Enterprise JavaBeans architecture defines components--called enterprise beans--that allow the developer to write business objects that use the services provided by the J2EE platform. There are three kinds of enterprise beans: session beans, entity beans, and message-driven beans.

Later sections of this chapter discuss each type of enterprise bean in detail.

Enterprise beans live inside EJB containers, which provide life cycle management, transactions, security, persistence, and a variety of other services for them. An EJB container is part of an EJB server, which provides naming and directory services, e-mail services, and so on. When a client invokes an operation on an enterprise bean, the call is intercepted by its container. By interposing between clients and components at the method call level, containers can inject services that propagate across calls and components, and even across containers running on different servers and different machines. Because the container adds these services "behind the scenes," this mechanism simplifies development of both components and clients. Figure 5.1 illustrates this.

Figure 5.1 Client View of Enterprise Beans


5.2.1 Enterprise Beans and EJB Containers

The EJB architecture endows enterprise beans and EJB containers with a number of unique features that enable portability, reusability, and ease of use:

The following sections describe the home and component interfaces and the enterprise bean class. Note that only session and entity beans have client view interfaces. Message-driven beans are not directly accessible by clients. Access to message-driven beans is discussed in Section 5.6 on page 153.

5.2.1.1 Home Interface

The home interface provides methods for creating and removing enterprise beans. In addition, the home interface of an entity bean also contains methods to find instances of the bean based on certain search criteria, and it may contain home business methods.

The home interface for an enterprise bean with a remote client view must extend javax.ejb.EJBHome. The home interface for an enterprise bean with a local client view extends javax.ejb.EJBLocalHome.

Generally, the enterprise bean's remote home interface allows a client to do the following:

An enterprise bean's local home interface is similar to the remote home interface. It allows clients to create and remove enterprise bean instances, but it does not provide methods for getting metadata or for obtaining a handle.

5.2.1.2 Component Interface

The component interface defines the set of business methods available to clients. An enterprise bean may have a remote and/or a local client view. The bean developer must define a component interface for each such client view that the bean provides. Usually, a bean provides either a local or remote view, but not both.

The remote component interface must extend javax.ejb.EJBObject, while the local component interface extends javax.ejb.EJBLocalObject. Implementations of these interfaces (which are generated by the container) delegate invocation of a business method to an instance of the enterprise bean class.

The javax.ejb.EJBObject and the javax.ejb.EJBLocalObject interfaces define the methods that allow clients to perform the following operations on a reference to an enterprise bean object:

The javax.ejb.EJBObject interface for a bean with a remote client view defines a method that allows its clients to obtain a handle to the enterprise bean object. This method is not available to clients of a bean with a local client view.

5.2.1.3 Enterprise Bean Class

The enterprise bean class provides the actual implementation of the business methods of the bean. A business method defined on the enterprise bean class is called by the container when the client calls the corresponding method listed in the component interface. In the case of a message-driven bean, the container invokes the onMessage method defined on the message-driven bean class when a message arrives for the bean to service. Depending on whether the bean is an entity bean, a session bean, or a message-driven bean, the enterprise bean class must implement the javax.ejb.EntityBean, the javax.ejb.SessionBean, or the javax.ejb.MessageDrivenBean interface.

In addition to business methods, the home interface and the enterprise bean class also share responsibility for create methods and for finder methods and home methods (in the case of entity beans).

The create methods provide ways to customize a bean at the time it is created. For each create... method listed in the home interface, the bean class implements a corresponding ejbCreate... method. A message-driven bean class must also implement an ejbCreate method, even though it has no home interface.

Finder methods provide ways to locate a bean. For each finder method listed in the home interface of an entity bean with bean-managed persistence, the bean class implements the corresponding ejbFind... method. In the case of an entity bean with container-managed persistence, the ejbFind... methods are defined as query methods in the deployment descriptors, and their implementation is provided by the container.

Entity bean classes must also provide an implementation for each home method listed in the home interface. These home methods are implemented with corresponding ejbHome... methods.

An entity bean with container-managed persistence may also define select methods in its bean class. A select method is a query method for the internal use of the entity bean class and is not exposed in the home or component interface. The bean provider defines the semantics of a select method by specifying an Enterprise JavaBeans Query Language (EJB QL) query string in the deployment descriptor. The container provides the implementation of the select method. Select methods return values from the container-managed persistent and container-managed relationship fields of entity beans with container-managed persistence.

The following three sections contain in-depth discussions of the properties and uses of entity and session beans. Message-driven beans are covered in Section 5.6 on page 153.



CONTENTS | PREV | NEXT | INDEX
Copyright © 2002 Sun Microsystems, Inc. All Rights Reserved.