|
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 3 (10.1.3) B14428-01 |
|
![]() Previous |
![]() Next |
The EJB architecture is flexible enough to implement the objects that Table 1-1 lists.
Table 1-1 EJB Types
| Type | Description | See ... |
|---|---|---|
|
Session |
An EJB 3.0 or EJB 2.1 EJB component created by a client for the duration of a single client-server session used to perform operations for the client. |
|
|
Stateless |
A session bean that does not maintain conversational state. Used for reusable business services that are not connected to any specific client. |
"What is a Stateless Session Bean?" |
|
Stateful |
A session bean that does maintain conversational state. Used for conversational sessions with a single client (for the duration of its lifetime) that maintain state, such as instance variable values or transactional state. |
"What is a Stateful Session Bean?" |
|
Entity |
An EJB 3.0 compliant light-weight entity object that represents persistent data stored in a relational database using container-managed persistence. Because it is not a remotely accessible component, an entity can represent a fine-grained persistent object. |
|
|
Entity Bean |
An EJB 2.1 EJB component that represents persistent data stored in a relational database. |
"What is an EJB 2.1 Entity Bean?" |
|
CMP |
A Container-Managed Persistence (CMP) entity bean is an entity bean that delegates persistence management to the container that hosts it. |
"What is an EJB 2.1 CMP Entity Bean?" |
|
BMP |
A Bean-Managed Persistence (BMP) entity bean is an entity bean that manages its own persistence. |
"What is an EJB 2.1 BMP Entity Bean?" |
|
MDB |
A Message-Driven Bean (MDB) is an EJB 3.0 or EJB 2.1 EJB component that functions as an asynchronous consumer of Java Message Service (JMS) messages. |
"What is a Message-Driven Bean?" |
For more information, see:
Using EJB 3.0, the interfaces for your EJB implementation are not restricted by EJB type. For example, in your EJB 3.0 entity bean implementation, you may implement an EJB using a plain old Java object (POJO) and any plain old Java interfaces (POJI): you do not need to implement interfaces like javax.ejb.EntityBean and you do not need to provide separate interfaces that extend EJBHome, EJBLocalHome, EJBObject, or EJBLocalObject. A client may instantiate an EJB 3.0 POJO entity instance with new (or the EntityManager: see "How Do You Query for an EJB 3.0 Entity?"). A client may instantiate an EJB 3.0 session bean using dependency injection or JNDI lookup. For more information, see, "EJB 3.0 Support".
Table 1-2 lists the parts you create when developing an EJB 3.0 EJB.
Table 1-2 Parts of an EJB 3.0 EJB
| Part | Type | Description |
|---|---|---|
|
POJI |
An optional POJI annotated with |
|
|
POJI |
An mandatory POJI annotated with |
|
|
POJO |
A mandatory POJO that may optionally implement a component interface and contains the Java code that implements the methods defined in the optional home interface and component interface (business methods). If necessary, you can optionally annotate any method to serve as a container lifecycle callback function. |
|
|
|
Optional means of specifying attributes of the bean for deployment. These designate configuration specifics, such as environment, interface names, transactional support, type of EJB, and persistence information. Because this metadata can be expressed entirely through annotations (or defaults), deployment descriptor XML files are less important in EJB 3.0. Configuration in a deployment descriptor XML file overrides the corresponding annotation configuration, if present. For more information, see "Understanding EJB Deployment Descriptor Files". |
As Figure 1-1 illustrates, to acquire an EJB 3.0 EJB instance, a Web client (such as a servlet) or Java client uses JNDI while an EJB client may use either JNDI or resource injection. For more information about EJB clients, see "What Type of Client Do You Have?".
For entity beans, EJB 3.0 provides an EntityManager that you use to create, find, merge, and persist an EJB 3.0 entity (see "How Do You Query for an EJB 3.0 Entity?").
Figure 1-1 A Client Using an EJB 3.0 Stateful Session Bean by Component Interface
The client in Figure 1-1 accesses the EJB as follows:
The client retrieves the component interface of the bean.
The servlet or Java client uses JNDI to look up an instance of Cart.
The EJB client uses resource injection by annotating a Cart instance variable with the @EJB annotation: at run time, the EJB container will ensure that the variable is initialized accordingly.
In both cases, the EJB container manages instantiation. A home interface is not necessary.
The client invokes a method defined in the component interface (remote or local interface), which delegates the method call to the corresponding method in the bean instance (through a stub).
The client can destroy the stateful session bean instance by invoking a method in its component interface that is annotated in the bean instance with @Remove.
Stateless session beans do not require a remove method; the container removes the bean if necessary. The container can also remove stateful session beans that exceed their configured timeout or to maintain the maximum configured pool size. Entity beans do not require a remove method; you use the EJB 3.0 EntityManager to create and destroy entity beans.
Using EJB 2.1, the interfaces for your EJB implementation are based on EJB type. For example, in your EJB 2.1 entity bean implementation, you must implement the javax.ejb.EntityBean interface and you must provide separate interfaces that extend EJBHome or EJBLocalHome and EJBObject or EJBLocalObject. A client may instantiate an EJB 2.1 EJB instance only with a create method that your EJB home interface provides. For more information, see "EJB 2.1 Support".
Table 1-3 lists the parts you create when developing an EJB 2.1 EJB.
Table 1-3 Parts of an EJB 2.1 EJB
| Part | Type | Description |
|---|---|---|
|
|
Specifies the interface to an object that the container itself implements: the home object. The home interface contains the life cycle methods, such as the |
|
|
|
Specifies the business methods that you implement in the bean. The bean must also implement additional container service methods. The EJB container invokes these methods at different times in the life cycle of a bean. |
|
|
|
Contains the Java code that implements the methods defined in the home interface (life cycle methods), component interface (business methods), and the required container methods (container callback functions). |
|
|
|
Specifies attributes of the bean for deployment. These designate configuration specifics, such as environment, interface names, transactional support, type of EJB, and persistence information. |
A client uses the home interface to acquire an EJB 2.1 EJB instance and uses the component interface to invoke its business methods as Figure 1-2 illustrates. For more information about EJB clients, see "What Type of Client Do You Have?".
Figure 1-2 A Client Using an EJB 2.1 Stateless Session Bean by Home and Component Interface
The client in Figure 1-2 accesses the EJB as follows:
The client retrieves the home interface of the bean—normally through JNDI.
The client invokes the create method on the home interface reference (home object). This creates the bean instance and returns a reference to the component interface (remote or local interface) of the bean.
The client invokes a method defined in the component interface (remote or local interface), which delegates the method call to the corresponding method in the bean instance (through a stub).
The client can destroy the bean instance by invoking the remove method that is defined in the component interface (remote or local interface).
For some beans, such as stateless session beans, calling the remove method does nothing: in this case, the container is responsible for removing the bean instance.
The lifecycle of an EJB involves important events such as creation, passivation, activation, and removal. Each such event is associated with a callback defined on the EJB class (see "Callback Methods"). The container invokes the callback prior to or immediately after the lifecycle event (depending on the event type).
The lifecycle events associated with an EJB and whether or not the container or the bean provider is responsible for implementing callbacks is determined by the type of EJB you are developing (as specified in the appropriate EJB interface).
For an EJB 3.0 bean, when the container is responsible for the lifecycle callback, you do not need to provide an implementation in your bean unless you want to perform some additional logic.
For an EJB 2.1 bean, even when the container is responsible for the lifecycle callback, and even if you do not want to perform additional logic, you must at least provide an empty implementation of the lifecycle methods to satisfy the requirements of the applicable EJB interface.
For more information, see:
A lifecycle callback method is a method of an EJB class that you define to handle a lifecycle event (see "What is the Lifecycle of an EJB?"). The container invokes the callback associated with a given lifecycle event immediately prior to or immediately after the lifecycle event (depending on the event type).
You implement a lifecycle callback method to change the default behaviour of an EJB.
For an EJB 3.0 bean, you can annotate any EJB class method as a lifecycle method.
For an EJB 2.1 bean, you must at least provide an empty implementation of the lifecycle methods to satisfy the requirements of the applicable EJB interface.
The EJBContext interface provides an instance with access to the container-provided runtime context of an EJB 2.1 EJB bean instance. This interface is extended by the SessionContext, EntityContext, and MessageDrivenContext interfaces to provide additional methods specific to the enterprise interface Bean type.
The javax.ejb.EJBContext interface has the following definition:
public interface EJBContext {
public EJBHome getEJBHome();
public Properties getEnvironment();
public Principal getCallerPrincipal();
public boolean isCallerInRole(String roleName);
public UserTransaction getUserTransaction();
public boolean getRollbackOnly();
public void setRollbackOnly();
}
A bean needs the EJB context when it wants to perform the operations listed in Table 1-4.
Table 1-4 EJB 2.1 EJBContext Operations
Do not confuse EJBContext with IntialContext (see "Configuring the Initial Context Factory").
For more information, see:
Using the @Resource or @EJB annotation, an EJB 3.0 bean may use dependency injection mechanisms to acquire references to resources or other objects in its environment. You use @Resource to inject non-EJB resources and @EJB to inject EJBs.
If an EJB 3.0 bean makes use of dependency injection, OC4J injects these references after the bean instance is created, and before any business methods are invoked.
If a dependency on the EJB context is declared, the EJB context is also injected (see "What is EJB Context?").
If dependency injection fails, OC4J discards the bean instance.
|
Note: In this release, OC4J does not support resource injection in the web container. For more information, see "Servlet or JSP Client"). |
Annotations are another way of specifying an environment reference without having to use XML. When you annotate a field or property, the container injects the value into the bean on your behalf by looking it up from JNDI. When a reference is specified using annotations, you can still look it up normally using JNDI.
Example 1-1 shows how annotations relate to JNDI. The annotations in this example correspond to the ejb-jar.xml file equivalent in Example 1-2. Your code would have the exact same behavior if this XML and JNDI was used instead.
Example 1-1 Using Annotations and Resource Injection
@Stateless
@EJB(name="bean1", businessInterface=Bean1.class)
public class MyBean
{
@EJB Bean2 bean2;
public void doSomething()
{
// Bean2 is already injected and available
bean2.foo();
// or it can be looked up from JNDI
((Bean2)(new InitialContext().lookup("java:comp/env/bean2"))).foo();
// Bean1 has not been injected and is only available through JNDI
((Bean1)(new InitialContext().lookup("java:comp/env/bean1"))).foo();
}
}
Example 1-2 Equivalent ejb-jar.xml File Configuration
<ejb-local-ref> <ejb-ref-name>bean1</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local>Bean1.class</local> </ejb-local-ref> <ejb-local-ref> <ejb-ref-name>bean2</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local>Bean2.class</local> <injection-target> <injection-target-name>bean2</injection-target-name> </injection-target> </ejb-local-ref>