程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2EE >> ejb內部資參之二

ejb內部資參之二

編輯:J2EE
使用EJB你就不用你自己寫支持分布式的對象的框架了   Java Beans是個功能的部件,而不是一個可運行的程序, 不需要也不能發布它, 但它又必須有一個可依賴的環境來運行 EJB則是可發布的部件、發布到一個容器中, 裝配成更大的系統 EJB和Applet和Servlet相似, Applet的容器是Browser, Servlet的容器是 支持Java的Web Server, 而EJB的容器是Application Server EJB1.1  (2.0的規范已經發布了,請到sun去download) 軟件生產工業化 多層應用簡單化 事務處理 並發安全 Enterprise beans當前兩個類型(2.0中有新內容, 我自己還沒理解,不多講了) Session Beans(又包括stateful 和stateless) 業務過程相關的邏輯,比如各種計算,查詢 entity beans 數據相關的邏輯, 比如修改帳號的余額 EJB的規范定義了一些你的Bean可以實現標准的接口。 這些接口強迫你實現其中特定方法。EJB的容器使用這些方法來管理你的Bean以及傳遞事件 最基本的接口javax.ejb.EnterpriseBean public interface javax.ejb.EnterpriseBean extends java.io.Serializable {} 這個接口中沒有方法,起到標示你的Bean是個Enterprise bean. sesssion beans 和entity beans有許多接口都是從這個特定的接口繼承來的(所以你的bean並不直接實現EnterpriseBean接口)。所有的session beans都從javax.ejb.SessionBean繼承,同理所有的EntityBean都從javax.ejb.EntityBean繼承。 remote object 由MiddleSoftware提供的工具生成 remote interface 遠程接口---客戶端的應用調用的接口 java.ejb.EJBObject public interface EJBObject extends java.rmi.Remote { public abstract javax.ejb.EJBHome getEJBHome() throws java.rmi.RemoteException; public abstract javax.ejb.Handle getHandle() throws java.rmi.RemoteException; //一個EJB的持續引用, 存儲起來重新構造 public abstract java.lang.Object getPrimaryKey() throws java.rmi.RemoteException; //只用在Entity Beans public abstract boolean isIdentical(javax.ejb.EJBObject param1) throws java.rmi.RemoteException; public abstract void remove() throws java.rmi.RemoteException, javax.ejb.RemoveException; }     home object -- EJB object factory, 由工具生成,是EJB container的一部分 創建EJB objects 發現已經存在的EJB objects (for entity beans) 刪除EJB objects home interface --本地接口, 定義創建的方法,查找的方法和析構的方法   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 sessioncontext)     throws EJBException, RemoteException; } setSessionContext(SessionContext sessioncontext) session context 是session bean 和container交互的通道, 通常的實現: 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(...) 至少實現一個 home object實現相應參數的一個create方法 比如 你的bean中有一個ejbCreate(int i)時 home object中有 public void create(int i)   鈍化和激活 ---僅用於stateful session bean   public abstract void ejbPassivate()   public abstract void ejbActivate()    當太多的session bean被事例化時,container做鈍化和激活操作, 釋放和打開資源         //stateless session bean 對於所有的客戶端是相同的,所有的信息通過參數傳遞或從數據庫等外部得到 初始化的唯一方式是無參數的 ejbCreate()方法 home object 有相應的無參數create()方法   客戶端調用過程: 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 services to stroe location infromation for resources that your application code uses in an enterprise deployment. These resources could be EJB home objects, enterprise bean enviroment properties, database derivers, message service drivers, and other resources. By using directory services, you can writer application code that does not depend on specific Machine names or locations. This is all part of EJB's location transparency, and it keeps your code portable. If later you decide thata resources should be located elsewhere, your code will not need to be rebuilt because the directory service can simply be updated to reflect the new resource locations. This greatly enhances maintenance of a multi-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 descriptor. Your J2EE product will bind the nickname to the resource. 2. ClIEnts of the resource can use the nickname with JNDI to look up the resource across a deployment. 目前的主要的分布式應用框架 1、 Miscrosoft's Distribute interNet Appplications Architecture(DNA) 相關的平台和技術 NT DCOM MSMQ MTS Microsoft Wolfpack Microsoft SQL Server Microsoft Internet Information Server Microsoft Management Console 2、SUN's J2EE J2EE是規范而不是產品, 不至於讓用戶綁定到一個賣家(Microsoft) 支持高端的Unix平台 內置的CORBA支持 3、The Object Management Group's CORBA Standard Common Object Request Broker Architecture (CORBA) Internet Inter-ORB Protocol (IIOP) ejb內部資參之一 ejb內部資參之二 ejb內部資參之三 ejb內部資參之四 ejb內部資參之五
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved