Enterprise JavaBean Types

The EJB 2.0 specification defines three types of Enterprise JavaBeans: the session bean, the entity bean, and the message-driven bean. Figure 2.1 shows all three types of Enterprise JavaBeans.

Figure 2.1. Enterprise JavaBean types.


Session beans contain business-processing logic. Entity beans contain data-processing logic. Message-driven beans allow clients to asynchronously invoke business logic. In the following sections, you'll learn about each type of Enterprise JavaBeans.

Session Beans

As its name suggests, session beans implement a conversation between a client and the server side. Session beans execute a particular business task on behalf of a single client during a single session. They implement business logic such as workflow, algorithms, and business rules.

Session beans are analogous to interactive sessions. Just as an interactive session isn't shared among users, a session bean is not shared among clients. Like an interactive session, a session bean isn't persistent (that is, its data isn't saved to a database). Session beans are removed when the EJB container is shut down or crashes.

You can think of a session bean object as an extension of the client on the server side. It works for its client, sparing the client from complexity by executing business tasks inside the server.

Session beans typically contain business process logic and workflow, such as sending an email, looking up a stock price from a database, and implementing compression and encryption algorithms.

There are two types of session beans: stateless and stateful. A stateless session bean represents a conversation with the client without storing any state. On the other hand, a stateful session bean represents a conversational session with a particular client. Such a session object automatically maintains its conversational state, within its member variables, across multiple client-invoked methods.

In Day 3, “Understanding Session Beans,” we'll scratch the surface of session beans and look into more details of both of their subtypes. In Day 5, “Developing Stateless Session Beans,” we'll take a hands-on approach to developing a stateless session bean. Day 6, “Developing Stateful Session Beans,” is dedicated to stateful session beans with a practical example from our University Registration System.

Entity Beans

If you've worked with databases, you're familiar with persistent data. The data in a database is persistent; that is, it exists even after the database server is shut down.

Entity beans are persistent objects. They typically represent business entities, such as customers, products, accounts, and orders. Typically, each entity bean has an underlying table in a relational database, and each instance of the bean corresponds to a row in that table.

The state of an entity bean is persistent, transactional, and shared among different clients. It hides complexity behind the bean and container common services. Because the clients might want to change the same data, it's important that entity beans work within transactions. Entity beans typically contain data-related logic, such as inserting, updating, and removing a customer record in the database.

Two types of entity beans are relevant to persistence: container-managed persistence (CMP) and bean-managed persistence (BMP). In a CMP entity bean, the EJB container manages the bean's persistence according to the data-object mapping in the deployment descriptor. Any change in the entity bean's state will be automatically saved to the database by the container. No code is required in the bean to reflect these changes or to manage the database connection. On the other hand, a BMP entity bean has to manage both the database connections and all the changes to the bean's state.

In Day 8, “Understanding Entity Beans,” we'll scratch the surface of entity beans and look into more details of both of their subtypes. Day 10, “Developing Bean-Managed Persistence Entity Beans,” is dedicated to developing a CMP entity bean. BMP is discussed with a hands-on example in Day 11, “Developing Container-Managed Persistence Entity Beans.” We'll also look into the container-managed relationship of entity beans, a new enhancement to EJB 2.0.

Message-Driven Beans

In synchronous communication, the client blocks until the server-side object completes processing. In asynchronous communication, the client sends its message and does not need to wait for the receiver to receive or process the message. Session and entity beans process messages synchronously.

Message-driven beans, on the other hand, are stateless components that are asynchronously invoked by the container as a result of the arrival of a Java Message Service (JMS) message. A message-driven bean receives a message from a JMS destination, such as a queue or topic, and performs business logic based on the message contents, such as logic to receive and process a client notification.

An example of a message-driven bean is when a shopper makes an online purchase order; an order bean could notify a credit verification bean. A credit verification bean could check the shopper's credit card in the background and send a notification message for approval. Because this notification is asynchronous, the shopper doesn't have to wait for the background processing to complete.

In Day 13, “Understanding JMS and Message-Driven Beans,” we'll explore the JMS architecture and highlight the use of message-driven beans. Developing a message-driven bean will be the subject of Day 14, “Developing Messsage-Driven Beans,” when we'll choose an example from our University Registration System.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.123.189