Oracle9iAS Containers for J2EE Enterprise JavaBean Developer's Guide and Reference Release 2 (9.0.2) Part Number A95881-01 |
|
This chapter discusses EJB concepts that are specified fully in the J2EE specification. The remainder of the chapters in this book show only the tasks necessary to develop your EJBs.
For more details and examples of the concepts presented in this chapter, refer to books written by Sun Microsystems that discuss EJBs and J2EE Blueprint Architecture recommendations.
This chapter includes the following topics:
Enterprise JavaBeans (EJBs) can be one of three types: session beans, entity beans, or message-driven beans.
An EJB has two client interfaces:
The client uses both of these interfaces when invoking a method on a bean.
Figure 1-1 demonstrates a stateless session bean and corresponds to the following steps:
create
method on the home interface reference (home object). This creates the bean instance and returns a reference to the remote interface of the bean.
remove
method that is defined in the remote interface. Some beans, such as stateless session beans, cannot call the remove
method. In this case, the container removes the bean.
You must create the following four major components to develop an EJB:
Your bean implements the methods within either the SessionBean
, EntityBean
, or MessageDrivenBean
interface. The implementation contains logic for lifecycle methods defined in the home interface, business methods defined in the remote interface, and container callback functions defined in the SessionBean
, EntityBean
, or MessageDrivenBean
interface.
When you implement an EJB or write the client code that calls EJB methods, you must be aware of the parameter-passing conventions used with EJBs.
A parameter that you pass to a bean method--or a return value from a bean method--can be any Java type that is serializable. Java primitive types, such as int
, double
, are serializable. Any non-remote object that implements the java.io.Serializable
interface can be passed. A non-remote object that is passed as a parameter to a bean or returned from a bean is passed by value, not by reference. So, for example, if you call a bean method as follows:
public class theNumber { int x; } ... bean.method1(theNumber);
then method1()
in the bean receives a copy of theNumber
. If the bean changes the value of theNumber
object on the server, this change is not reflected back to the client, because of pass-by-value semantics.
If the non-remote object is complex--such as a class containing several fields--only the non-static and non-transient fields are copied.
When passing a remote object as a parameter, the stub for the remote object is passed. A remote object passed as a parameter must extend remote interfaces.
The next section demonstrates parameter passing to a bean, and remote objects as return values.
The EmployeeBean
getEmployee
method returns an EmpRecord
object, so this object must be defined somewhere in the application. In this example, an EmpRecord
class is included in the same package as the EJB interfaces.
The class is declared as public
and must implement the java.io.Serializable
interface so that it can be passed back to the client by value, as a serialized remote object. The declaration is as follows:
package employee; public class EmpRecord implements java.io.Serializable { public String ename; public int empno; public double sal; }
There are three types of EJBs: session beans, entity beans, and message-driven beans.
A session bean implements one or more business tasks. A session bean might contain methods that query and update data in a relational table. Session beans are often used to implement services. For example, an application developer might implement one or several session beans that retrieve and update inventory data in a database.
Session beans are transient because they do not survive a server crash or a network failure. If, after a crash, you instantiate a bean that had previously existed, the state of the previous instance is not restored. State can be restored only to entity beans.
A session bean implements the javax.ejb.SessionBean
interface, which has the following definition:
public interface javax.ejb.SessionBean extends javax.ejb.EnterpriseBean { public abstract void ejbActivate(); public abstract void ejbPassivate(); public abstract void ejbRemove(); public abstract void setSessionContext(SessionContext ctx); }
At a minimum, an EJB must implement the following methods, as specified in the javax.ejb.SessionBean
interface:
You use this method to obtain a reference to the context of the bean. Session beans have session contexts that the container maintains and makes available to the beans. The bean may use the methods in the session context to make callback requests to the container.
The container invokes setSessionContext
method, after it first instantiates the bean, to enable the bean to retrieve the session context. The container will never call this method from within a transaction context. If the bean does not save the session context at this point, the bean will never gain access to the session context.
When the container calls this method, it passes the reference of the SessionContext
object to the bean. The bean can then store the reference for later use. The following example shows the bean saving the session context in the sessctx
variable.
import javax.ejb.*; import oracle.oas.ejb.*; public class myBean implements SessionBean { SessionContext sessctx; void setSessionContext(SessionContext ctx) {sessctx
= ctx; // session context is stored in // instance variable } // other methods in the bean }
The javax.ejb.SessionContext
interface has the following definition:
public interface SessionContext extends javax.ejb.EJBContext { public abstract EJBObject getEJBObject(); }
And 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 session context when it wants to perform the operations listed in Table 1-1.
There are two types of session beans:
A stateless session bean does not maintain any state for the client. It is strictly a single invocation bean. It is employed for reusable business services that are not connected to any specific client, such as generic currency calculations, mortgage rate calculations, and so on. Stateless session beans may contain client-independent, read-only state across a call. Subsequent calls are handled by other stateless session beans in the pool. The information is used only for the single invocation.
The EJB container maintains a pool of these stateless beans to service multiple clients. An instance is taken out of the pool when a client sends a request. There is no need to initialize the bean with any information. There is implemented only a single create/ejbCreate
with no parameters--containing no initialization for the bean within these methods. There is no need to implement any actions within the remove/ejbRemove
, ejbPassivate
, ejbActivate
, and setSessionContext
methods. In addition, there is no need for the intended use for these methods in a stateless session bean. Instead, these methods are used mostly for EJBs with state--for stateful session beans and entity beans. Thus, these methods should be empty or extremely simple.
A stateful session bean maintains its state between method calls. Thus, there is one instance of a stateful session bean created for each client. Each stateful session bean contains an identity and a one-to-one mapping with an individual client. The state of this type of bean is maintained across several calls through serialization of its state, called passivation. This is why the state that you passivate must be serializable. However, this information does not survive system crashes.
To maintain state for several stateful beans in a pool, it serializes the conversational state of the least recently used stateful bean to a secondary storage. When the bean instance is requested again by its client, the state is activated to a bean within the pool. Thus, all resources are used performantly, and the state is not lost.
The type of state that is saved does not include resources. The container invokes the ejbPassivate
method within the bean to provide the bean with a chance to clean up its resources, such as sockets held, database connections, and hash tables with static information. All these resources can be reallocated and recreated during the ejbActivate
method.
If the bean instance fails, the state can be lost--unless you take action within your bean to continually save state. However, if you must make sure that state is persistently saved in the case of failovers, you may want to use an entity bean for your implementation. Alternatively, you could also use the SessionSynchronization
interface to persist the state transactionally.
For example, a stateful session bean could implement the server side of a shopping cart on-line application, which would have methods to return a list of objects that are available for purchase, put items in the customer's cart, place an order, change a customer's profile, and so on.
An entity bean is a complex business entity. An entity bean models a business entity or models multiple actions within a business process. Entity beans are often used to facilitate business services that involve data and computations on that data. For example, an application developer might implement an entity bean to retrieve and perform computation on items within a purchase order. Your entity bean can manage multiple, dependent, persistent objects in performing its necessary tasks.
An entity bean is a remote object that manages persistent data, performs complex business logic, potentially uses several dependent Java objects, and can be uniquely identified by a primary key. Entity beans are normally coarse-grained persistent objects, because they utilize persistent data stored within several fine-grained persistent Java objects.
Entity beans are persistent because they do survive a server crash or a network failure. When an entity bean is re-instantiated, the state of previous instances is automatically restored.
Each entity bean has a persistent identity associated with it. That is, the entity bean contains a unique identity that can be retrieved if you have the primary key--given the primary key, a client can retrieve the entity bean. If the bean is not available, the container instantiates the bean and repopulates the persistent data for you.
The type for the unique key is defined by the bean provider.
The persistence for entity bean data is provided both for saving state when the bean is passivated and for recovering the state when a failover has occurred. Entity beans are able to survive because the data is stored persistently by the container in some form of data storage system, such as a database. Entity beans persist business data using one of the two following methods:
An entity bean manages its data persistence through callback methods, which are defined in the javax.ejb.EntityBean
interface. When you implement the EntityBean
interface in your bean class, you develop each of the callback functions as designated by the type of persistence that you choose: bean-managed persistence or container-managed persistence. The container invokes the callback functions at designated times.
The javax.ejb.EntityBean
interface has the following definition:
public interface javax.ejb.EntityBean extends javax.ejb.EnterpriseBean { public abstract void ejbActivate(); public abstract void ejbLoad(); public abstract void ejbPassivate(); public abstract void ejbRemove(); public abstract void ejbStore(); public abstract void setEntityContext(EntityContext ctx); public abstract voic unsetEntityContext(); }
The container expects these methods to have the following functionality:
An entity bean is similar to a session bean because certain callback methods, such as ejbCreate
, are invoked at specified times. Entity beans use callback functions for managing its persistent data, primary key, and context information. The following diagram shows what methods are called when an entity bean is created.
An entity bean instance uses this method to retain a reference to its context. Entity beans have contexts that the container maintains and makes available to the beans. The bean may use the methods in the entity context to retrieve information about the bean, such as security, and transactional role. Refer to the Enterprise JavaBeans specification from Sun Microsystems for the full range of information that you can retrieve about the bean from the context.
The container invokes the setEntityContext
method, after it first instantiates the bean, to enable the bean to retrieve the context. The container will never call this method from within a transaction context. If the bean does not save the context at this point, the bean will never gain access to the context.
When the container calls this method, it passes the reference of the EntityContext
object to the bean. The bean can then store the reference for later use. The following example shows the bean saving the context in the this.ctx
variable.
public void setEntityContext(EntityContext ctx) { this.ctx = ctx; }
When the client invokes the remove
method, the container invokes the methods shown in Figure 1-3.
In addition, the ejbStore
and ejbLoad
methods are called for managing your persistent data. These are the most important callback methods--for bean-managed persistent beans. Container-managed persistent beans can leave these methods empty, because the persistence is managed by the container.
ejbStore
method is called by the container before the object is passivated or whenever a transaction is about to end. Its purpose is to save the persistent data to an outside resource, such as a database.
ejbLoad
method is called by the container before the object is activated or whenever a transaction has begun, or when an entity bean is instantiated. Its purpose is to restore any persistent data that exists for this particular bean instance.
You can choose to have the container manage your persistent data for the bean. You do not have to implement some of the callback methods to manage persistence for your bean's data, because the container stores and reloads your persistent data to and from the database. When you use container-managed persistence, the container invokes a persistence manager class that provides the persistence management business logic. In addition, you do not have to provide management for the primary key: the container provides this key for the bean.
The following table details the implementation requirements for the callback functions of the bean class:
There are two methods for managing the persistent data within an entity bean: bean-managed (BMP) and container-managed persistence (CMP). The main difference between BMP and CMP beans is defined by who manages the persistence of the entity bean's data. With CMP beans, the container manages the persistence--the bean deployment descriptor specifies how to map the data and where the data is stored. With BMP beans, the logic for saving the data and where it is saved is programmed within designated methods. These methods are invoked by the container at the appropriate moments.
In practical terms, the following table provides a definition for both types, and a summary of the programmatic and declarative differences between them:
Message-Driven Beans (MDB) provide an easier method to implement asychronous communication than using straight JMS. MDBs were created to receive asynchronous JMS messages. The container handles much of the setup required for JMS queues and topics. It sends all messages to the interested MDB.
Previously, EJBs could not send or receive JMS messages. It took creating MDBs for an EJB-type object to receive JMS messages. This provides all of the asynchronous and publish/subscribe abilities to an enterprise object that is able to be synchronous with other Java objects.
The purpose of an MDB is to exist within a pool and to receive and process incoming messages from a JMS queue. The container invokes a bean from the queue to handle each incoming message from the queue. No object invokes an MDB directly: all invocation for an MDB comes from the container. After the container invokes the MDB, it can invoke other EJBs or Java objects to continue the request.
A MDB is similar to a stateless session bean because it does not save conversational state and is used for handling multiple incoming requests. Instead of handling direct requests from a client, MDBs handle requests placed on a queue. Figure 1-4 demonstrates this by showing how clients place requests on a queue. The container takes the requests off of the queue and gives the request to an MDB in its pool.
MDBs implement the javax.ejb.MessageDrivenBean
interface, which also inherits the javax.jms.MessageListener
methods. Within these interfaces, the following methods must be implemented:
The container handles JMS message retrieval and acknowledgment. Your MDB does not have to worry about JMS specifics. The MDB is associated with an existing JMS queue. Once associated, the container handles dequeuing messages and sending acknowledgments. The container communicates the JMS message through the onMessage
method.
The major differences between session and entity beans are that entity beans involve a framework for persistent data management, a persistent identity, and complex business logic. The following table illustrates the different interfaces for session and entity beans. Notice that the difference between the two types of EJBs exists within the bean class and the primary key. All of the persistent data management is done within the bean class methods.
|
![]() Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|