無狀態會話bean是可以模擬業務過程的組件,它可以在單獨的方法調用中被執行。Stateless session Bean不能夠維持一個調用客戶的狀態,在一個方法調用中,Stateless Session Bean 可以維持調用客戶的狀態,當方法執行完,狀態不會被保持。在調用完成后,Stateless Session Bean被立即釋放到緩沖池中,所以Stateless Session Bean具有很好的伸縮性,可以支持大量用戶的調用。 無狀態會話beans的特點 沒有對話狀態 無狀態會話bean可以擁有內部狀態,它們的狀態不能為非凡的客戶端定制。這意味著所有的無狀態bean對于客戶端是無差別的,客戶端也不能分離它們。客戶端必須將所有的必需的客戶端數據作為業務邏輯方法的參數傳給無狀態bean,無狀態bean可以從外部資源(例如數據庫)獲得所需的數據。 初始化無狀態bean只有一種方法 我們知道會話bean的初始化調用ejbCreate()方法,因為無狀態會話bean不能夠在方法調用之間保留狀態,因此它也不能在客戶端給ejbCreate()調用傳遞數據以后保留狀態。調用不帶參數的ejbCreate()或create()。 容器可以聚集和重用無狀態會話Bean 構建“Hello,World!”遠程接口 package com.wiley.compBooks.roman.session.helloworld; import javax.ejb.*; import java.rmi.RemoteException; import java.rmi.Remote; /** * This is the HelloBean remote interface. * * This interface is what clients Operate on when * they interact with EJB objects. The container * vendor will implement this interface; the * implemented object is the EJB object, which * delegates invocations to the actual bean. */ public interface Hello extends EJBObject { /** * The one method - hello - returns a greeting to the client. */ public String hello() throws java.rmi.RemoteException; } Source 4.1 Hello.java. Hello接口繼續了EJBObject接口,EJBObject繼續Remote接口,因此hello可以拋出rmi異常。 下面建立bean,實現業務方法:hello()。 他實現了javax.ejb.SessionBean接口 package com.wiley.compBooks.roman.session.helloworld; import javax.ejb.*; /** * Demonstration stateless session bean. */ public class HelloBean implements SessionBean { // // EJB-required methods // public void ejbCreate() { System.out. } public void ejbRemove() { System.out.println("ejbRemove()"); } public void ejbActivate() { System.out.println("ejbActivate()"); } public void ejbPassivate() { System.out.println("ejbPassivate()"); } public void setSessionContext(SessionContext ctx) { System.out.println("setSessionContext()"); } // // Business methods // public String hello() { System.out.println("hello()"); return "Hello, World!"; } } Source 4.2 HelloBean.java 注重:不需要實現自己的遠程接口,初始化方法不帶參數。破壞bean時,使用比較簡單的ejbRemove()方法。ejbActivate() 和ejbPassivate()方法不需應用在無狀態會話bean,因此,這兩個方法為空。 建立“Hello,World!”Home接口 Home接口繼續了javax.ejb.EJBHome。Home接口為EJB對象擴展了一個不帶參數的方法——create()方法。 package com.wiley.compBooks.roman.session.helloworld; import javax.ejb.*; import java.rmi.RemoteException; /** * This is the home interface for HelloBean. This interface * is implemented by the EJB Server's glue-code tools - the * implemented object is called the Home Object and serves * as a factory for EJB Objects. * * One create() method is in this Home Interface, which * corresponds to the ejbCreate() method in HelloBean. */ public interface HelloHome extends EJBHome { /* * This method creates the EJB Object. * * @return The newly created EJB Object. */ Hello create() throws RemoteException, CreateException; } creat方法拋出了a java.rmi.RemoteException和aavax.ejb.CreateException.異常。 寫配置描述符 在EJB1.0中,配置描述符是作為文件存儲在磁盤上的java對象。在EJB1.1種,配置描述符是一個xml文檔。EJB容器或IDE環境應該提供生成配置描述符的工具。 配置描述符的設置 bean home的名字 企業級bean類名 home接口類名 遠程接口類名 Re-entrant 狀態或無狀態 會話時間
HelloBean的配置描述符 環境屬性 bean通過使用此信息來適應不同的非凡環境。 Ejb-jar文件 我們需要將我們所需要的文件打包成Ejb-jar文件。 企業級的bean 遠程接口 home接口 配置描述符,包括屬性 以上這些必須被包含進Ejb-jar文件。在EJB1.0中,jar文件理有一個文本文件的列表。它表示jar的具體信息。它用來鑒別哪個企業bean在Ejb-jar文件。在EJB1.1中,XML文件包含了所有的必要信息。 生成Ejb-jar文件 jar cmf ../manifest HelloWorld.jar * 配置bean 最后,我們還需要在Ejb容器中配置bean。經常執行一下步驟: Ejb-jar文件的檢驗 容器工具來產生EJB對象和home對象 容器工具來生成RMI所需的stubs和skeletons 寫無狀態bean的客戶代碼 package com.wiley.compBooks.roman.session.helloworld; import javax.ejb.*; import javax.naming.*; import java.rmi.*; import java.util.Properties; /** * This class is an example of client code that invokes * methods on a simple stateless session bean. */ public class HelloClient { public static void main(String[] args) { try { /* * Get System properties for JNDI initialization */ Properties props = System.getProperties(); /* * Form an initial context */ Context ctx = new InitialContext(props); /* * Get a reference to the home object * (the factory for EJB objects) */ HelloHome home = (HelloHome) ctx.lookup("HelloHome"); /* * Use the factory to create the EJB Object */ Hello hello = home.create(); /* * Call the hello() method, and print it */ System.out.println(hello.hello()); /* * Done with EJB Object, so remove it */ hello.remove(); } catch (Exception e) { e.printStackTrace(); } } } 客戶端代碼執行了一下任務: 定位home接口 使用home接口建立EJB對象 調用EJB對象上的hello() 移走EJB對象 運行 首先運行應用服務器。對于BEA的WebLogic,執行 t3server 客戶端執行: java -Djava.naming.factory.initial= weblogic.jndi.TengahInitialContextFactory -Djava.naming.provider.url= t3://localhost:7001 com.wiley.compBooks.roman.session.helloworld.HelloClient 服務端輸出: setSessionContext() ejbCreate() hello() ejbRemove() 客戶端輸出: Hello, World!