|
会话EJB完整开发过程(以weblogic为服务器)
发表日期:2007-12-23
|
开发步骤(操作界面的图片不能贴上,见谅!)
开发运行环境:j2eesdk1.4+weblogic8.1 说明:本试验已开发一个会话EJB为例,系统采用的应用服务器为weblogic8.1 1、编写bean代码(代码的目录在c:ejbhello下) ① 定义Home Interface EJB容器通过EJB的Home Interface来创建EJB实例,和Remote Interface一样,执行Home Interface的类由EJB生成工具生成。代码如下: package ejb.hello; import Javax.ejb.*; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.*; /** *只定义create方法 */ public interface HelloHome extends EJBHome {
public Hello create() throws CreateException, RemoteException; } ②定义EJB远程接口(Remote Interface) 任何一个EJB都是通过Remote Interface被调用,首先要在Remote Interface中定义这个EJB可以被外界调用的所有方法。执行Remote Interface的类由EJB生成工具生成,试验的代码如下: package ejb.hello;
import java.rmi.RemoteException; import java.rmi.Remote; import javax.ejb.*;
public interface Hello extends EJBObject, Remote {
//定义供远程调用的业务逻辑方法 public String getHello() throws RemoteException; public String getStr() throws RemoteException; } ③ EJB类的实现 在EJB类中,必须给出在Remote Interface中定义的远程方法的具体实现。EJB类中还包括一些EJB规范中定义的必须实现的方法,这些方法都有比较统一的实现模版,只需花费精力在具体业务方法的实现上。试验的代码如下: package ejb.hello; import javax.ejb.*; import java.util.*; import java.rmi.*; //类的实现 public class HelloBean implements SessionBean { static final boolean verbose = true; private SessionContext ctx; // Implement the methods in the SessionBean // interface public void ejbActivate() { if (verbose) System.out.println("ejbActivate called"); } public void ejbRemove() { if (verbose) System.out.println("ejbRemove called"); } public void ejbPassivate() { if (verbose) System.out.println("ejbPassivate called"); } //Sets the session context. public void setSessionContext(SessionContext ctx) { if (verbose) System.out.println("setSessionContext called"); this.ctx = ctx; } /** * This method corresponds to the create method in * the home interface HelloHome.java. * The parameter sets of the two methods are * identical. When the client calls * HelloHome.create(), the container allocates an * instance of the EJBean and calls ejbCreate(). */ public void ejbCreate () { if (verbose) System.out.println("ejbCreate called"); } //以下业务逻辑的实现 public String getStr() throws RemoteException { return("...My First EJB Test??Lenper_xie...!"); } } ④会话Bean的代码完成后,编写客户端,代码如下: package ejb.hello; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Hashtable; import javax.ejb.*; import java.rmi.RemoteException;
/** *用weblogic */ public class HelloClient{ public static void main(String args[]){ String url = "t3://localhost:7001"; Context initCtx = null; HelloHome hellohome = null; try{ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); env.put(Context.PROVIDER_URL, url); initCtx = new InitialContext(env); }catch(Exception e){ System.out.println("Cannot get initial context: " + e.getMessage()); System.exit(1); } try{ hellohome = (HelloHome)initCtx.lookup("HelloHome"); Hello hello = hellohome.create(); String s = hello.getStr(); System.out.println(s); }catch(Exception e){ System.out.println(e.getMessage()); System.exit(1); } } }
2、将代码进行编译 先在c:ejbhello目录下建一个目录build,然后执行编译命令如下: javac ?Cd build *.java //-d build 表示编译后的class放到build目录下 编译完之后会在build建立包的文件夹。
3、创建ejb-jar.XML部署描述文件 ejb-jar.xml文件是EJB的部署描述文件,包含EJB的各种配置信息,如是有状态Bean(Stateful Bean) 还是无状态Bean(Stateless Bean),交易类型等。以下是HelloBean的配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar> <description>Sidney.Xie EJB Test example</description> <display-name>>Sidney.Xie EJBTest</display-name> <small-icon></small-icon> <large-icon></large-icon> <enterprise-beans> <session> <ejb-name>Hello</ejb-name> <home>ejb.hello.HelloHome</home> <remote>ejb.hello.Hello</remote> <ejb-class>ejb.hello.HelloBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>Hello</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor>
</ejb-jar>
4、创建weblogic-ejb-jar.xml文件 此文件适用于weblogic部署是用的 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd"> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>Hello</ejb-name> <jndi-name>HelloHome</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar>
5、创建jar文件用于部署到服务器上 在创建jar文件之前要将文件的目录设定好,首先在ejbhellouild 建立一子目录META-INF,将weblogic-ejb-jar.xml文件和ejb-jar.xml要放到该目录下,然后制作jar文件.命令如下: jar cvf Hello2.jar META-INF ejb //将META-INF和包ejb中的文件打包
6、创建能部署到weblogic上的jar文件 由于不同的厂商的应用服务器有不同的机制,所以要分别制作个服务器所识别的jar文件,试验中使weblogic,使用以下命令: java weblogic.ejbc -complier javac buildHello2.jar buildHello.jar
7、部署EJB到weblogic 首先要启动weblogic服务,然后在浏览器中输入http://localhost:7001/console,打开网页后输入帐号和密码进入weblogic的控制台,在控制台中可以部署EJB,画面如下: 进入部署页面后根据提示将Hello.jar文件部署到服务器上去,会有显示部署成功。 8、测试结果 在命令行执行client端程序来测试,进入目录build下,执行命令java ejb.hello.HelloClient
(出处:)
|
|
上一篇:索爱MIDP 2.0手机的系统字体特效制作
人气:736
下一篇:j2me最佳联网方案终结版
人气:758 |
浏览全部J2EE/J2ME的内容
Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐
|
|