Sun's JavaBean architecture is a component model for building reusable Java modules that can be composed to create standalone or client applications. One goal of the architecture is dynamic development within a direct manipulation GUI environment through drag and drop and linking operations. JavaBeans have been used most frequently to build user interfaces.
Sun's Enterprise JavaBean architecture is a component model for building and deploying multithread server-side applications. It is not used for building user interfaces, but, instead, for building very large distributed applications, such as those on which large, distributed enterprises, such as multinational corporations, depend. One goal is to support Web-based computing in which user access is provided through a conventional browsers, often in the form of an applet. One strategy for making this practical is moving most of the function from client to server, resulting in so-called thin clients.
The EJB supports multiple tiers of function, as opposed to the familiar two-tier client/server model. For example, a database application in a multitier architecture might include a thin client web interface or an applet that supports only data display and input (including user commands), a Web server to which the client connects, a Java servlet engine to which the seb server connects, an EJB "container to which the Java servlet class(es) connect, and a separate database engine to which the server connects. Thus, user requests are routed through multiple layers, from applet to server to database engine, with results following the reverse path.
The multitier approach can be expanded to include additional remote processors to support scaling. For example, a server program might route a user request to a number of different database systems, each residing on a different host anywhere on the network. Results could then be sent back to the original server, where they are consolidated and returned to the user.
Another goal of the EJB architecture is to remove common distributed application functions -- such as persistent storage, transaction processing, access control, and security -- from the application. Instead, those services are generalized and provided by the EJB environment, leaving the developer responsible only for the basic function of the application.
In the sections below, key concepts in the EJB architecture are discussed. That architecture is described in detail in Sun's Enterprise JavaBeans Specification.
Key Concepts
Container
Enterprise JavaBeans function within some sort of container. Most often, this container will be some form of Web server. However, its function and the interfaces it supports are well-defined within the EJB Specification.
In addition to supporting EJBs, the server that provides the container may also support other common Web functions. For example, it may support the HTTP protocol, thereby providing access to conventional Web pages; it may also support servlets and dynamic pages. The particular Enterprise server used in these lessons is IBM's WebSphere. It relies on IBM's DB2 database system, although it can use other DBMS such as Oracle and Sybase. WebSphere is also closely, aligned with IBM's VisualAge for Java development enviroanment.
The EJB container provides a number of different functions. It is responsible for executing the EJB. It provides persistent storage, if that is required. It provides access control and security. It supports transactions, including distributed transactions. And it supports deployment of an EJB over the network. Thus, the Enterprise server is a very powerful piece of software that supports not just EJBs but a variety of services used in distributed and Web-based applications.
If you want to work with Enterprise JavaBeans, the first step is to get and install an enterprise server.
Component
EJBs are components. They are reusable building blocks from which a particular application is composed. For example, a financial investment application might include an account bean, one or more portfolio beans, a stock ticker quote bean, and a buy/sell bean. The quote bean might connect to a database of current quotes, while the account and portfolio beans might connect to a different database. The user would interact with the application through an HTML page/form or a client applet that supports only data display and user input.
Transaction processing, persistence, security, and access control can all be provided by the container. This is done through property lists and deployment parameters, thereby relieving the application developer from these responsibilities and allowing a given bean to be reused in flexible ways in different applications.
There are two basic types of EJBs: session beans and entity beans.
Session Beans
Session beans are usually created, function for some limited period of time (a session), and are then destroyed. One of their most important functions is providing transactions. State may be maintained from one user interaction to another, and a session object can provide persistent storage, for example, through a file write, but that is not emphasized. If the system crashes, session beans are normally lost.
Entity Bean
Entity beans provide persistent data storage, maintained in a database. They support transactions and they can be recovered in the event of a crash, although these functions are normally handled at the session bean level.
Mechanics
When a client program, either an application or an applet, starts, it contacts the server and requests that the JavaBean it wishes to use be created. the server returns a proxy for the EJB that implements the EJBHome interface. This interface includes a create method. The client uses this object and its create method to ask the server to create an actual instance of the bean.
The object that is returned by the create method implements the remote interface of the EJB by extending the EJBObject interface. Once that object is received by the client, it can invoke its methods to carry out functions in the remote EJB.
Example