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 Server's 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 Server's
* 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

