EJB2.0有状态SessionBean
 
环境:
JBoss 4.0.2
JDK1.5
IDEA8.1.4
 
一、EJB开发
 
package lavasoft.testejb20yzt;

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

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

public class HelloStatefulBean implements SessionBean {
         private SessionContext context;
         private String someOne;

         /**
         * 业务方法
         *
         * @return Hello的消息
         */

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

         /**
         * 必须有这个方法,这是EJB的一条原则,这个方法不是来自SessionBean接口的
         * 这只能是一个有状态的bean,因为无状态的bean除了无参数的create方法外,不能有其他的创建方法
         */

         public void ejbCreate(String someOne) {
                System.out.println( "HelloStatefulBean: ejbCreate(String someOne) is called!");
                 this.someOne = someOne;
        }

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

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

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

         public void setSessionContext(SessionContext sessionContext) {
                System.out.println( "HelloStatefulBean: setSessionContext() is called!");
                context = sessionContext;
        }
}
 
package lavasoft.testejb20yzt;

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

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

public interface HelloStateful extends EJBObject {
         /**
         * 业务方法,组件接口中的业务方法必须抛出RemoteException
         *
         * @return
         * @throws java.rmi.RemoteException
         */

         public String sayHello() throws RemoteException;
}
EJB2.0有状态SessionBean_第1张图片
 
 
package lavasoft.testejb20yzt;

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

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

public interface HelloStatefulHome extends EJBHome {
         public HelloStateful create(String someOne) throws CreateException, RemoteException;
}
EJB2.0有状态SessionBean_第2张图片
 
xml version ="1.0" encoding ="UTF-8" ?>
                'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
< ejb-jar >
         < display-name >Hello Stateful EJB display-name >
         < enterprise-beans >
                 < session >
                         < display-name >helloStatefulEJB display-name >
                         < ejb-name >helloStatefulEJB ejb-name >
                         < home >lavasoft.testejb20yzt.HelloStatefulHome home >
                         < remote >lavasoft.testejb20yzt.HelloStateful remote >
                         < ejb-class >lavasoft.testejb20yzt.HelloStatefulBean ejb-class >
                         < session-type >Stateful session-type >
                         < transaction-type >Bean transaction-type >
                         < security-identity >
                                 < description > description >
                                 < use-caller-identity > use-caller-identity >
                         security-identity >
                 session >
         enterprise-beans >
ejb-jar >
 
xml version ="1.0" encoding ="UTF-8" ?>
< jboss >
         < enterprise-beans >
                 < session >
                         < ejb-name >helloStatefulEJB ejb-name >
                         < jndi-name >ejb/helloStatefulEJB jndi-name >
                 session >
         enterprise-beans >
jboss >
 
 
二、客户端开发
 
package lavasoft.client;

import lavasoft.testejb20yzt.HelloStateful;
import lavasoft.testejb20yzt.HelloStatefulHome;

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-31 15:54:20
*/

public class TestStatefulSessionBeanClient {
         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/helloStatefulEJB");
                 //强制转换为所需类型的对象
                HelloStatefulHome home = (HelloStatefulHome) PortableRemoteObject.narrow(objref, HelloStatefulHome. class);
                 //通过home对象创建一个组件接口对象
                HelloStateful hello = home.create( "StatefulEJB");
                 //通过组件接口对象调用业务方法
                String msg = hello.sayHello();
                System.out.println(msg);
        }
}
 
客户端调用结果:
EJB2.0有状态SessionBean_第3张图片
 
jboss控制台输出:
EJB2.0有状态SessionBean_第4张图片
 
 
在此,没有使用本地接口,本地接口规则如下:
EJB2.0有状态SessionBean_第5张图片