無狀態(tài)會(huì)話bean是可以模擬業(yè)務(wù)過程的組件,它可以在單獨(dú)的方法調(diào)用中被執(zhí)行。Stateless session Bean不能夠維持一個(gè)調(diào)用客戶的狀態(tài),在一個(gè)方法調(diào)用中,Stateless Session Bean 可以維持調(diào)用客戶的狀態(tài),當(dāng)方法執(zhí)行完,狀態(tài)不會(huì)被保持。在調(diào)用完成后,Stateless Session Bean被立即釋放到緩沖池中,所以Stateless Session Bean具有很好的伸縮性,可以支持大量用戶的調(diào)用。 無狀態(tài)會(huì)話beans的特點(diǎn) 沒有對話狀態(tài) 無狀態(tài)會(huì)話bean可以擁有內(nèi)部狀態(tài),它們的狀態(tài)不能為非凡的客戶端定制。這意味著所有的無狀態(tài)bean對于客戶端是無差別的,客戶端也不能分離它們。客戶端必須將所有的必需的客戶端數(shù)據(jù)作為業(yè)務(wù)邏輯方法的參數(shù)傳給無狀態(tài)bean,無狀態(tài)bean可以從外部資源(例如數(shù)據(jù)庫)獲得所需的數(shù)據(jù)。 初始化無狀態(tài)bean只有一種方法 我們知道會(huì)話bean的初始化調(diào)用ejbCreate()方法,因?yàn)闊o狀態(tài)會(huì)話bean不能夠在方法調(diào)用之間保留狀態(tài),因此它也不能在客戶端給ejbCreate()調(diào)用傳遞數(shù)據(jù)以后保留狀態(tài)。調(diào)用不帶參數(shù)的ejbCreate()或create()。 容器可以聚集和重用無狀態(tài)會(huì)話Bean 構(gòu)建“Hello,World!”遠(yuǎn)程接口 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接口繼續(xù)了EJBObject接口,EJBObject繼續(xù)Remote接口,因此hello可以拋出rmi異常。 下面建立bean,實(shí)現(xiàn)業(yè)務(wù)方法:hello()。 他實(shí)現(xiàn)了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 注重:不需要實(shí)現(xiàn)自己的遠(yuǎn)程接口,初始化方法不帶參數(shù)。破壞bean時(shí),使用比較簡單的ejbRemove()方法。ejbActivate() 和ejbPassivate()方法不需應(yīng)用在無狀態(tài)會(huì)話bean,因此,這兩個(gè)方法為空。 建立“Hello,World!”Home接口 Home接口繼續(xù)了javax.ejb.EJBHome。Home接口為EJB對象擴(kuò)展了一個(gè)不帶參數(shù)的方法——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中,配置描述符是作為文件存儲(chǔ)在磁盤上的java對象。在EJB1.1種,配置描述符是一個(gè)xml文檔。EJB容器或IDE環(huán)境應(yīng)該提供生成配置描述符的工具。 配置描述符的設(shè)置 bean home的名字 企業(yè)級bean類名 home接口類名 遠(yuǎn)程接口類名 Re-entrant 狀態(tài)或無狀態(tài) 會(huì)話時(shí)間
HelloBean的配置描述符 環(huán)境屬性 bean通過使用此信息來適應(yīng)不同的非凡環(huán)境。 Ejb-jar文件 我們需要將我們所需要的文件打包成Ejb-jar文件。 企業(yè)級的bean 遠(yuǎn)程接口 home接口 配置描述符,包括屬性 以上這些必須被包含進(jìn)Ejb-jar文件。在EJB1.0中,jar文件理有一個(gè)文本文件的列表。它表示jar的具體信息。它用來鑒別哪個(gè)企業(yè)bean在Ejb-jar文件。在EJB1.1中,XML文件包含了所有的必要信息。 生成Ejb-jar文件 jar cmf ../manifest HelloWorld.jar * 配置bean 最后,我們還需要在Ejb容器中配置bean。經(jīng)常執(zhí)行一下步驟: Ejb-jar文件的檢驗(yàn) 容器工具來產(chǎn)生EJB對象和home對象 容器工具來生成RMI所需的stubs和skeletons 寫無狀態(tài)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(); } } } 客戶端代碼執(zhí)行了一下任務(wù): 定位home接口 使用home接口建立EJB對象 調(diào)用EJB對象上的hello() 移走EJB對象 運(yùn)行 首先運(yùn)行應(yīng)用服務(wù)器。對于BEA的WebLogic,執(zhí)行 t3server 客戶端執(zhí)行: java -Djava.naming.factory.initial= weblogic.jndi.TengahInitialContextFactory -Djava.naming.provider.url= t3://localhost:7001 com.wiley.compBooks.roman.session.helloworld.HelloClient 服務(wù)端輸出: setSessionContext() ejbCreate() hello() ejbRemove() 客戶端輸出: Hello, World!