Java Reference
In-Depth Information
Passivation and activation
A stateful session object lasts for the duration of the business process that typically spans multiple
client-invoked business methods. The process may last for several minutes, hours, or even days.
During its life cycle, the state of a stateful session instance may occupy a nontrivial amount of main
memory on the server. In addition, the state may include expensive resources such as database
connections. Because of these factors, it is important that the EJB container be able to reclaim the
resources (when the available resources become too low) by saving the state into some form of
secondary memory, such as a database or file systems. Later, when the state of the session object is
once again needed for the invocation of a business method, the EJB container can restore the state
from the saved image.
The process of saving the session objects' state to secondary storage is called passivation , whereas the
process of restoring the state is called activation . The container typically passivates a session object
when it needs to free resources in order to process requests from other clients or when it needs to
transfer the session bean instance to a different process for load-balancing purposes. The container
passivates the instance by invoking the ejbPassivate method and then serializing the instance and
moving it to some secondary storage. When it activates the session objects, it restores the session
bean's instance by deserializing the saved image of the passivated instance and then invoking the
ejbActivate method.
For many session beans, including the example YachtSessionEJB , the passivation and activation
processes do not require any programming effort from the bean developer. The bean developer has
only to ensure that the objects held in the session bean instance variables are serializable at
passivation. An object is serializable if it is an instance of a class that has implemented the
java.io.Serializable interface.
Business processes and rules
In this chapter and the next two, we build a simple example application to demonstrate the use of
stateful session beans and entity beans. Please note that these example EJBs are written for
educational purposes only. They may not represent the best (or even appropriate) approaches for the
hypothetical business process. The error and exception handling are not enough for these programs to
be used in any a production release. Nevertheless, once you have fully understood the example code
and have had it running, you can easily extend its functionality to meet your needs. In that sense, it
serves as a good starting point for your own EJB application development.
The example application is used for a yacht club in its yacht cruise operation. From time to time, the
club offers its members free yacht cruises. The business process includes the following tasks:
 
Operate the yacht such as start, stop, accelerate and decelerate.
 
Check the status of the yacht.
 
Pick up a club members as a passengers (only members can come on board).
 
Drop off passengers.
Since the business process involves multiple business-method invocations, it is implemented as a
stateful session bean: YachtSessionEJB . The yacht and club members are business entities and can
be modeled by entity beans. Although the MemberEJB and the YachtEJB are developed in the next
two chapters, the YachtSessionEJB code is listed in Listings 20-6 and 20-7 .
Listing 20-6: Remote and Home interfaces of YachtSessionEJB
/** YachtSessionEJB YachtSessionEJB
* @author: Andrew Yang
* @version: 1.0
*/
package java_database.YachtSessionSFBean;
Search WWH ::




Custom Search