您现在的位置是: 软件 > 开发者网络 > 程序方舟 > Java专栏 > 正文


-Win xp中的多种网络
-试验试验试验试验
-用Freehand实现位图矢量化
-网络电话面面观

EJB核心技术及其应用
2001-07-11· ·--··出自jdeveloper

上一页  1 2 3  


  六、一个简单的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 {

file://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的配置文件:

<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems Inc.//DTD Enterprise JavaBeans 1.2//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_1_2.dtd">
<ejb-jar>
<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>


  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 加入以下几行:


<module>
<ejb>
<ejb-uri>classes/Hello.jar</ejb-uri>
<bean>
<ejb-name>Hello</ejb-name>
<jndi-name>HelloHome</jndi-name>
</bean>
</ejb>
</module>

  启动服务器

  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


上一页  1 2 3  

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 优化Java applets 加载过程
 爪哇语言单态创立性模式介绍
 充分利用 EJB 使移动工作群体变得强大
 Enterprise JavaBeans组件慨述之一
 用JavaMail显示复合邮件的内容
 利用UML为Java Servlets和JSP建模
 无状态Enterprise JavaBeans的观点
 EJB的专用术语解释
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!