## 一,定義有狀態Bean和無狀態Bean
有狀態Bean:
~~~
@Stateful
@Remote
public class StatefulEjbBean implements StatefulEjb{
private int state;
@Override
public void compute(int i) {
state=state+i;
}
@Override
public int getResult() {
return state;
}
}
~~~
無狀態Bean:
~~~
@Stateless
@Remote
public class StatelessEjbBean implements StatelessEjb {
private int state;
@Override
public void compute(int i) {
state = state + i;
}
@Override
public int getResult() {
return state;
}
}
~~~
## 二,客戶端測試及結果
1,測試有狀態EJB對象:
~~~
public class StatefulEjbClient {
public static void main(String[] args) throws Exception {
InitialContext context=new InitialContext();
//第一次會話
StatefulEjb ejb1=(StatefulEjb)context.lookup("StatefulEjbBean/remote");
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
System.out.println("第一次會話結束---------");
//第二次會話
StatefulEjb ejb2=(StatefulEjb)context.lookup("StatefulEjbBean/remote");
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
System.out.println("第二次會話結束---------");
}
}
~~~
結果:

2,測試無狀態EJB對象:
~~~
public class StatelessEjbClient {
public static void main(String[] args) throws NamingException {
InitialContext context=new InitialContext();
//第一次會話
StatelessEjb ejb1=(StatelessEjb)context.lookup("StatelessEjbBean/remote");
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
ejb1.compute(1);
System.out.println(ejb1.getResult());
System.out.println("第一次會話結束---------");
//第二次會話
StatelessEjb ejb2=(StatelessEjb)context.lookup("StatelessEjbBean/remote");
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
ejb2.compute(1);
System.out.println(ejb2.getResult());
System.out.println("第二次會話結束---------");
//判斷每次查找到的對象是否一樣
System.out.println(ejb1==ejb2);//false
}
}
~~~
結果:

## 三,結果對比
通過多次執行,發現對于有狀態的EJB對象,每次通過查找獲得的對象都是新對象;而對于無狀態的EJB對象,每次查找獲得的對象都有一個單例類的效果,多次執行測試無狀態的EJB對象的方法,會發現服務端的貌似始終在對一個對象進行操作。
- 前言
- Spring簡化配置
- Spring中使用AspectJ實現AOP
- Spring中JDK的動態代理和CGLIB代理的區別
- Spring配置問題——元素 "context:component-scan" 的前綴 "context" 未綁定
- Hibernate中編程式事物的簡單使用
- 使用Spring為Hibernate配置聲明式事物
- Struts2+AJAX獲取json數據
- 中間件概述
- EJB(Enterprise Java Bean)概述
- JBoss 6.1安裝配置問題
- EJB對象的部署及客戶端調用簡單示例
- 有狀態的EJB對象和無狀態的EJB對象
- EJB遠程調用和本地調用
- MyBatis——入門select