JBoss技術支持文檔(二)
1、下載例子源程序
所有例子的源代碼可以直接從 www.jboss.org進行下載。下載完后放在一個目錄下。下載網址:http://www.jboss.org/docs/manual/files/documentation-example.zip
1.1 建立 BEAN
此節主要是建立一個簡單的EJB,可以查看代碼,這個“Interest”例子,是一個簡單無狀態的會話EJB。它的目的是根據說明的利息率,來對借的所有錢計算利息。實際上在整個包代碼中只有一行功能。
1.2 回顧EJBs
在我們查看代碼之前,我們先對EJB進行復習一下。在EJB最小類型,也必須有三個類:remote interface, home interface和bean實現類。
remote interface是會把EJB中方法提供給外邊世界,讓外邊的代碼來進行調用,在這個例子中類名稱是org.jboss.interest.Interrest。
home interface是管理remote interface類的類。包括建立、刪除等操作。在這個例子中類名稱是org.jboss.interest.InterrestHome。
bean實現類提供home interface和remote interface所有方法的實現。在這個例子中類名稱是org.jboss.interest.InterrestBean。
當然一個Bean可能還包括其他類,甚至其他包。但是必須有此三個類,其他類是在此三個類之上建立的。所有類被打包進一個JAR文件,此文件是用一個目錄結構來反映出包的層次關系。在此例子中所有類都打包在org.jboss.interest包中,所以他們需要在目錄org/jboss/interest/下。
在包含所有類的jar文件建立之前,必須有一個META-INF目錄。此目錄存放了部署描述符(通常叫“ejb-jar.xml”),和可選的其他XML文件。這些文件告訴服務器關于應用明確服務信息。對于JBoss 來講,文件名必須叫“jboss.xml”。
創建jar文件后部署到JBoss Server上。在客戶端你需要一個jndi.properties文件,此文件告訴你的客戶端程序從哪里初始化查找JNDI 命名服務。從這個服務,客戶端將查找到Interest bean,并且返回bean的home interface。home interface用來得到bean的一個remote interface。它可以用遠程接口來訪問bean提供的商業方法。
1.3 EJB類
我們需要三個類:remote interface, home interface 和bean實現類。remote interface遠程接口類,文件名Interest.java。代碼如下:
package org.jboss.docs.interest; import javax.ejb.EJBObject; import java.rmi.RemoteException; /** This interface defines the `Remote' interface for the `Interest' EJB. Its single method is the only method exposed to the outside world. The class InterestBean implements the method. */ public interface Interest extends EJBObject { /** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ public double calculateCompoundInterest(double principle, double rate, double periods) throws RemoteException; } |
遠程接口只有一個商業方法calculateCompoundInterest。Home interface 文件名InterestHome.java。代碼如下:
package org.jboss.docs.interest; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; /** This interface defines the 'home' interface for the 'Interest' EJB. */ public interface InterestHome extends EJBHome { /** Creates an instance of the `InterestBean' class on the server, and returns a remote reference to an Interest interface on the client. */ Interest create() throws RemoteException, CreateException; } |
最后我們給出bean實現類,文件名稱:InterestBean.java。代碼如下:
package org.jboss.docs.interest; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; /** This class contains the implementation for the 'calculateCompoundInterest' method exposed by this Bean. It includes empty method bodies for the methods prescribe by the SessionBean interface; these don't need to do anything in this simple example. */public class InterestBean implements SessionBean { /** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */ public double calculateCompoundInterest(double principle, double rate, double periods) { System.out.println("Someone called `calculateCompoundInterest!'"); return principle * Math.pow(1+rate, periods) - principle; } /** Empty method body */ public void ejbCreate() {} /** Every ejbCreate() method ALWAYS needs a corresponding ejbPostCreate() method with exactly the same parameter types. */ public void ejbPostCreate() {} /** Empty method body */ public void ejbRemove() {} /** Empty method body */ public void ejbActivate() {} /** Empty method body */ public void ejbPassivate() {} /** Empty method body */ public void setSessionContext(SessionContext sc) {} } |
注意大部分方法是空的。因為這些方法在SessionBean接口中說明,所以必須在InterestBean中存在,但在這個簡單例子中,不需要具體內容。