Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Building Java Enterprise Applications Volume I: Architecture phần 7 pps
Nội dung xem thử
Mô tả chi tiết
Building Java™ Enterprise Applications Volume I: Architecture
157
public float getBalance(int accountId) throws RemoteException {
return getAccount(accountId).getBalance( );
}
private Account getAccount(int id) {
try {
// Get an InitialContext
Context context = new InitialContext( );
// Look up the Account bean
AccountHome accountHome = (AccountHome)
context.lookup("java:comp/env/ejb/AccountHome");
Account account = accountHome.findByPrimaryKey(new
Integer(id));
return account;
} catch (Exception e) {
// Any problems - just return null
return null;
}
}
}
This is all basic EJB material, and shouldn't cause you any problems. You'll notice that this
class also uses a new finder method on the Account bean:
public Collection findByUserId(Integer userId)
throws FinderException, RemoteException;
The accompanying query element in the Account bean's entry in the ejb-jar.xml descriptor
would look like this:
<query>
<query-method>
<method-name>findByUserId</method-name>
<method-params>
<method-param>java.lang.Integer</method-param>
</method-params>
</query-method>
<ejb-ql>
<![CDATA[WHERE userLocal.id = ?1]]>
</ejb-ql>
</query>
To deploy the AccountManager bean, you would use this (additional) XML entry in your ejbjar.xml deployment descriptor:
<session>
<description>
This AccountManager bean allows administration of Forethought accounts.
</description>
<ejb-name>AccountManagerBean</ejb-name>
<home>com.forethought.ejb.account.AccountManagerHome</home>
<remote>com.forethought.ejb.account.AccountManager</remote>
<ejb-class>com.forethought.ejb.account.AccountManagerBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
Building Java™ Enterprise Applications Volume I: Architecture
158
<ejb-ref>
<ejb-ref-name>ejb/AccountHome</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>com.forethought.ejb.account.AccountHome</home>
<remote>com.forethought.ejb.account.Account</remote>
<ejb-link>AccountBean</ejb-link>
</ejb-ref>
</session>
Additions to your application server's vendor-specific descriptors should be equally simple.
With this bean in stateful form ready for use, it's time to see how it can be turned into a betterperforming stateless session bean.
8.3.2 Going Stateless
To move this bean into stateless territory, you first need to change the home interface's
create( ) signature. Since stateless beans can't maintain any information between method
calls, passing in a username (or any other data) to the create( ) method is useless. Make the
following change:
public AccountManager create( )
throws CreateException, RemoteException;
Once this change has been made, you need to determine which methods advertised by the
bean require a username for operation. In other words, browse through your bean's
implementation class and note any method that uses the username or user method variable.
Once you've determined the methods in this category, you will need to change the signature
for those methods in the remote interface:
public interface AccountManager extends EJBObject {
public AccountInfo add(String username, String type, float balance)
throws RemoteException, UnknownAccountTypeException;
public AccountInfo get(int accountId) throws RemoteException;
public List getAll(String username) 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;
}
In this case, only two methods require this information, so it's not terribly inconvenient.
However, in many cases conversion from stateful to stateless requires a parameter to be added
to ten, twenty, or more methods. Even though this example is somewhat trivial, I want to
continue the discussion assuming that it is a major issue to have to keep the username around
for these multiple method calls. Before getting to the solution, though, you'll need to update