[ Pobierz całość w formacie PDF ]
.Use of the LDAPManagercomponent makes up almost all of the rest of the code.As you can see, the process of usingtwo (or more) data sources, formats, or types is all abstracted nicely in the UserManager bean,and allows the client to happily add, delete, and update users without worrying about physicaldata storage details.8.3 State DesignContinuing on with the Forethought business logic, I want to spend some time on the issue ofstateful versus stateless beans.I refer to it as an issue because it almost always manages tocome up when working with session beans, and can have a drastic effect on your application'sperformance.Specifically, stateless session beans are much more efficient than statefulsession beans.For a more detailed discussion on the issue, you can check out Richard Monson-Haefel'sEnterprise JavaBeans, which spends a great deal of print on how a container handles thesetwo kinds of beans.I'll briefly sum up the relevant portions here.A stateless session bean is avery lightweight bean, since it needs to carry around only EJB-mandated variables, notprogrammer-defined ones.As a result, most containers have pools of stateless beans.Becauseno single client needs to have access to a specific stateless bean instance (no state is beingkept, remember), a single instance can serve two, three, ten, or even a hundred clients.Thisallows the bean pool to be kept small, and negates a frequent need to grow or shrink the pool,which would take valuable processor cycles.A stateful bean is just the opposite: an instance is tied to the client that invoked its create( )method.This means that an instance must exist for every client accessing the stateful bean.Therefore, the bean pools must be larger, or must frequently be grown as more requests comein.The end result is longer process times, more beans, and fewer clients being served.Themoral of this technology tale is that if at all possible, you should use stateless session beans.152 Building Java"! Enterprise Applications Volume I: ArchitectureThe following sections demonstrate these principles, and specifically how a bean that appearsto be a better stateful bean can easily be converted into a stateless one.This should provideyou with some good ideas about state design and some handy tips on how to convert yourown stateful beans into stateless ones.8.3.1 Starting StatefulTake the case of an AccountManager bean that will handle a single user's accounts.For thisexercise, I'll keep the methods required for the bean simple.Example 8-7 shows the remoteinterface for this bean.Example 8-7.The AccountManager Remote Interfacepackage com.forethought.ejb.account;import java.rmi.RemoteException;import java.util.List;import javax.ejb.EJBObject;// Account beanimport com.forethought.ejb.account.AccountInfo;// AccountType beanimport com.forethought.ejb.accountType.UnknownAccountTypeException;public interface AccountManager extends EJBObject {public AccountInfo add(String type, float balance)throws RemoteException, UnknownAccountTypeException;public AccountInfo get(int accountId) throws RemoteException;public List getAll( ) throws RemoteException;public AccountInfo deposit(AccountInfo accountInfo, float amount)throws RemoteException;public AccountInfo withdraw(AccountInfo accountInfo, float amount)throws RemoteException;public float getBalance(int accountId) throws RemoteException;public boolean delete(int accountId) throws RemoteException;}As you can see, the manager operates upon a single account for a single user.This designallows a client to simply pass in the user's username one time (using the create( ) method),and worry about details of the account independently of keeping up with a username.Example 8-8 shows this method in the manager's home interface.153 Building Java"! Enterprise Applications Volume I: ArchitectureExample 8-8.The AccountManager Home Interfacepackage com.forethought.ejb.account;import java.rmi.RemoteException;import javax.ejb.CreateException;import javax.ejb.EJBHome;public interface AccountManagerHome extends EJBHome {public AccountManager create(String username)throws CreateException, RemoteException;}This interface provides a means to create a new account or to find all existing accounts for agiven username.Because the bean is keeping up with the account's user and the account's ID,both required for entity bean interaction, the bean must be stateful [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • odbijak.htw.pl