EJB.5.7 Creating and Using a Session Object


An example of the session bean runtime objects is illustrated by the Figure EJB.5-3.

Figure EJB.5-3. Session Bean Example Objects

A client creates a Cart session object (which provides a shopping service) using a create(...) method of the Cart 's home interface. The client then uses this session object to fill the cart with items and to purchase its contents.

Suppose that the end- user wishes to start the shopping session, suspend the shopping session temporarily for a day or two, and later complete the session. The client might implement this feature by getting the session object's handle, saving the serialized handle in persistent storage, then using it later to reestablish access to the original Cart .

For the following example, we start by looking up the Cart 's home interface in JNDI. We then use the home interface to create a Cart session object and add a few items to it:

 CartHome cartHome = (CartHome)javax.rmi.PortableRemoteObject.narrow(    initialContext.lookup(...), CartHome.class);  Cart cart = cartHome.create(...);  cart.addItem(66);  cart.addItem(22); 

Next we decide to complete this shopping session at a later time so we serialize a handle to this Cart session object and store it in a file:

 Handle cartHandle = cart.getHandle();  serialize cartHandle, store in a file...  

Finally we deserialize the handle at a later time, re-create the reference to the cart session object, and purchase the contents of the shopping cart:

 Handle cartHandle =  deserialize from a file...  Cart cart = (Cart)javax.rmi.PortableRemoteObject.narrow(              cartHandle.getEJBObject(), Cart.class);  cart.purchase();  cart.remove(); 


Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net