




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、會話會話Bean Session Bean 主要與商業處理邏輯有關,它代表一個商業處理過程。會話會話Bean12.1 Session Bean 的作用的作用 12.3 Session Bean的分類的分類12.4 創建無狀態創建無狀態 Session Bean 12.2 Session Bean 的生命周期的生命周期12.5 創建有狀態創建有狀態 Session Bean會話會話Bean12.6 Sessin Bean的更多細節問題的更多細節問題 12.8 Remote和和Home接口的限制接口的限制12.7 Session Bean要求、限制和許可要求、限制和許可 12.1 Session
2、Bean的作用的作用 Session Bean代表客戶應用和EJB容器之間的會話。Session Bean通常都是實現商業邏輯并且和entity bean交互以執行具體操作。不過session bean并不一定要使用entity bean。如果需要,它可以直接和數據庫通信。 12.2 Session Bean的生命周期的生命周期 Session Bean 和 Entity Bean 的最大區別在于它們的生命周期。 Session Bean 的生命周期很短,通常它的壽命和一個會話的時間或客戶端的一次調用過程相當。 一個客戶端的會話過程可能會持續到瀏覽器窗口完全關閉,或持續到Java應用程序運行結
3、束。客戶端一次會話時間的長短決定了Session Bean 的生命周期的長短,這就是“會話” Bean 的來歷。12.2 Session Bean的生命周期的生命周期 EJB容器負責管理Session Bean 的生命周期,通常每個Session Bean 都有一個生命期限的限制,當客戶端連接時間超過這個時間限制,EJB容器自動刪除該 Session Bean。如果應用程序關閉或重新啟動, Session Bean就徹底刪除了,也就是說 Session Bean 是生存在內存中的。 Session Bean不能進行永久性存儲,但 Session Bean 也可以執行數據庫相關操作,但它自己卻并
4、不是一個永久性對象。12.3 Session Bean的分類的分類 無狀態的Session Bean(Stateless Session Bean) 無狀態的Session Bean并不保存客戶端狀態,所以對于客戶端每個Bean實例都是一樣的,這就意味著任何一個Bean實例都可以為任何一個客戶端提供服務,也就是說無狀態Session Bean可以一直生存在Bean池中供客戶端使用,而不需要浪費時間重新初始化和創建。12.3 Session Bean的分類的分類 有狀態的Session Bean(Stateful Session Bean) 有狀態的Session Bean需要維護客戶端與Ses
5、sion Bean之間的會話狀態,為了節省資源,希望一個Session Bean實例可以服務于多個客戶端,當一個客戶端暫時不發出請求時,Session Bean會把與客戶端對應的會話保存起來,這叫Bean的鈍化(Passivate),然后這個Bean就可以服務于其他客戶端了,當重新發出請求時, 容器從Bean池中取出一個Bean實例,或者新創建一個Bean實例,或者先鈍化一個Bean實例,然后再讀入相關信息到Bean實例,這個過程叫做Bean的激活(Activated) 。12.4 創建無狀態的創建無狀態的Session Bean 從編程的角度看,創建無狀態的Session Bean和創建有狀
6、態的是一樣簡單的。除了在配置工具里修改一個設置外,僅有的一點不同是在bean的初始設計階段,無狀態的Session Bean并不記得方法調用之間的任何東西,bean需要的任何消息都必須由客戶端獲得。雖然無狀態的Session Bean并不記得面向session的數據,不過可以在一個無狀態的session bean中存放數據,只是不能存放與客戶端相關的數據。 12.4 創建無狀態的創建無狀態的Session Bean 在StatelessHello的例子中,通過調用greet()方法,返回Hello+“參數” 。下面是“Hello World”session bean Remote 接口(無狀態
7、版本)12.4 創建無狀態的創建無狀態的Session Beanimport java.rmi.*;import javax.ejb.*;/* Defines the methods you can call on a StatelessHello object */public interface StatelessHello extends EJBObject /* Returns a greeting for the named object */public String greet(String thingToGreet) throws RemoteException;12.4 創建
8、無狀態的創建無狀態的Session Bean 在這個例子中,Remote接口僅提供了一個greet方法,該方法接收一個參數并且返回一個歡迎詞。例如,如果傳送“World”參數給greet,greet方法將返回“Hello World!”。下面是StatelessHello bean的Home接口。12.4 創建無狀態的創建無狀態的Session Beanimport java.rmi.*;import javax.ejb.*;/* Defines the methods for creating a StatelessHelloWorld */public interface Stateles
9、sHelloHome extends EJBHome /* Creates a StatelessHello session bean. A stateless session beancant have a create method that takes parameters. */public StatelessHello create() throws RemoteException, CreateException;12.4 創建無狀態的創建無狀態的Session Bean 無狀態的session bean僅擁有一個create方法,而且該方法不能接受任何參數。這看起來有些奇怪,不過
10、如果考慮到無狀態session bean的含義你就會明白了。這種bean不能記住某個客戶的任何信息,實際上,為了性能上的原因,容器也許會不時地讓不同的session處理某個客戶的方法調用。由于session并不需要記住某個客戶的信息,因此使用另一個bean來處理負載并不會帶來任何問題。 12.4 創建無狀態的創建無狀態的Session Bean 如果bean的create方法接受任何的參數,session bean實例之間的行為將會有所不同,因為你為create方法提供不同的值。實現無狀態session bean與有狀態的session bean是一樣簡單的。下面是StatelessHello
11、Bean類,它實現了Remote和Home接口。12.4 創建無狀態的創建無狀態的Session Beanimport java.rmi.*;import java.util.*;import javax.ejb.*;/* The implementation class for the StatelessHello bean */public class StatelessHelloBean implements SessionBean/* The session context provided by the EJB container. A session bean must hold
12、on to the context it is given. */private SessionContext context;12.4 創建無狀態的創建無狀態的Session Bean/* An EJB must have a public, parameterless constructor */public StatelessHelloBean ()/* Called by the EJB container to set this sessions context */public void setSessionContext(SessionContext aContext)conte
13、xt = aContext; /* Called by the EJB container when a client calls the create() method inthe Home interface */public void ejbCreate()throws CreateException12.4 創建無狀態的創建無狀態的Session Bean/* Called by the EJB container to wake this session bean up after ithas been put to sleep with the ejbPassivate metho
14、d. */public void ejbActivate()/* Called by the EJB container to tell this session bean that it is beingsuspended from use (its being put to sleep). */public void ejbPassivate() 12.4 創建無狀態的創建無狀態的Session Bean/* Called by the EJB container to tell this session bean that it has beenremoved, either becau
15、se the client invoked the remove() method or thecontainer has timed the session out. */public void ejbRemove()/* Returns a greeting for the named object */public String greet(String thingToGreet)return Hello +thingToGreet+!; 12.4 創建無狀態的創建無狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean 使用EJB時,只需要得到EJB Home接
16、口的一個引用。在得到Home接口的引用后,就可以通過其中的create方法來創建一個bean實例,然后調用bean的Remote接口的方法。 12.4 創建無狀態的創建無狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean 首先做的第一件事情是得到JNDI naming context的一個引用,它是命名系統的一個接口,通過使用命名系統來找到EJB和其它對象。要得到naming context的一個引用,一個方法是創建一個InitialContext對象,如下:Context namingContext = new InitialContext();12.4 創建無狀
17、態的創建無狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean javax.naming.InitialContext public class InitialContext extends Object implements Context12.4 創建無狀態的創建無狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean擁有了naming context,這樣就可以使用lookup方法來找到需要的EJB。例如,如果使用“StatelessHello”的JNDI名字來配置HelloWorldSession bean,以下的代碼可以找到該bean
18、的Home接口:HelloWorldSessionHome home = (HelloWorldSessionHome)PortableRemoteObject.narrow(context.lookup( StatelessHello ),HelloWorldSessionHome.class);12.4 創建無狀態的創建無狀態的Session Bean創建客戶端訪問創建客戶端訪問Session Bean 使用EJB的時候,不能通過標準的Java強制轉換操作符來轉換遠程的引用。必須使用PortableRemoteObject.narrow。EJB使用一個稱為RMI-IIOP的特殊形式RMI,
19、需要這個特殊的語法來作轉換。必須要記住的是無需找到EJB的Remote接口,只需要Home接口;然后就可以使用Home接口的create和find方法來得到Remote接口。12.4 創建無狀態的創建無狀態的Session Bean下面是一個客戶測試無狀態session bean的代碼import java.util.*;import javax.naming.*;import javax.rmi.*;public class TestStatelessHello public static void main(String args) try /* Creates a JNDI naming
20、 context for location objects */Context context = new InitialContext();12.4 創建無狀態的創建無狀態的Session Bean/* Asks the context to locate an object named HelloWorld and expects theobject to implement the HelloWorldSessionHome interface */StatelessHelloHome home = (StatelessHelloHome)PortableRemoteObject.nar
21、row(context.lookup(StatelessHello),StatelessHelloHome.class);/* Asks the Home interface to create a new session bean */StatelessHello session = (StatelessHello) home.create();12.4 創建無狀態的創建無狀態的Session BeanSystem.out.println(session.greet(World);System.out.println(session.greet(Interstage Application
22、Server);System.out.println(session.greet(Apworks);/* Destroy this session */session.remove(); catch (Exception exc) exc.printStackTrace(); 13.5 創建有狀態的創建有狀態的Session Bean創建創建Remote接口接口 下面展示的是HelloWorldSession接口,它是Hello World session bean的Remote接口。 12.5 創建有狀態的創建有狀態的Session Beanimport java.rmi.*;import
23、javax.ejb.*;/* Defines the methods you can call on a HelloWorldSession object */public interface HelloWorldSession extends EJBObject /* Returns the sessions greeting */public String getGreeting() throws RemoteException;/* Changes the sessions greeting */public void setGreeting(String aGreeting) thro
24、ws RemoteException; 12.5 創建有狀態的創建有狀態的Session Bean創建創建Home接口接口 一個session bean的Home接口包含有創建新session的方法。對于Hello World例子,有兩個不同的create方法,一個沒有參數,而另一個允許你提供自己的歡迎詞。下面展示了HelloWorldSessionHome接口。 12.5 創建有狀態的創建有狀態的Session Beanimport java.rmi.*;import javax.ejb.*;/* Defines the methods for creating a HelloWorldSe
25、ssion */public interface HelloWorldSessionHome extends EJBHome /* Creates a HelloWorldSession bean with default settings */public HelloWorldSession create() throws RemoteException, CreateException;/* Creates a HelloWorldSession bean with a specific initial greeting */public HelloWorldSession create(
26、String aGreeting)throws RemoteException, CreateException; 12.5 創建有狀態的創建有狀態的Session Bean創建實現的類創建實現的類 接口是EJB開發中比較簡單的部分,而session bean還需要更多的工作。當你寫一個session bean時,有一些方法你必須包含在bean中以滿足EJB容器的要求。這些額外的方法是setSessionContext, ejbRemove, ejbActivate和 ejbPassivate。此外,當你實現你的create方法時,你需要將它們命名為ejbCreate而不只是create。 1
27、2.5 創建有狀態的創建有狀態的Session Bean注意注意要記住的是容器調用這些方法。當使用Home接口的方法來創建一個新的EJB時,容器最終會調用ejbCreate方法。同樣,當刪除一個bean時,容器將會調用ejbRemove方法來告訴bean它已經被移除。 下面展示的是HelloWorldSession和HelloWorldSessionHome接口的實現。 12.5 創建有狀態的創建有狀態的Session Beanimport java.rmi.*;import java.util.*;import javax.ejb.*;/* The implementation class
28、for the HelloWorldSession bean */public class HelloWorldSessionBean implements SessionBean/* Holds the sessions greeting */protected String greeting;12.5 創建有狀態的創建有狀態的Session Bean/* The session context provided by the EJB container. A session bean musthold on to the context it is given. */private Ses
29、sionContext context;/* An EJB must have a public, parameterless constructor */ public HelloWorldSessionBean ()/* Called by the EJB container to set this sessions context */public void setSessionContext(SessionContext aContext)context = aContext;12.5 創建有狀態的創建有狀態的Session Bean/* Called by the EJB conta
30、iner when a client calls the create() method inthe Home interface */public void ejbCreate()throws CreateExceptiongreeting = Hello World!;12.5 創建有狀態的創建有狀態的Session Bean/* Called by the EJB container when a client calls thecreate(String) method in the Home interface */ public void ejbCreate(String aGre
31、eting)throws CreateExceptiongreeting = aGreeting;/* Called by the EJB container to wake this session bean up after ithas been put to sleep with the ejbPassivate method. */public void ejbActivate()12.5 創建有狀態的創建有狀態的Session Bean/* Called by the EJB container to tell this session bean that it is being s
32、uspended from use (its being put to sleep). */public void ejbPassivate()/* Called by the EJB container to tell this session bean that it has been removed, either because the client invoked the remove() method or the container has timed the session out. */public void ejbRemove() 12.5 創建有狀態的創建有狀態的Sess
33、ion Bean/* Returns the sessions greeting */public String getGreeting()return greeting;/* Changes the sessions greeting */public void setGreeting(String aGreeting)greeting = aGreeting; 12.5 創建有狀態的創建有狀態的Session Bean 你想做的全部工作就是建立一個帶有getGreeting和setGreeting方法的bean,最終會得到兩個Java接口和一個有8個方法的類。 12.5 創建有狀態的創建有
34、狀態的Session Bean 提示提示 由于在編寫EJB應用時你最終會產生很多的文件,因此對于產生的類使用一些一致的命名傳統是重要的。Remote和 Home接口都通常命令為XXX和XXXHome,XXX是bean的名字。實現的類通常命令為XXXBean或者XXXImpl。你還可以考慮根據這個bean是一個session bean還是一個entity bean,將實現的類命令為XXXEB或者XXXSB。不管你決定如何命名你的類,它們都必須是一致的,這樣在多人開發時會避免很多麻煩。 12.5 創建有狀態的創建有狀態的Session Bean注意注意 這里使用XXX代表Remote接口,XXXH
35、ome代表Home接口,而XXXImp1代表實現。該bean使用Remote接口名。如果Remote接口被稱為ShoppingCart,Home的接口就是ShoppingCartHome,實現就是ShoppingCartImp1,而且該bean被引用為ShoppingCart bean。 12.5 創建有狀態的創建有狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean下面顯示了一個客戶端程序。import java.util.*;import javax.naming.*;import javax.rmi.*;public class TestHellopublic
36、static void main(String args)try12.5 創建有狀態的創建有狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean/* Creates a JNDI naming context for location objects */Context context = new InitialContext();/* Asks the context to locate an object named HelloWorld and expects the object to implement the HelloWorldSessionHome i
37、nterface */HelloWorldSessionHome home = (HelloWorldSessionHome)PortableRemoteObject.narrow(context.lookup(HelloWorld),HelloWorldSessionHome.class);12.5 創建有狀態的創建有狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean/* Asks the Home interface to create a new session bean */HelloWorldSession session = (HelloWorldSes
38、sion) home.create();System.out.println(The default greeting is: +session.getGreeting();session.setGreeting(Interstage!);System.out.println(The greeting is now: +session.getGreeting();/* Destroy this session */session.remove();12.5 創建有狀態的創建有狀態的Session Bean創建客戶訪問創建客戶訪問Session Bean/* Now create a sessi
39、on with a different greeting */session = (HelloWorldSession) home.create(Welcome to J2EE!);System.out.println(Created a new session with a greeting of: +session.getGreeting();/* Destroy this session */session.remove();catch (Exception exc)exc.printStackTrace(); 12.6 Sessin Bean的更多細節問題的更多細節問題 Session
40、Bean的接口的接口setSessionContext SessionBean的接口的接口 每一個session bean都必須實現SessionBean接口,它包含有4個方法,EJB容器使用這些方法來管理session bean。 setSessionContext ejbRemove() ejbActivate() ejbPassivate() setSessionContext SessionContext對象包含有session bean運行環境的信息,并包含到Home接口的引用,以及自身的引用,事務信息和某個方法調用者的標識符。 對于每個session bean,setSession
41、Context方法都會被調用一次,這也是bean初始化的一部分。在調用setSessionContext后,該bean就成為EJB容器的一個活動部分,并且一直保持活動狀態,直到調用ejbRemove為止。 setSessionContext提示提示 setSessionContext方法是放入初始代碼的好地方,在這里可以創建數據庫連接或者查找另一個bean的Home接口。 ejbRemove EJB容器調用session bean的ejbRemove方法來告訴該bean的服務將要停止。這時bean應該要清除它保留的全部資源。提示提示: 如果bean在setSessionContext方法中建立
42、了一個數據庫連接,那么需要在ejbRemove方法中關閉該連接。如果你創建了任何的session bean,也可在ejbRemove方法中移除,同時將定位的Home接口設置為null。 ejbPassivate 和和 ejbActivate Enterprise JavaBeans規范中,提供了各種方法讓EJB容器實現負載均衡以及其它各種和性能相關的工作。Passivation/Activation就是這樣一個操作,它與計算機管理內存的方式是類似的。 ejbPassivate 和和 ejbActivate ejbPassivate 和 ejbActivate 方法允許一個EJB容器來使用內存交
43、換技術。在某個時刻,如果EJB容器覺得內存中的許多bean都有一段時間沒人訪問了,它可能選擇將其中的bean存儲到磁盤上。也就是說EJB容器使用對象串行化不常用的bean并存儲在某個文件中。這個過程在EJB中被稱為passivation。當一個客戶想訪問一個passivated的bean時,EJB容器通過將它由磁盤中讀出,從而再次激活該bean。 ejbPassivate 和和 ejbActivate ejbPassivate和ejbActivate方法幫助EJB容器解決了一個問題-你不能串行化某些“活動的”操作系統資源,例如網絡連接等。由于大部分的數據庫連接都需要一個網絡連接,這就意味著不能
44、串行化數據庫連接。 如果你在setSessionContext方法中建立了一個數據庫連接,在EJB容器需要passivate該session bean時,你必須對該連接做一些處理,通常是你應該關閉連接并且設置該連接變量為null。當EJB容器調用ejbActivate方法時,再重新建立連接。 ejbPassivate 和和 ejbActivate 提示 不要錯誤地認為在session bean首次創建時會調用ejbActivate。ejbActivate方法僅在ejbPassivate方法被調用后執行。 12.7 Session Bean要求、限制和許可要求、限制和許可 在EJB規范中,對Se
45、ssion Bean有一些限制和要求。這些限制會指出哪些是bean必須做的,哪些是不能做的,以及bean必須實現的一些方法和接口。也有一些規范指出哪些處理是允許做的,這是為了避免你以為這些處理是被其它限制禁止的。 實現實現SessionBean接口接口session bean必須實現javax.ejb.SessionBean接口聲明該類為Public,而不是Final或者Abstract 要記住EJB容器需要創建bean實例,因此該類必須是public,而且不是抽象的。 創建一個創建一個Pubic,無參數的構造器,無參數的構造器 同樣,由于EJB容器必須創建bean的實例。如果構造器是prot
46、ected或者private,容器就不能創建實例。 不要實現不要實現finalize方法方法 雖然你很少需要定義一個finalize方法,不過EJB規范中還是明文禁止在session和entity bean中定義finalize方法。如果你的bean需要做任何清除的操作,它們應該在ejbRemove或者ejbPassivate方法中進行。 實現實現create方法和所有的方法和所有的remote方法方法 一個session bean必須實現Home接口中指定的全部create方法,以及Remote接口中指定的全部方法。在實現這些方法時,有一些額外的要求:這些方法必須是public,而不能是st
47、atic或者final參數和返回的類型必須是有效的RMI/IIOP返回類型。在通常的情況下,這意味著必須是原有的類型(int, char, double等),可串行化的對象或者Remote接口方法名不能以ejb開頭在必要時擴展其它類在必要時擴展其它類 你的實現類也可以是其它類的子類。實際上,該 超 類 也 可 以 是 另 一 類 b e a n 的 實 現 類 。不要拋出不要拋出RemoteException 如果需要拋出一個和EJB有關的異常,拋出javax.ejb.EJBException代替。 12.8 Remote和和Home接口的限制接口的限制 除了對實現類有限制外,對于Remote
48、和Home接口也有一些限制。這些限制大部分和實現類類似。 Remote接口必須擴展接口必須擴展javax.ejb.EJBObject Remote接口必須擴展javax.ejb.EJBObject Home接口必須擴展接口必須擴展javax.ejb.EJBHome 和Remote接口必須擴展EJBObject一樣,EJBHome接口幫助標識Home接口,并且定義了一些你可以在每個Home接口上調用的方法。 參數和返回類型必須符合參數和返回類型必須符合RMI/IIOP 這意味著它們必須是原始類型、可串行化對象或者Remote接口。 所有的方法必須拋出所有的方法必須拋出java.rmi.Remot
49、eException 由 于 H o m e 和 R e m o t e 接 口 擴 展java.rmi.Remote接口,所以接口的全部方法都必須拋出java.rmi.RemoteException。RMI規范明確規定在Remote接口的所有方法都必須拋出 RemoteException。 所有的方法都必須有相應的實現所有的方法都必須有相應的實現 對于Home接口的每個create方法,在實現類中都必須有一個相應的ejbCreate方法。還有,create方法拋出CreateException。 擴展其它的接口擴展其它的接口 要支持Enterprise JavaBeans的子類,Home和Remote接口可以擴展其它的接口,只要父接口是擴展EJBObject(對于Remote接口)或者EJBHome(對于Home接口)。 創建創建 Session BeanSession Bean 11. 1. 新建一個項目新建一個項目 右擊右擊“包資源管理器包資源管理器”的空白處,的空白處, 在彈出菜單中在彈出菜單中選擇選擇“新建新建” -“Lomboz J2E
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025冰箱清潔服務合同
- 2025辦公室裝修合同書協議
- 2025租賃合同(辦公樓)
- 2025物業管理委托合同模板
- 2025房屋買賣的合同書樣本
- 2024年醫用電子直線加速器項目資金需求報告代可行性研究報告
- 2025版合同范本模板下載
- 纖維基太陽能電池的研究考核試卷
- 2025合同法規管理包含哪些內容
- 《動感十足的》課件
- 鏟車三個月、半年、年保養記錄(新)
- 腦電圖(圖譜)課件
- 給水廠畢業設計正文(全)
- 《概率思想對幾個恒等式的證明(論文)9600字》
- 重金屬冶金學-鈷冶金課件
- 《EBSD數據分析》課件
- 初高中生物銜接課課件
- KET詞匯表(英文中文完整版)
- DBJ61-T 112-2021 高延性混凝土應用技術規程-(高清版)
- JJF(閩)1097-2020總溶解固體(TDS)測定儀校準規范-(現行有效)
- 推拉門定制安裝合同協議書范本
評論
0/150
提交評論