Enterprise JavaBeans are server-side components that encapsulate business logic. Typically, a client makes a call to an intermediary, usually a servlet. The servlet intercepts the call and invokes the appropriate EJB to execute and fulfill its task. EJBs are categorized by their communication characteristics: they are either synchronous or asynchronous beans.
Synchronous beans are transient and may not be shared or used by more than one client application at a time. One stipulation for invoking synchronous calls is that both client and Enterprise JavaBean are available at a specified time in order to interact with one another for the duration of a single transaction. The drawback to a synchronous call is the following: a client must wait until the EJB executes and returns the desired result before the client can continue to pursue other tasks. In essence, the communication line is temporarily blocked. Two kinds of EJBs use synchronous communications: session beans and entity beans.
Asynchronous beans are convenient because both caller and EJB need not be available at the same time.
Message-driven beans (MDBs) use asynchronous communications.
Consider the following scenario: a person orders a new laptop from a vendor. The vendor forwards the client’s order immediately to the vendor’s fulfillment house for assembling the unit, delivering the product to the consumer, and notifying both billing and customer when the transaction is complete. An asynchronous EJB is perfect for this kind of situation. It would be folly to invoke a synchronous bean and wait until the entire process is finished.
Clients interact with message-driven beans by sending and receiving Java Message Service messages in an asynchronous mode. We’ll look at MDBs in more detail later in this chapter. For now, let’s see how session beans and entity beans work.
Session beans exist in two categories:
Stateless session beans
Stateful session beans
Session beans encapsulate business processes by implementing both business logic and rules. They process credit card approvals, home loans, calculate payrolls, and track deliveries and order fulfillment. Stating it succinctly, session beans are action oriented. For example, a stateless session bean exists only for the duration of a single transaction. In case either a system or container event fails, data is not persisted and cannot be recovered. In addition, a stateless session bean is transient and can participate in a single transaction, but is not available for sharing by more than one client.
Stateless session beans are appropriate for occasions when a client request can be accommodated in a short period of time. Conversely, stateful session beans are appropriate for spanning multisession interaction between a client and an EJB. Shopping cart applications use stateful session beans for preserving relevant data between calls to the same site.
Entity beans model data such as Excel spreadsheet data, invoices, and business reports that must be saved to a repository such as a relational database, data warehousing source, or resource planning system. Entity beans provide an in-memory view of persisted data, are transactional, and do survive a container or system failure.
Three kinds of entity beans exist:
Container-managed persistent (CMP) beans
Bean-managed persistent (BMP) beans
Message-driven (MDB) beans
The container-managed persistent entity bean absolves the bean developer from responsibility for managing and writing system-level code such as client authentication and authorization, session pooling, the ejbPassivate() method called for use with session beans, and the ejbActivate() method invoked for use with session beans. The container, on behalf of the client, automatically performs these critical tasks.
The developer, responsible for specifying the container-managed persistent fields and container-managed relationship fields, flags data that is saved in the deployment descriptor file.
When a bean is designed to manage its own state, it is called bean-managed persistence.
Message-driven beans are complete enterprise beans. They are stateless, server-side, transaction-aware components used for processing asynchronous Java Message Service messages.
Here is how the EJB components break down so far:
Message-Driven Bean (MDB)
Container-managed persistent (CMP)
Bean-managed persistent (BMP)