| |
weblogic的研究之开发、部署ejb(1) 这里不会讨论ejb的概念,只讨论如何编写一个简单ejb,部署ejb,weblogic与jbuilder的整合,本文先把介绍仅用文本编辑器编写一个最简单的ejb所需要的一切,以让大家一览ejb的概貌,然后才介绍如何把weblogic与jbuilder整合起来,使用jbuilder开发weblogic的ejb,我相信这样会得到很好的学习效果,因为这是我学习的路径,当然我会把我遇到的问题告诉大家,以免大家走弯路。
下面是一个最简单的ejb所需要的代码及xml说明,手工制作ejb的jar包比较麻烦,在win2000下,我仿照例子制作了一个 build.cmd 批处理文件
weblogic-ejb-jar.xml <?xml version="1.0"?>
<!doctype weblogic-ejb-jar public ´-//bea systems, inc.//dtd weblogic 5.1.0 ejb//en´ ´ ;http://www.bea.com/servers/wls510/dtd/weblogic-ejb-jar.dtd´ ;>
<weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>helloworldbean</ejb-name> <caching-descriptor> <max-beans-in-free-pool>100</max-beans-in-free-pool> </caching-descriptor> <jndi-name>hello.helloworld</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar
-------------------------------------------------------------------------------- ejb-jar.xml <?xml version="1.0" encoding="gbk"?> <!doctype ejb-jar public ´-//sun microsystems, inc.//dtd enterprise javabeans 1.1//en´ ´ ;http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd´ ;>
<ejb-jar> <enterprise-beans> <session> <ejb-name>helloworldbean</ejb-name> <home>hello.helloworldhome</home> <remote>hello.helloworld</remote> <ejb-class>hello.helloworldbean</ejb-class> <session-type>stateless</session-type> <transaction-type>container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>helloworldbean</ejb-name> <method-name>*</method-name> </method> <trans-attribute>required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>
-------------------------------------------------------------------------------- package hello; import java.rmi.*; import javax.ejb.*;
public class helloworldbean implements sessionbean { private sessioncontext sessioncontext; public void ejbcreate() { } public void ejbremove() { } public void ejbactivate() { } public void ejbpassivate() { } public void setsessioncontext(sessioncontext context) { sessioncontext = context; } public string gethelloworld(){ return "hello world!"; } } -------------------------------------------------------------------------------- helloworld.java package hello; import java.rmi.*; import javax.ejb.*;
public interface helloworld extends ejbobject { public java.lang.string gethelloworld() throws remoteexception; } -------------------------------------------------------------------------------- helloworldhome.java package hello; import java.rmi.*; import javax.ejb.*;
public interface helloworldhome extends ejbhome { public helloworld create() throws remoteexception, createexception; } --------------------------------------------------------------------------------
helloworldbean.java package hello; import java.rmi.*; import javax.ejb.*;
public class helloworldbean implements sessionbean { private sessioncontext sessioncontext; public void ejbcreate() { } public void ejbremove() { } public void ejbactivate() { } public void ejbpassivate() { } public void setsessioncontext(sessioncontext context) { sessioncontext = context; } public string gethelloworld(){ return "hello world!"; } } --------------------------------------------------------------------------------
helloworldbeanclient1.java package hello;
import javax.naming.*; import javax.rmi.portableremoteobject;
public class helloworldbeanclient1 { private static final string error_null_remote = "remote interface reference is null. it must be created by calling one of the home interface methods first."; private static final int max_output_line_length = 50; private boolean logging = true; private helloworldhome helloworldhome = null; private helloworld helloworld = null;
/**construct the ejb test client*/ public helloworldbeanclient1() { long starttime = 0; if (logging) { log("initializing bean access."); starttime = system.currenttimemillis(); }
try { //get naming context context ctx = new initialcontext();
//look up jndi name object ref = ctx.lookup("helloworld");
//cast to home interface helloworldhome = (helloworldhome) portableremoteobject.narrow(ref, helloworldhome.class); if (logging) { long endtime = system.currenttimemillis(); log("succeeded initializing bean access."); log("execution time: " + (endtime - starttime) + " ms."); }
helloworld hello=helloworldhome.create(); string str=hello.gethelloworld(); system.out.println(str); } catch(exception e) { if (logging) { log("failed initializing bean access."); } e.printstacktrace(); } } //---------------------------------------------------------------------------- // methods that use home interface methods to generate a remote interface reference //----------------------------------------------------------------------------
public helloworld create() { long starttime = 0; if (logging) { log("calling create()"); starttime = system.currenttimemillis(); } try { helloworld = helloworldhome.create(); if (logging) { long endtime = system.currenttimemillis(); log("succeeded: create()"); log("execution time: " + (endtime - starttime) + " ms."); } } catch(exception e) { if (logging) { log("failed: create()"); } e.printstacktrace(); }
if (logging) { log("return value from create(): " + helloworld + "."); } return helloworld; }
//---------------------------------------------------------------------------- // methods that use remote interface methods to access data through the bean //----------------------------------------------------------------------------
public string gethelloworld() { string returnvalue = ""; if (helloworld == null) { system.out.println("error in gethelloworld(): " + error_null_remote); return returnvalue; } long starttime = 0; if (logging) { log("calling geth
|
|