先泛泛而論,講一講EJB的開發步驟。
1.1 SessionBean的開發
第一步,寫遠程接口(remote interface),繼承EJBObject接口,把需要調用的public方法寫在里面(這些方法將在SessionBean中實現),注意要聲明throws
java.rmi.RemoteException。
例如:
| package jsper.ejb; import java.rmi.*; import javax.ejb.*; public interface MyEJB extends EJBObject { public String sayHello() throws java.rmi.RemoteException; } |
第二步,
寫Home接口(生成EJBObject引用的factory) 至少生成一個create方法, 注意要聲明throws java.rmi.RemoteException和javax.ejb.CreateException。
比如:
| package jsper.ejb; import java.rmi.*; import javax.ejb.*; public interface MyEJBHome extends EJBHome { MyEJB create() throws java.rmi.RemoteException, javax.ejb.CreateException; } |
第三步,
寫真正的Session Bean的實現(實現定義在遠程接口中的方法), 需要實現javax.ejb.SessionBean接口
注意:不能用implents
MyEJB的方式直接實現遠程接口,此處不用拋出RemoteException package jsper.ejb;
| import java.rmi.RemoteException; import javax.ejb.*; public class MyEJBClass implements SessionBean { public MyEJBClass() { } //定義在SessionBean 中的方法 public void ejbCreate() throws RemoteException, CreateException { } public void ejbActivate() throws RemoteException { } public void ejbPassivate() throws RemoteException { } public void ejbRemove() throws RemoteException { } public void setSessionContext(SessionContext ctx) throws RemoteException { } //此處是具體的實現 public String sayHello() { System.out.println("Hello"); } } |
第四步,寫一個發布用的配置文件ejb-jar.xml 需要提供的信息:
| Bean Home name -- The nickname that clients use to lookup your bean’s home object. Enterprise bean class name -- The fully qualified name of the enterprise bean class. Home interface class name Remote interface class name Re-entrant -- Whether the enterprise bean allow re-entrant calls. This setting must be false for session beans(it applies to entity beans only) stateful or stateless Session timeout --The length of time (in seconds) before a client should time out when calling methods on your bean. |
最后你還可以提供屬于自己的配置信息供自己控制EJB的工作方式。
例子:
| helloEjb com.jsper.ejb.MyEJBHome com.jsper.ejb.MyEJB com.jsper.ejb.MyEJBClass Stateless Container |
第五步,將你的所有文件用jar工具生成jar文件
ejb-jar.xml須在頂級的META-INF子目錄
這句話比較咬嘴, 舉個例子
| mylib----META-INF--*.XML | |com--coucouniu--ejb---EJBClass |-EJBHome |-EJB 在生成.jar文件時 sh>cd mylib//注意此處所在目錄 sh>jar cv0f myejb.jar * |
請注意:
到這一步我們做出的東西都是和和特定的EJB Server是無關的, 只是和遵循EJB的標準有關
第六步,使用特定平臺的發布工具生成發布使用的jar文件。
不同的中間件產品此步驟非常不同,產生的結果都是生成只有自己的EJB Server能理解的遠程接口和Home接口實現等等東西,打包在一個jar文件中 一般是很簡單的
第七步,把.jar文件發布到EJB
Server
根據不同的中間件產品此步驟非常不同, 可以分為啟動時發布和運行時發布兩種,一般是很簡單的, 以weblogic為例:
1、在weblogic.properties 文件中配置使weblogic 啟動時自動裝載。
添加一個條目比如:
weblogic.ejb.deploy=C:/weblogic510/myserver/ejb_basic_beanManaged.jar,
C:/weblogic510/myserver/ejb_basic_test.jar
2、使用deploy或DeployerTool動態裝載/卸載/更新
第八步,寫客戶端的程序(我迄今為止的理解)
在我們使用發布工具把EJB發布到EJB Container的過程中,會綁定一個名字到Container的目錄服務中,現在我們要調用時從這個目錄服務中把EJBHome對象取出, 這里分為從本地和外部兩種情況:
一種是客戶端本地調用EJB。 比如和EJB引擎和Servlet引擎是整合在同一個Application Server中, 這時當一個Servlet要調用EJB時無須驗證,即可得到EJBHome接口的實現
| Context ic = new InitialContext(); System.out.println("Looking for the EJB published as ’hello’"); com.jsper.ejb.MyEJBHome homeInterface = (com.jsper.ejb.MyEJBHome) ic.lookup(“hello”); //發布時綁定的名字是hello |
這樣就可從目錄服務中得到Home接口的實現, 也是我們最常用的方式, 可移植性很好
外部調用的話首先要經過身份驗證,
比如Oracle8i :
| String ejbUrl = "sess_iiop://localhost:2481:ORCL/test/MyEJB"; String username = "scott"; String password = "tiger"; // Setup the environment Hashtable environment = new Hashtable(); // Tell JNDI to speak sess_iiop environment.put(javax.naming.Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); // Tell sess_iiop who the user is environment.put(Context.SECURITY_PRINCIPAL, username); // Tell sess_iiop what the password is environment.put(Context.SECURITY_CREDENTIALS, password); // Tell sess_iiop to use credential authentication environment.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN); // Lookup the URL com.jsper.ejb.MyEJBHome homeInterface = null; try { System.out.println("Creating an initial context"); Context ic = new InitialContext(environment); System.out.println("Looking for the EJB published as ’test/MyEJB’"); homeInterface = (com.jsper.ejb.MyEJBHome) ic.lookup(ejbUrl); } catch (ActivationException e) { System.out.println("Unable to activate : " + e.getMessage()); e.printStackTrace(); System.exit(1); } 再比如weblogic的調用方式: try { // Get an InitialContext String url="t3://localhost:7001"; Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, url); Context ctx =new InitialContext(h); System.out.println("Getting the EJBHome object…"); com.jsper.ejb.EJBHome tmp= (com.jsper.ejb.EJBHome)ctx.lookup("hello"); //create three element array of COUNT object EJB ejb =tmp.create(); System.out.println(ejb.sayHello()); } catch(Exception e) { e.printStackTrace(); } 由于和具體的目錄服務、協議相關,為了達到可移植的目的, 只好多做一些工作,幸好一般不需要做這些工作。 /* |