繁体中文
设为首页
加入收藏
当前位置:JSP技术首页 >> J2EE/EJB/服务器 >> EJB核心技术及其应用(再续)

EJB核心技术及其应用(再续)

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:6  文字大小:【】【】【
简介:五、EJB的编程环境: 1、 使用Jbuilder   Jbuilder与EJB Container能够进行无缝连接。Jbuilder和Inprise的应用服务器包括了所有的开发和配置Enterprise Beans的工具以及所需要的库:运行和管理Enterprise Bea...

五、EJB的编程环境:

1、 使用Jbuilder

  Jbuilder与EJB Container能够进行无缝连接。Jbuilder和Inprise的应用服务器包括了所有的开发和配置Enterprise Beans的工具以及所需要的库:运行和管理Enterprise Bean的容器、命名服务、 事务服务、Java数据库、开发Enterprise Beans所需要的API、一个增强的java-to-iiop编译器,支持值类型和RMI信号等等。

  Jbuilder还提供了一个快速开发应用程序Enterprise Beans的工具和向导。通过简单而且直观的步骤,向导帮助你建立一个Enterprise Bean。自己设定某些缺省值,产生了bean的模板,在上面,我们可以增加我们自己的应用逻辑。Jbuilder也提供了一个EJB的接口生成向导。向导在Enterprise Bean的公共方法基础上生成了Remote接口和Home接口。Jbuilder还提供一个配置器的向导帮助我们逐步的建立XML描述器文件。并将生成的Stubs集中到一个jar文件中。

2、使用Jbuilder之外的集成环境:

  如果你使用其它的除了别的集成环境(IDE)。要确定使用了集成环境IDE所带的容器工具。也要验证IDE是否支持EJB规范的相应的版本,还要确定它是否正确的支持EJB的API。

  要确定JD到所支持的EJB容器的版本。可以通过检查Inprise的安装说明来确定EJB容器所支持的支持JDK的版本。

  在配置Enterprise Bean的时候,你必须使用Inprise的应用服务器所提供的工具。这些工具能够编辑和修改第三方的代理商提供的Inprise配置描述器。还能够验证配置描述器,能够验证bean的源代码。

六、一个简单的HELLO例子

1、安装Apusic Application Server

  Note:以下以Linux为例,来说明Apusic Application Server的安装过程。其他平台的安装,可参考Apusic Application Server安装手册。

  下载JDK1.2,Apusic Application Server必须运行在JDK1.2以上环境中。可从以下站点下载最新JDK。

http://java.sun.com

  下载Apusic Application Server

Apusic Application Server 试用版可从以下网址得到:

  http://www.apusic.com/download/enter.jsp

  在下载完成后,你可以得到一个包裹文件apusic.zip,选定安装目录,假设安装到/usr下,则用以下命令:

cd /usr

jar xvf apusic.zip

/usr下会出现一个目录apusic,Apusic Application Server的所有程序都被解压到/usr/apusic下。

将以下路径加入到CLASSPATH中

/usr/apusic/lib/apusic.jar

$JAVA_HOME/lib/tools.jar

用以下命令运行Apusic Application Server

java -Xms64m com.apusic.server.Main -root /usr/apusic

2、定义EJB远程接口(Remote Interface)

  任何一个EJB都是通过Remote Interface被调用,EJB开发者首先要在Remote Interface中定义这个EJB可以被外界调用的所有方法。执行Remote Interface的类由EJB生成工具生成。

  以下是HelloBean的Remote Inteface程序:

package ejb.hello;

import java.rmi.RemoteException;

import java.rmi.Remote;

import javax.ejb.*;

public interface Hello extends EJBObject, Remote {

//this method just get "Hello World" from HelloBean.

public String getHello() throws RemoteException;

}

3、定义Home Interface

  EJB容器通过EJB的Home Interface来创建EJB实例,和Remote Interface一样,执行Home Interface的类由EJB生成工具生成。

以下是HelloBean 的Home Interface程序:

package ejb.hello;

import javax.ejb.*;

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.util.*;

/**

* This interface is extremely simple it declares only

* one create method.

*/

public interface HelloHome extends EJBHome {

public Hello create() throws CreateException,

RemoteException;

}

4、写EJB类

  在EJB类中,编程者必须给出在Remote Interface中定义的远程方法的具体实现。EJB类中还包括一些 EJB规范中定义的必须实现的方法,这些方法都有比较统一的实现模版,编程者只需花费精力在具体业务方法的实现上。

以下是HelloBean的代码:

package ejb.hello;

import javax.ejb.*;

import java.util.*;

import java.rmi.*;

public class HelloBean implements SessionBean {

static final boolean verbose = true;

private transient 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.

*

* @param SessionContext

*/

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");

}

/**

* **** HERE IS THE BUSINESS LOGIC *****

* the getHello just return a "Hello World" string.

*/

public String getHello()

throws RemoteException

{

return("Hello World");

}

}

5、创建ejb-jar.xml文件

  ejb-jar.xml文件是EJB的部署描述文件,包含EJB的各种配置信息,如是有状态Bean(Stateful Bean) 还是无状态Bean(Stateless Bean),交易类型等。ejb-jar.xml文件的详细信息请参阅EJB规范。以下是HelloBean的配置文件:

Hello

ejb.hello.HelloHome

ejb.hello.Hello

ejb.hello.HelloBean

Stateless

Container

Hello

*

Required

6、编译和部署

编译Java源文件并将编译后class和ejb-jar.xml打包到Hello.jar

mkdir build

mkdir build/META-INF

cp ejb-jar.xml build/META-INF

javac -d build *.java

cd build

jar cvf Hello.jar META-INF ejb

cd ..

用EJB工具生成可部署到Apusic Application Server中运行的jar文件:

java com.apusic.ejb.utils.EJBGen -d /usr/apusic/classes/Hello.jar build/Hello.jar

增加/usr/apusic/classes/Hello.jar到CLASSPATH中

将Hello.jar加入到Apusic Application Server配置文件中。在/usr/apusic/config/server.xml 加入以下几行:

classes/Hello.jar

Hello

HelloHome

启动服务器

java -Xms64m com.apusic.server.Main -root /usr/apusic

7、写客户端调用程序

  您可以从Java Client,JSP,Servlet或别的EJB调用HelloBean。

  调用EJB有以下几个步骤:

  通过JNDI(Java Naming Directory Interface)得到EJB Home Interface

  通过EJB Home Interface 创建EJB对象,并得到其Remote Interface

  通过Remote Interface调用EJB方法

以下是一个从Java Client中调用HelloBean的例子:

package ejb.hello;

import javax.naming.Context;

import javax.naming.InitialContext;

import java.util.Hashtable;

import javax.ejb.*;

import java.rmi.RemoteException;

/**

* @author Copyright (c) 2000 by Apusic, Inc. All Rights Reserved.

*/

public class HelloClient{

public static void main(String args[]){

String url = "rmi://localhost:6888";

Context initCtx = null;

HelloHome hellohome = null;

try{

Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,

"com.apusic.jndi.InitialContextFactory");

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.getHello();

System.out.println(s);

}catch(Exception e){

System.out.println(e.getMessage());

System.exit(1);

}

}

}

运行HelloClient,可得到以下输出:

Hello World

责任编辑:admin
相关文章