5.7 Key Point Summary
This chapter introduces stateful session beans and contrasts them with stateless sessions beans. The MusicCart EJB is a classic example of a stateful session bean since it keeps track of client-specific conversational state. In addition, we implement the Value List Iterator Pattern, leveraging the benefits of both a stateful session bean (MusicIterator EJB) and a stateless session bean (MusicPage EJB). We bring these three EJBs together in one J2EE application with a web component JSP client. We examine how the EJB container manages the life cycles and assignment of EJB objects to clients and how it affects the overall performance of an enterprise application. We use local interfaces to further improve the performance garnered through the Value List Iterator Pattern.
Here are the key points from this chapter.
A stateful session bean keeps track of client-specific data throughout the session.
An EJB container assigns an instance of a stateful session bean to a single client. It can assign an instance of a stateless session bean to multiple clients (one at a time).
The conversational state of a stateful session bean goes away when the session terminates.
The EJB container can reacquire the resources consumed by a stateful bean by making it passive (this is called passivation). It can make it active when necessary.
Passivation requires serializing instance variables and writing them to secondary storage. During activation, the EJB container de-serializes the data and reads it back into memory.
Any instance variable that cannot be serialized must be handled by the bean developer in the ejbPassivate()and ejbActivate()methods. The EJB container invokes ejbPassivate()just before passivation and ejbActivate()just after activation.
The MusicCart EJB is a stateful session bean that allows a client to create a shopping cart and add and remove recordings.
CustomerVOis a value object that encapsulates the data pertaining to a "customer."
The home interface of a stateful session bean may contain more than one create()method. Each create()method must have a corresponding ejbCreate()method in the bean implementation class with the same signature.
The purpose of ejbCreate()is to properly initialize a stateful session bean's instance variables. If the client provides improper initialization data with create(), ejbCreate()should throw a CreateException.
A stateful session bean's remote interface contains the EJB's business method definitions.
A stateful session bean's implementation contains the code for the EJB business methods.
Class RecordingVOimplements method equals()so that collection utility class ArrayListcan perform comparison between its RecordingVOelements.
Business method removeRecording()throws application exception ShoppingExceptionif the RecordingVOobject is not currently in the shopping cart.
The Value List Iterator Pattern helps manage large lists by allowing the client to request data a page at a time.
We implement Value List Iterator by wrapping all client-specific operations in a stateful session bean (MusicIterator EJB). MusicIterator EJB then bundles the appropriate data into calls to a stateless session bean (MusicPage EJB).
The MusicPage EJB is a modification of the Music EJB presented in Chapter 4 (see "Session Beans and JDBC" on page 85). We replaced getMusicList()with getPage(), which returns a subset of the entire list.
The MusicIteratorremote interface extends the ValueListIteratorinterface. This separates the reusable methods from the domain-specific methods. Methods in ValueListIteratormust throw RemoteExceptionso that interface MusicIteratorcan extend it.
Because the resource-intensive MusicPage EJB is stateless and the fairly light-weight MusicIterator EJB is stateful, we've leveraged the benefits of both session bean types for optimal performance and client convenience.
The web component JSP client uses five separate JSP files to allow the user to login and manipulate an online shopping cart. It uses session objects and stateful session beans to maintain client state.
We've implemented local access for the MusicPage EJB. This requires providing two new interfaces: a local interface (MusicPageLocal) for the business methods, and a local home interface (MusicPageLocalHome) for the create()method.
Local EJBs and their clients must execute in the same Java Virtual Machine (JVM).
Local EJBs use pass by reference for method parameters and return values instead of pass by value. This eliminates the overhead of copying arguments as well as the serialization required by RMI.
Consider using local interfaces for tightly coupled EJBs.