In Figure 10.2 you can see that there are three stages in the life cycle of a stateful session bean: creation, process operations, and termination. Figure 10.2. Life cycle of a stateful session bean.CreationThe life cycle of a session bean begins when a client application looks up the reference of the EJBHome of a stateful session bean and calls the create() method of the EJBHome to obtain a reference to the remote object of the stateful session bean. Because stateful session beans maintain conversational state with a client, the EJB container does not maintain a pool of stateful session bean instances. For each different client, the EJB container in the WebLogic Server creates an instance of the stateful session bean. For every subsequent request from the client, the same stateful session bean services the client requests. This initiates the process of creation of the stateful session bean by the EJB container in the WebLogic Server. The EJB container creates a new instance of the stateful session bean class on receiving the first request from a new client application. The stateful session bean remains associated with the client application through the end of its life cycle. After this, the EJB container invokes the first callback method of the stateful session bean, which is the setSessionContext() method, and passes the SessionContext object. The stateful session bean uses this object throughout its life cycle to maintain conversational state with the client application. In the next step of the creation process of the stateful session bean, the EJB container in the WebLogic Server invokes the ejbCreate() method of the bean that matches the signature of the create() method called by the client application on the EJBHome. After invoking the ejbCreate() method, the reference of the remote object of the stateful session bean is returned to the client application. The client application uses this remote object reference to invoke business methods published in the remote interface. The stateful session bean is now active and ready to process the client application requests. Process OperationsBecause stateful session beans are essentially transactional in nature, they in turn invoke other EJBs. For example, stateless session beans and entity beans are invoked upon receiving remote method invocations from the client application. During the conversational state, the EJB container maintains the stateful session bean in two states: active or passive. A stateful session bean in an active state remains in memory in the EJB container. The EJB container in the WebLogic Server determines which of the active stateful session beans are not being invoked frequently by client applications. Once the EJB container determines that a stateful session bean has not been used for quite some time, the EJB container optimizes resources by removing the active stateful session bean from its memory and saving its state to disk. This is called passivation. When a stateful session bean is passivated by the EJB container, the EJB container invokes the ejbPassivate() method of the stateful session bean. If a client application invokes a request on a passivated stateful session bean, the EJB container performs an activation of the stateful session bean. This essentially consists of reconstructing the state of the session bean from the disk. During this activation process, the EJB container invokes the ejbActivate() method of the stateful session bean. The ejbActivate() and ejbPassivate() callback methods enable the stateful session bean to perform any restoration, cleanup, or any intermediate processing before the stateful session bean is activated or passivated by the EJB container. The stateful session bean goes through activate and passivate states throughout its life cycle till it is terminated by the EJB container. Note Fields declared in the stateful session bean to be of type transient will not have their state maintained during passivation and activation of the stateful session bean. TerminationThere are two ways for a stateful session bean to be terminated by the EJB container. The normal way to terminate a stateful session bean is for the client application to call the remove() method. On receiving the remove() request from the client application, the EJB container unloads the loaded session bean from memory and invokes the ejbRemove() callback method. The ejbRemove() method enables the session bean to perform cleanup and the releasing of resources. The loaded session bean is then destroyed by the EJB container. The second way for an EJB to be terminated is for the session bean to be timed out. If a timeout occurs, the EJB container removes the session bean from memory and disassociates it from the client application. Flowchart for Developing a Stateful Session BeanNow look at the steps involved in developing a stateful session bean; see Figure 10.3. Figure 10.3. Flowchart for developing a stateful session bean.The first step is to define the requirements of the application. This involves understanding the different entities involved, their interaction, and the work flow of the application. In the second step, you will identify the primary actors in the application and their interactions. This will help you to determine which of the actors can be modeled as client components and which of them can be modeled as session beans, either stateful or stateless. The actors that perform business logic processing and need to maintain state with other actors are candidates for being implemented as stateful session beans. In this design step, you need to create the class diagrams and sequence diagrams. Then, write the different classes and interfaces of the stateful session bean and the other application classes. For the stateful session bean, define the following: Remote Interface extends javax.ejb.EJBObject Home Interface extends javax.ejb.EJBHome Session Bean Implementation class implements javax.ejb.SessionBean The third step involves compiling all the source code Java files and generating .class files. After this you register the stateful session bean in the deployment descriptor files ejb-jar.xml and weblogic-ejb-jar.xml. These deployment descriptor files must be copied to the subdirectory META-INF under the directory where your source files are located. Once your .class files and deployment descriptor files are ready, you must package them all together in a Java archive file. This Java archive package in turn must be compiled using the weblogic.ejbc compiler to generate the final Java archive, which contains the EJB container-generated classes for the EJBHome, EJBObject, and the stub and skeleton classes. Finally, you need to deploy the Java archive generated from the previous step in the WebLogic Server environment. Deploying the stateful session EJB can be done in three ways:
The stateful session EJB is now ready for use by any EJB client. |