The EJB specification defines three types of enterprise beans: session beans, entity beans, and message-driven beans (MDB).
You use session beans to manage business logic in your application. You often use session beans to coordinate interactions between EJBs and perform complex manipulations to accomplish tasks, such as generating prices of products, interacting with an ATM, or booking a trip reservation. There are two types of session beans:
· Stateless session beans
· Stateful session beans
Stateless session beans Stateless session beans do not maintain a conversational state. A single-request business process is one that does not require state to be maintained. Stateless session beans can accommodate these types of single-request business processes.
Stateless session beans are particularly useful in the session-entity facade pattern. A facade is a high-level interface that masks lower-level subsystems. Stateless session beans can provide a high-level interface facade to business processes, in which session bean methods perform a unit of work, calling one or more methods in one or more entity beans.
Stateful session beans You can perform some business processes in a single business request, such as computing the price of goods or verifying a credit card account. Other business processes are more drawn out and last across multiple requests and transactions. For example, on an e-commerce website, adding items to an online shopping cart spans multiple method requests. The business process must track the user's state from request to request. Stateful session beans maintain a conversational state on behalf of a client. If a stateful session bean changes during a method invocation, that same state is available to the client on the following invocation.
When used in a clustered environment, JRun supports failover by maintaining stateful session-bean state across the cluster.
Entity beans Entity beans represent objects that persist through a server shutdown. The data representing an instance of an entity bean is typically stored in rows and tables of a relational database, which you access using a JDBC data store. These tables can also span multiple databases.
There are two types of entity bean persistence:
· Bean-managed persistence (BMP) The entity bean implementation manages persistence by coding database access and update statements in callback methods.
· Container-managed persistence (CMP) The container uses specifications made in the deployment descriptor to perform database access and update statements automatically.
BMP In BMP, the developer manages persistence by coding the appropriate database update code in callback methods. For example, the ejbUpdate method updates the database, and the ejbFindByPrimaryKey method locates a database row using the primary key.
For more information, see the CreditCard and Reservation EJBs in the compass-ear directory of the samples server.
CMP In CMP, the container manages persistence by automatically synchronizing the bean with the database at certain points in the lifecycle. With CMP, your bean implementation coding is simpler and can focus on the business logic.
The EJB 2.0 specification requires that application servers support EJB 1.1 CMP and EJB 2.0 CMP.
Support for EJB 2.0 CMP ·
The EJB implementation class is defined as an abstract class.
· Persistence is managed using EJB query language (EJBQL).
· Relationships let you maintain relationships between entity beans.
· CMP beans can use select methods in addition to finder methods.
A message-driven bean (MDB) acts as a JMS message listener. MDBs are different from session beans and entity beans because they have no remote, remote home, local, or local home interfaces. They are similar to other bean types in that they have a bean implementation, they are defined in the ejb-jar.xml file, and they can take advantage of EJB features such as transactions, security, and lifecycle management.
Like session beans, MDBs are responsible for coordinating tasks involving other session and entity beans. A major difference between a message-driven bean and a session bean is how they are accessed. A session bean provides a remote interface that defines which methods you can invoke, whereas, a message-driven bean does not. A message-driven bean subscribes to or listens for specific asynchronous messages; it responds by processing the message and managing the actions other beans take in response to those messages.
The main advantage that MDBs have over simple JMS message consumers is that the EJB container can instantiate multiple MDB instances to handle multiple messages simultaneously.