EJB 非產品而是組件事務監控標準(Sun 定義):可伸縮、事務型、多用戶安全、一次編寫多平台部署。屬於 J2EE 伺服器端組件模型核心。
| 類型 | 子類型 | 特徵 |
|---|---|---|
| Entity Bean | 容器管理持久化 (CMP) | 容器自動映射字段至表、自動 INSERT/UPDATE/DELETE |
| Bean 管理持久化 (BMP) | 開發者手寫 JDBC 代碼,容器僅回調生命周期 | |
| Session Bean | 有狀態 | 專屬客戶端、維護會話狀態、協調 Entity Bean 完成任務 |
| 無狀態 | 通用可重用、無會話狀態、適合高併發 |
CabinPKCustomerPKCruisePKCruiseID+CabinID,防止重複預訂(選擇與下單間隔期並發風險)listAvailableCabins() 查詢可用艙位,bookPassage() 執行預訂、計費、生成票據(使用 CreditCard、Ticket 輔助類)ByCheck() ByCash() ByCredit() 三種事務方法package com.titan.travelagent;
public interface TravelAgent extends javax.ejb.EJBObject {
void setCruiseID(int cruise) throws RemoteException, FinderException;
int getCruiseID() throws RemoteException, IncompleteConversationalState;
void setCabinID(int cabin) throws RemoteException, FinderException;
int getCabinID() throws RemoteException, IncompleteConversationalState;
int getCustomerID() throws RemoteException, IncompleteConversationalState;
Ticket bookPassage(CreditCard card, double price) throws RemoteException, IncompleteConversationalState;
}
package com.titan.travelagent;
public interface TravelAgentHome extends javax.ejb.EJBHome {
TravelAgent create(Customer cust) throws RemoteException, CreateException;
}
實現上述所有業務邏輯(篇幅限制略),在 IBM VisualAge for Java 中開發,Entity Bean 相對 Session Bean 更易生成。
WebSphere Application Server 為 EJB 容器,Servlet (ReservationServlet) 調用 TravelAgent.bookPassage(),成功後轉 ProcessPaymentServlet 調用 ProcessPaymentBean 完成收費。