附录:一个具体而微的ejb应用实例?
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
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器