EJB內(nèi)部資參2
發(fā)表時間:2023-08-15 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]使用EJB你就不用你自己寫支持分布式的對象的框架了 Java Beans是個功能的部件,而不是一個可運行的程序, 不需要也不能發(fā)布它, 但它又必須有一個可依賴的環(huán)境來運行 EJB則是可發(fā)布的部件、發(fā)...
使用EJB你就不用你自己寫支持分布式的對象的框架了
Java Beans是個功能的部件,而不是一個可運行的程序, 不需要也不能發(fā)布它,
但它又必須有一個可依賴的環(huán)境來運行
EJB則是可發(fā)布的部件、發(fā)布到一個容器中, 裝配成更大的系統(tǒng)
EJB和Applet和Servlet相似, Applet的容器是Browser, Servlet的容器是
支持Java的Web Server, 而EJB的容器是Application Server
EJB1.1 (2.0的規(guī)范已經(jīng)發(fā)布了,請到sun去download)
軟件生產(chǎn)工業(yè)化
多層應(yīng)用簡單化
事務(wù)處理
并發(fā)安全
Enterprise beans當(dāng)前兩個類型(2.0中有新內(nèi)容, 我自己還沒理解,不多講了)
Session Beans(又包括stateful 和stateless) 業(yè)務(wù)過程相關(guān)的邏輯,比如各種
計算,查詢
entity beans 數(shù)據(jù)相關(guān)的邏輯, 比如修改帳號的余額
EJB的規(guī)范定義了一些你的Bean可以實現(xiàn)標(biāo)準(zhǔn)的接口。 這些接口強迫你實現(xiàn)其中
特定方法。EJB的容器使用這些方法來管理你的Bean以及傳遞事件
最基本的接口javax.ejb.EnterpriseBean
public interface javax.ejb.EnterpriseBean extends java.io.Serializable
{}
這個接口中沒有方法,起到標(biāo)示你的Bean是個Enterprise bean.
sesssion beans 和entity beans有許多接口都是從這個特定的接口繼承來的(所
以你的bean并不直接實現(xiàn)EnterpriseBean接口)。所有的session beans都從jav
ax.ejb.SessionBean繼承,同理所有的EntityBean都從javax.ejb.EntityBean繼
承。
remote object 由MiddleSoftware提供的工具生成
remote interface 遠程接口---客戶端的應(yīng)用調(diào)用的接口
java.ejb.EJBObject
public interface EJBObject extends java.rmi.Remote {
public abstract javax.ejb.EJBHome getEJBHome() throws java.rmi.RemoteE
xception;
public abstract javax.ejb.Handle getHandle() throws java.rmi.RemoteExc
eption;
//一個EJB的持續(xù)引用, 存儲起來重新構(gòu)造
public abstract java.lang.Object getPrimaryKey() throws java.rmi.Remot
eException;
//只用在Entity Beans
public abstract boolean isIdentical(javax.ejb.EJBObject param1) throws
java.rmi.RemoteException;
public abstract void remove() throws java.rmi.RemoteException, javax.e
jb.RemoveException;
}
home object -- EJB object factory, 由工具生成,是EJB container的一部分
創(chuàng)建EJB objects
發(fā)現(xiàn)已經(jīng)存在的EJB objects (for entity beans)
刪除EJB objects
home interface --本地接口,
定義創(chuàng)建的方法,查找的方法和析構(gòu)的方法
package javax.ejb;
import java.rmi.Remote;
import java.rmi.RemoteException;
// Referenced classes of package javax.ejb:
// RemoveException, EJBMetaData, HomeHandle, Handle
public interface EJBHome
extends Remote
{
public abstract EJBMetaData getEJBMetaData()
throws RemoteException;
public abstract HomeHandle getHomeHandle()
throws RemoteException;
public abstract void remove(Object obj)
throws RemoteException, RemoveException;
public abstract void remove(Handle handle)
throws RemoteException, RemoveException;
}
session bean
package javax.ejb;
import java.rmi.RemoteException;
// Referenced classes of package javax.ejb:
// EJBException, EnterpriseBean, SessionContext
public interface SessionBean
extends EnterpriseBean
{
public abstract void ejbActivate()
throws EJBException, RemoteException;
public abstract void ejbPassivate()
throws EJBException, RemoteException;
public abstract void ejbRemove()
throws EJBException, RemoteException;
public abstract void setSessionContext(SessionContext sessionconte
xt)
throws EJBException, RemoteException;
}
setSessionContext(SessionContext sessioncontext)
session context 是session bean 和container交互的通道, 通常的實現(xiàn):
import javax.ejb.*;
public class MyBean implements SessionBean
{
private SessionBean sessiontext;
public void setSessionContext(SessionContext sessioncontext)
throws EJBException, RemoteException
{
this.sessiontext = sessiontext;
}
......
}
public void ejbCreate(...)
至少實現(xiàn)一個
home object實現(xiàn)相應(yīng)參數(shù)的一個create方法
比如
你的bean中有一個ejbCreate(int i)時
home object中有
public void create(int i)
鈍化和激活 ---僅用于stateful session bean
public abstract void ejbPassivate()
public abstract void ejbActivate()
當(dāng)太多的session bean被事例化時,container做鈍化和激活操作, 釋放和打開資
源
//stateless session bean
對于所有的客戶端是相同的,所有的信息通過參數(shù)傳遞或從數(shù)據(jù)庫等外部得到
初始化的唯一方式是無參數(shù)的 ejbCreate()方法
home object 有相應(yīng)的無參數(shù)create()方法
客戶端調(diào)用過程:
1、Look up a home object.
2、Use the home object to create an EJB object.
3、Call business methods on the EJB object.
4、Remove the EJB object.
Look up a home object
your client code must use the JNDI. J2EE products exploit directory se
rvices to stroe location infromation for resources that your applicati
on code uses in an enterprise deployment. These resources could be EJB
home objects, enterprise bean enviroment properties, database deriver
s, message service drivers, and other resources. By using directory se
rvices, you can writer application code that does not depend on specif
ic machine names or locations. This is all part of EJB's location tran
sparency, and it keeps your code portable. If later you decide thata r
esources should be located elsewhere, your code will not need to be re
built because the directory service can simply be updated to reflect t
he new resource locations. This greatly enhances maintenance of a mult
i-tier deployment that may evolve over time.
There are two common steps that must be taken to find any resource in
a J2EE deployment:
1. Associate the resource with a "nickname" in your deployment descrip
tor. Your J2EE product will bind the nickname to the resource.
2. Clients of the resource can use the nickname with JNDI to look up t
he resource across a deployment.
目前的主要的分布式應(yīng)用框架
1、 Miscrosoft's Distribute interNet Appplications Architecture(DNA)
相關(guān)的平臺和技術(shù)
NT
DCOM
MSMQ
MTS
Microsoft Wolfpack
Microsoft SQL Server
Microsoft Internet Information Server
Microsoft Management Console
2、SUN's J2EE
J2EE是規(guī)范而不是產(chǎn)品, 不至于讓用戶綁定到一個賣家(Microsoft)
支持高端的Unix平臺
內(nèi)置的CORBA支持
3、The Object Management Group's CORBA Standard
Common Object Request Broker Architecture (CORBA)
Internet Inter-ORB Protocol (IIOP)