ejb 起步手记
简介
肯定有许多朋友和我一样,刚开始学j2ee的ejb时看了许多介绍,定义,和对它种种优势的描述,看得半懂不懂,迷迷糊糊。却始终不知道该从何处入手学习ejb,今天我们就来一个最简单的helloworld,是大家对j2ee的框架,ejb的基本运行环境,过程,部署有一个最基本的概念。使以后能知道怎么实践学习ejb。本文只适合那些没有真正接触ejb的初级读者,其他的人就不要浪费宝贵的时间了。
事先准备
首先得有一个j2ee的运行环境,现在许多app server都支持,而且配置起来也不难,我们这里是用国产的apusic1.2(可以去www.apusic.com下载,12m左右),它能全面支持ejb2.0标准,安装使用方便,全中文帮助对于我们的学习,理解更加容易。如果安装过程没出什么错,启动服务器,进入http://localhost:6888/你应该能看到它的欢迎页面,那么恭喜你,你已经有了j2ee的运行环境了。最好你还的有jdk和j2ee包,为了使设置环境简单一些,建议把所有的附加包.jar文件都放在java_home\jre\lib\ext内,这样jvm会自动寻找所需的类库而不用设置长长的classpath。这样我们就可以开始进入ejb了。
建立一个ejb部件的整个过程
我们在这里仅仅是为了向大家展示如何构建,因此只是一个很简单的ejb(stateless sessionbean),有一个hello方法,返回"hello, world!"。首先把所有步骤列出来,使大家有一个清晰的思路:
- 编写所有的.java文件:各种组件接口,home接口和组件类。编译成.class文件,这一过程与其他java程序没有区别,只是引用的包不同。
- 正确启动服务器(如果没启动的话),启动部署工具,这个会很慢,特别是机器不太好的朋友一定要耐心等候,会让你输入用户名和密码,默认都是admin,
- 新建一个空白工程,随便起个名字hello,然后选择存放路径,建议放在apusic_home\application下新建一个目录例如hello,完成。
- 把属性卡最下面的"产生application-client-jar"打勾,其他的各项可以默认就行了。
- 右键点击工程图表,选择“添加一个ejb-jar模块”,选择添加新的空的模块,随便起个名字hello。
- 右键点击工程图表,选择“添加一个ejb-jar”,下一步,再将所有编译好的.class文件添加进来,一切按照默认就行了,记下jndi的名称,待会测试需要。
- 部署,查看部署信息,检查是否出错。出错会去检查各步是否有错。否则o.k.
- 把生成的xxx-client.jar中的xxxstub.class文件解压到你开始的编译目录,建立客户端测试环境,准备测试。
- 编写测试.java文件,编译,运行,正确的话会得到"hello, world!",查看服务器的log还可以看到服务器端的过程
看上去似乎很复杂,但实际上大部分的步骤是在wizard的提示下进行的,而且apusic做得很智能,几乎不需要做任何修改。最重要的还是建立自己的bean。
编写必须的接口
我把整个框架都画出来,看图
这里我真正需要写的只有第三层,图中我已经定义了各种接口: hellolocal.java
package examples;
/** * this is the hellobean local interface.
* * this interface is what local clients operate
* on when they interact with ejb local objects.
* the container vendor will implement this
* interface; the implemented object is the
* ejb local object, which delegates invocations
* to the actual bean. */
public interface hellolocal extends javax.ejb.ejblocalobject{
/**
* the one method - hello - returns a greeting to the client.
*/ public string hello();
}
hello.java
package examples;
/**
* this is the hellobean remote interface. *
* this interface is what clients operate on when
* they interact with ejb objects. the container
* vendor will implement this interface; the
* implemented object is the ejb object, which
* delegates invocations to the actual bean.
*/
public interface hello extends javax.ejb.ejbobject{
/**
* the one method - hello - returns a greeting to the client.
*/
public string hello() throws java.rmi.remoteexception;
}
hellohome.java
package examples;
/**
* this is the home interface for hellobean. this interface
* is implemented by the ejb servers tools - the
* implemented object is called the home object, and serves
* as a factory for ejb objects.
* * one create() method is in this home interface, which
* corresponds to the ejbcreate() method in hellobean.
*/
public interface hellohome extends javax.ejb.ejbhome{
/*
* this method creates the ejb object. *
* @return the newly created ejb object.
*/
hello create() throws java.rmi.remoteexception,
javax.ejb.createexception;
}
package examples;
/** * this is the local home interface for hellobean.
* this interface is implemented by the ejb servers
* tools - the implemented object is called the
* local home object, and serves as a factory for
* ejb local objects.
*/
public interface hellolocalhome extends javax.ejb.ejblocalhome{
/* * this method creates the ejb object.
* * @return the newly created ejb object.
*/
hellolocal create() throws javax.ejb.createexception;
}
接下来我们就要实现我们的核心类:hellobean,它非常的简单,只有一个真正的商业逻辑的方法:hello.
package examples;
/** * demonstration stateless session bean.
*/
public class hellobean implements javax.ejb.sessionbean {
private javax.ejb.sessioncontext ctx;
//
// ejb-required methods
//
public void ejbcreate() {
system.out.println("ejbcreate()");
}
public void ejbremove() {
system.out.println("ejbremove()");
}
public void ejbactivate() {
system.out.println("ejbactivate()");
}
public void ejbpassivate() {
system.out.println("ejbpassivate()");
}
public void setsessioncontext(javax.ejb.sessioncontext ctx) {
this.ctx = ctx; }
//
// business methods
//
public string hello() {
system.out.println("hello()");
return "hello, world!";
}
}
将这些类都编译好后,按上面提到的步骤部署好,系统自动就会产生第四层的对象和rmi所需的stub和keleton。把客户端所需的stub放入classpath,我们就可以通过rmi访问ejb了,现在开始写一个测试类。
访问ejb的步骤
- 寻找 (look up) home对象
- 利用home对象创建一个ejb对象
- 调用ejb对象的商业方法
- 清除创建的ejb对象
寻找是通过 java naming and directory interface (jndi),apusic是支持的,我们只需要利用部署时得到的jndi名称就可以访问ejb对象了,如果你没有记住这个名字,可以查看hello/meta-inf/apusic-application.xml中jndi-name的值就知道了。我这里是ejb/hellobean.
package examples;
import javax.naming.context;
import javax.naming.initialcontext;
import java.util.properties;
/** * this class is an example of client code which invokes
* methods on a simple stateless session bean.
*/
public class helloclient {
public static void main(string[] args) throws exception {
/* * setup properties for jndi initialization.
* * these properties will be read-in from
* the command-line. */
properties props = system.getproperties();
/* * obtain the jndi initial context.
* * the initial context is a starting point for
* connecting to a jndi tree. we choose our jndi
* driver, the network location of the server, etc
* by passing in the environment properties.
*/
context ctx = new initialcontext(props);
/* * get a reference to the home object - the
* factory for hello ejb objects
*/
object obj = ctx.lookup("ejb/hellobean");
/* * home objects are rmi-iiop objects, and so
* they must be cast into rmi-iiop objects
* using a special rmi-iiop cast. *
* see appendix x for more details on this.
*/
hellohome home = (hellohome) javax.rmi.portableremoteobject.narrow(
obj, hellohome.class);
/* * use the factory to create the hello ejb object */
hello hello = home.create();
/* * call the hello() method on the ejb object. the
* ejb object will delegate the call to the bean,
* receive the result, and return it to us.
* * we then print the result to the screen. */
system.out.println(hello.hello());
/* * done with ejb object, so remove it.
* the container will destroy the ejb object.
*/
hello.remove();
}
}
编译执行你可以得到"hello, world!",大功告成。
再说点:)
其实标准的过程不是这样的,应该是
- 编写.java,编译
- 编写标准的deployment descriptor即ejb-jar.xml,目的是定义各类接口和实现的bean
- 编写server特定的deployment descriptor, 这里是apusic-application.xml,因为ejb-jar毕竟不能包含所有的东西,这个得看服务器的帮助文件。
- 生成ejb部件.jar文件,我们一般利用jar工具 jar cf xxx.jar * 记住.xml的放在meta-inf/下。但在windows下有些server因文件名大小写会有问题。
- 部署,直接添加这个.jar即文成。
不过,我还是推荐使用前一种,使用简单,当然要有相应的工具才行。
后记
首先我得申明:这个程序不是我写的,但图是我画的,没有工具,我是用excel和画图完成的。我很菜,也是第一次接触ejb,不懂的很多,只是与你分享我的学习过程。
joachimz@sina.com
