EJB2.0版本的HelloWorld
 
虽然EJB3.1已经出来了,可是EJB2.0的项目还需要维护啊。下面写个简单EJB2.0的HelloWorld程序,练练手。
 
环境:
JBoss 4.0.2
JDK1.5
IDEA8.1.4
 
一、开发EJB组件
 
1、开发Bean类
package lavasoft.testejb20;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
* Bean类
* 所有具体的业务逻辑都在此类里面,此类不抛出远程异常
*
* @author leizhimin 2010-3-30 9:06:57
*/

public class HelloBean implements SessionBean {
        /**
         * 业务方法
         *
         * @param someOne
         * @return Hello的消息
         */

        public String sayHello(String someOne) {
                System.out.println("HelloBean: sayHello() is called!");
                return "Hello, " + someOne + "!";
        }

        /**
         * 必须有这个方法,这是EJB的一条原则,这个方法不是来自SessionBean接口的
         */

        public void ejbCreate() {
                System.out.println("HelloBean: ejbCreate() is called!");
        }

        //-------下面四个方法是来自SessionBean的,必须写出来,但没啥用-------
        public void ejbActivate() {
                System.out.println("HelloBean: ejbActivate() is called!");
        }

        public void ejbPassivate() {
                System.out.println("HelloBean: ejbPassivate() is called!");
        }

        public void ejbRemove() {
                System.out.println("HelloBean: ejbRemove() is called!");
        }

        public void setSessionContext(SessionContext sessionContext) {
                System.out.println("HelloBean: setSessionContext() is called!");
        }
}
 
2、组件接口
 
package lavasoft.testejb20;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

/**
* 组件接口
* 所有的业务方法都要在此接口中声明
*
* @author leizhimin 2010-3-30 9:10:32
*/

public interface Hello extends EJBObject {
        /**
         * 业务方法,组件接口中的业务方法必须抛出RemoteException
         *
         * @param someOne
         * @return
         * @throws RemoteException
         */

        public String sayHello(String someOne) throws RemoteException;
}
 
3、Home接口
 
package lavasoft.testejb20;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;

/**
* Home接口
* Home接口必须扩展EJBHome或者EJBLocalHome接口
* 客户使用Home接口来请求组件接口的一个引用
* 可以将Home接口看做是一个工厂,它能制造Bean的引用个,而且能向客户分配bean引用
*
* @author leizhimin 2010-3-30 9:12:05
*/

public interface HelloHome extends EJBHome {
        public Hello create() throws CreateException, RemoteException;
}
 
4、ejb-jar.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
                'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
        <display-name>Hello EJB</display-name>
        <enterprise-beans>
                <session>
                        <display-name>helloEJB</display-name>
                        <ejb-name>helloEJB</ejb-name>
                        <home>lavasoft.testejb20.HelloHome</home>
                        <remote>lavasoft.testejb20.Hello</remote>
                        <ejb-class>lavasoft.testejb20.HelloBean</ejb-class>
                        <session-type>Stateless</session-type>
                        <transaction-type>Bean</transaction-type>
                        <security-identity>
                                <description></description>
                                <use-caller-identity></use-caller-identity>
                        </security-identity>
                </session>
        </enterprise-beans>
</ejb-jar>
 
5、jboss.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
        <enterprise-beans>
                <session>
                        <ejb-name>helloEJB</ejb-name>
                        <jndi-name>ejb/helloEJB</jndi-name>
                </session>
        </enterprise-beans>
</jboss>
 
6、编译打包
 
C:\test\testejb20\out\exploded\testejb20EJB>jar -cvf ../helloejb.jar .
标明清单(manifest)
增加:lavasoft/(读入= 0) (写出= 0)(存储了 0%)
增加:lavasoft/testejb20/(读入= 0) (写出= 0)(存储了 0%)
增加:lavasoft/testejb20/Hello.class(读入= 250) (写出= 183)(压缩了 26%)
增加:lavasoft/testejb20/HelloBean.class(读入= 1455) (写出= 670)(压缩了 53%)
增加:lavasoft/testejb20/HelloHome.class(读入= 277) (写出= 200)(压缩了 27%)
增加:lavasoft/client/(读入= 0) (写出= 0)(存储了 0%)
增加:lavasoft/client/TestClient.class(读入= 1367) (写出= 701)(压缩了 48%)
忽略项 META-INF/
增加:META-INF/ejb-jar.xml(读入= 882) (写出= 365)(压缩了 58%)
增加:META-INF/jboss.xml(读入= 237) (写出= 130)(压缩了 45%)
 
7、部署EJB
解压缩jboss-4.0.2.zip到一个目录下面,然后将打包后的helloejb.jar组件复制到jboss-4.0.2\server\default\deploy目录下面。
 
8、启动jboss
运行jboss-4.0.2\bin\run.bat文件,启动服务器
 
 
二、开发EJB客户端调用代码
 
package lavasoft.client;

import lavasoft.testejb20.Hello;
import lavasoft.testejb20.HelloHome;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Properties;

/**
* 客户端代码
* 客户端会依赖组件接口和Home接口
*
* @author leizhimin 2010-3-30 10:22:50
*/

public class TestClient {
        public static void main(String[] args) throws NamingException, CreateException, RemoteException {
                Properties props = new Properties();
                props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                props.setProperty("java.naming.provider.url", "localhost:1099");
                //初始化JNDI上下文环境,因为客户端不知道JBOSS的环境
                InitialContext ctx = new InitialContext(props);
                //检索指定的对象
                Object objref = ctx.lookup("ejb/helloEJB");
                //强制转换为所需类型的对象
                HelloHome home = (HelloHome) PortableRemoteObject.narrow(objref, HelloHome.class);
                //通过home对象创建一个组件接口对象
                Hello hello = home.create();
                //通过组件接口对象调用业务方法
                String msg = hello.sayHello("ejb");
                System.out.println(msg);
        }
}
 
调用结果:

jboss控制台输出消息:
 
再调用一次看看服务端会做什么:
 
 
注意:客户端程序依赖JBoss应用服务器下的一些库文件,这些文件存在于:jboss-4.0.2\client下面,供客户端程序来使用。