環境:
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;
}
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;
}
<?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 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);
}
}
客戶端調用結果:
jboss控制台輸出:
在此,沒有使用本地接口,本地接口規則如下:
出處http://lavasoft.blog.51cto.com/62575/290058