An enterprise bean is a Java component that can be combined with other resources to create J2EE applications. There are three types of enterprise beans, entity beans, session beans, and message-driven beans.
All beans reside in EJB containers, which provide an interface between the beans and the application server on which they reside.
Entity beans store permanent data, so they require connections to a form of persistent storage. This storage might be a database, an existing legacy application, a file, or another type of persistent storage.
Session beans typically contain the high-level and mid-level business logic for an application. Each method on a session bean typically performs a particular high-level operation. For example, submitting an order or transferring money between accounts. Session beans often invoke methods on entity beans in the course of their business logic.
Session beans can be either stateful or stateless. A stateful bean instance is intended for use by a single client during its lifetime, where the client performs a series of method calls that are related to each other in time for that client. One example is a "shopping cart" where the client adds items to the cart over the course of an online shopping session. In contrast, a stateless bean instance is typically used by many clients during its lifetime, so stateless beans are appropriate for business logic operations that can be completed in the span of a single method invocation. Stateful beans should be used only where absolutely necessary -- using stateless beans improves the ability to debug, maintain, and scale the application.
Message-driven beans enable asynchronous message servicing.
- The EJB container and a Java Message Service (JMS) provider work together to process messages. When a message arrives from another application component through JMS, the EJB container forwards it through an onMessage() call to a message-driven bean instance, which then processes the message. In other respects, message-driven beans are similar to stateless session beans.
- The EJB container and a Java Connector Architecture (JCA) resource adapter work together to process messages from an enterprise information system (EIS). When a message arrives from an EIS, the resource adapter receives the message and forwards it to a message-driven bean, which then processes the message. The message-driven bean is provided services such as transaction support by the EJB container in the same way that other enterprise beans are provided service.
Beans that require data access use data sources, which are administrative resources that define pools of connections to persistent storage mechanisms.
For more information about enterprise beans, see "Resources for learning."
Message-driven beans - JCA components
Message-driven beans - listener port components
Task overview: Using enterprise beans in applications
Enterprise beans: Resources for learning