IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
      
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks > Java technology >
developerWorks
J2EE technologies for the stateless network: The ABCs of EJBs
An EJB technology primer

Return to article.

In this sidebar, Kyle Gabhart introduces you to the basics of Enterprise JavaBeans technology. Do you know the difference between EJB components and plain old JavaBeans components? Or what differentiates session beans from message-driven beans? Or why entity beans offer two kinds of persistence, not just one? This EJB primer answers these questions and more.
What is an EJB component?

Enterprise JavaBeans (EJB) technology is a server-side component architecture that provides an objective, vendor-neutral framework for developing enterprise-class distributed applications. Many people do not realize that Enterprise JavaBeans and JavaBeans are different technologies. Both are component architectures, but the similarities end there. A JavaBean is a simple Java class that provides an object-oriented interface to one or more pieces of data. An EJB component, on the other hand, is a scalable enterprise component capable of fulfilling complicated requests for business services.

Enterprise beans come in three varieties: session beans, message-driven beans, and entity beans. Session beans and entity beans are further divided into subtypes. We'll discuss each type of bean individually.

Session beans

Session beans are the workhorses of the EJB family. They handle client requests by processing business logic, managing transactions, and accessing enterprise resources, including other enterprise beans. Session beans are used to trigger a process in the enterprise such as running a payroll or processing an order. There are two types of session bean:

  • Stateless session beans maintain only per-request state. A stateless session bean treats each client identically. Often, a servlet or JSP page will maintain state for the client, but use stateless session beans to fulfill requests for business services.

  • Stateful session beans can retain information about a given client between calls. This allows the management of a client's state to be tightly coupled with the actual processing of business requests. Stateful session beans are not very commonly used, since they tend to consume large quantities of server resources.
Message-driven beans

Introduced in EJB 2.0, message-driven beans are similar to stateless session beans in that they fulfill individual client requests without regard to previous requests made by that client. The key difference between stateless session beans and message-driven beans is that the latter can be invoked asynchronously. A client simply sends a message to a topic or queue to which the message-driven bean is registered. The message-driven bean then retrieves the message and processes it locally, accessing enterprise resources and other EJB components much like a session bean does. Message-driven beans extend the same standardization, portability, and scalability that are associated with J2EE and EJB technology into realm of enterprise messaging.

Entity beans

The third enterprise bean type is the entity bean. Entity beans represent persistent data entities that need to be managed for concurrency, efficiency, integrity, security, and reliability. An entity could be a single row in a relational table, multiple rows from multiple tables in a relational table, or a single record from an object-oriented data store. There are two types of entity bean:

  • Container-managed persistent entity beans simplify and automate the development of EJB applications, particularly when it comes to mapping data elements to an entity model. The container handles the persistence, concurrency, and transactional flow internally. The developer does not write any code to control these processes.

  • Bean-managed persistent entity beans require more work, but they allow for more fine-grained control over a system's data access. With this type of entity bean, the developer must implement life-cycle methods defined by the EJB spec. These methods control the persistence, retrieval, and management of entity state. The container then decides if and when to call such life-cycle methods. Although the container calls the shots, the bean developer has control over what happens once a particular method has been called.
Why is EJB technology useful?

In the past, developing enterprise software was slow and tedious. Every application required some fundamental infrastructure (security, transaction control, persistence, resource management, legacy connections, and so on) before the business logic could even be written. Companies either created a new application infrastructure for every project, or they attempted to reuse existing components and architecture from previous projects. Most often, the applications they were attempting to borrow from had not been written with reuse in mind. Companies that did code with reuse in mind were often troubled by the lack of a standard for designing reusable, modular components. To make up for this, some companies created internal standards, even creating reusable component libraries. While this increased the productivity of their teams overall, training new staff to learn the proprietary framework was often difficult and time-consuming, and the results were sketchy at best.

The EJB standard is the result of a broad collaboration among information technology companies. Many industry leaders came together to produce the set of guidelines and component-centric infrastructure that is EJB technology. Using EJB technology, architects and developers can focus on solving business problems rather than on lower level functionality. EJB enables developers to write scalable, reliable, and secure applications without creating a custom distributed-object framework for every project. By allowing developers to leverage an existing distributed infrastructure, EJB facilitates rapid server-side application development.

EJB technology in the enterprise

Enterprise JavaBeans technology represents a heavyweight component architecture that is ideally suited for delivering secure, reliable request processing. EJB components are capable of handling thousands of concurrent requests for back-end business services, and they are best used in systems that require that much power. EJB components are considered middleware, because they often sit in the middle tier of a system architecture. In such an architecture, the client tier (browser, cell phone, or similar GUI) generally accesses the resource tier (database, XML repository, or similar data store), through middle-tier components such as enterprise beans. In an EJB system architecture, session beans typically handle the request processing, invoking entity beans, other session beans, and message-driven beans as necessary. In the case of an asynchronous request, a message-driven bean might take the place of a session bean as the direct request processor.

EJB technology offer a tremendous degree of enterprise-grade power and scalability. Because EJB technology is standardized, it offers portability and ease of development. It also frees organizations from the need to train staff members in a proprietary standard or to maintain a proprietary codebase. While it is robust, portable, and scalable, the EJB framework does come at a price. Substantial server resources are required to run enterprise beans, and although they can handle tremendous loads smoothly, EJB components do not perform especially well under more lightweight demands. In short, Enterprise JavaBeans technology is a powerful and compelling technology, but it should be used only when the business case demands it.

Return to article.




developerWorks > Java technology >
developerWorks
  About IBM  |  Privacy  |  Terms of use  |  Contact