分享
 
 
 

精通ejb【四】(转载)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

转载自:http://www.java-cn.com/technology/technology_detail.jsp?id=784

状态会话Bean基础

Stateful Session Bean可以一对一的维持某个调用客户的状态,并且在不同的方法调用中维持这个状态, 由于对于每一个并发用户,必须有一个对应的Stateful Session Bean,为了提高系统的效率,Stateful Session Bean可以在一定的客户空闲时间后被写入二级存储设备(如硬盘),在客户发出新的调用请求后,再从二级存储 设备恢复到内存中。但是在多用户下,Stateless Session Bean运行效率高于Stateful Session Bean。

javax.ejb.EnterpriseBean接口继承了java.io.Serializable,用以实现写入读出操作。

当EJB容器调用ejbPassivate()方法钝化了bean之后,就可以把它写入二级存储设备,然后容器调用ejbActivate()方法激活bean,把它从二级存储设备中读出。

状态bean的钝化过程

计数bean的远程接口

远程接口定义了一个业务方法count(),它将在企业bean类中实现。

激活状态bean

package com.wiley.compBooks.roman.session.count;

import javax.ejb.*;

import java.rmi.RemoteException;

/**

* These are CountBean´s business logic methods.

*

* 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 Count extends EJBObject {

/**

* Increments the int stored as conversational state

*/

public int count() throws RemoteException;

}

Source Count.java

package com.wiley.compBooks.roman.session.count;

import javax.ejb.*;

/**

* Demonstration Stateful Session Bean. This bean is

* initialized to some integer value and has a business

* method that increments the value.

*

* This example shows the basics of how to write a stateful

* session bean and how passivation/activation works.

*/

public class CountBean implements SessionBean {

private SessionContext ctx;

// The current counter is our conversational state.

public int val;

//

// Business methods

//

/**

* Counts up

*/

public int count() {

System.out.println("count()");

return ++val;

}

//

// EJB-required methods

//

public void ejbCreate(int val) throws CreateException {

this.val = val;

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(SessionContext ctx) {

}

}

Source CountBean.java

Bean实现了javax.ejb.SessionBean。所以,它必须定义所有SessionBean定义的方法。

OjbCreate()初始化带了val的参数。它将作为counter的初始状态。在钝化和激活bean的过程中,val变量被保护。

计数bean的home接口

package com.wiley.compBooks.roman.session.count;

import javax.ejb.*;

import java.rmi.RemoteException;

/**

* This is the home interface for CountBean. This interface

* is implemented by the EJB Server´s glue-code 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 the CountBean file.

*/

public interface CountHome extends EJBHome {

/*

* This method creates the EJB Object.

*

* @param val Value to initialize counter to

*

* @return The newly created EJB Object.

*/

Count create(int val) throws RemoteException, CreateException;

}

Source CountHome.java.

计数bean的配置描述符

计数bean的配置描述符

计数bean的环境属性

生成计数bean的Ejb-jar文件

计数bean的客户端代码

package com.wiley.compBooks.roman.session.count;

import javax.ejb.*;

import javax.naming.*;

import java.util.Properties;

/**

* This class is a simple example of client code that invokes

* methods on a simple Stateless Enterprise Bean.

*

* We create 3 EJB Objects in this example, but we allow

* the container to have only 2 in memory. This illustrates how

* beans are passivated to storage.

*/

public class CountClient {

public static void main(String[] args) {

try {

/*

* Get System properties for JNDI initialization

*/

Properties props = System.getProperties();

/*

* Get a reference to the Home Object - the

* factory for EJB Objects

*/

Source CountClient.java

1、需要JNDL初始化上下文

2、使用JNDL定位home接口

3、使用home对象建立3个不同的计数EJB对象,因此也就和三个不同的客户端建立了会话

4、配置描述符限制了同时只能有两个bean工作,因此3个bean中一定有钝化的。在调用ejbPassivate()时,打印一条信息。

5、在每个EJB对象上调用count()方法,调用ejbActivate()方法激活bean,该方法打印一条信息。

6、最后所有的EJB对象被删除。

package com.wiley.compBooks.roman.session.count;

import javax.ejb.*;

import javax.naming.*;

import java.util.Properties;

/**

* This class is a simple example of client code that invokes

* methods on a simple Stateless Enterprise Bean.

*

* We create 3 EJB Objects in this example, but we allow

* the container to have only 2 in memory. This illustrates how

* beans are passivated to storage.

*/

public class CountClient {

public static void main(String[] args) {

try {

/*

* Get System properties for JNDI initialization

*/

Properties props = System.getProperties();

/*

* Get a reference to the Home Object - the

* factory for EJB Objects

*/

Context ctx = new InitialContext(props);

CountHome home = (CountHome) ctx.lookup("CountHome");

/*

* An array to hold 3 Count EJB Objects

*/

Count count[] = new Count[3];

int countVal = 0;

/*

* Create and count() on each member of array

*/

System.out.println("Instantiating beans...");

for (int i=0; i < 3; i++) {

/*

* Create an EJB Object and initialize

* it to the current count value.

*/

count[i] = home.create(countVal);

/*

* Add 1 and print

*/

countVal = count[i].count();

System.out.println(countVal);

/*

* Sleep for 1/2 second

*/

Thread.sleep(500);

}

/*

* Let´s call count() on each EJB Object to

* make sure the beans were passivated and

* activated properly.

*/

System.out.println("Calling count() on beans...");

for (int i=0; i < 3; i++) {

/*

* Add 1 and print

*/

countVal = count[i].count();

System.out.println(countVal);

/*

* Sleep for 1/2 second

*/

Thread.sleep(500);

}

/*

* Done with EJB Objects, so remove them

*/

for (int i=0; i < 3; i++) {

count[i].remove();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

Source CountClient.java

运行客户端:

对于BEA的WebLogic,执行:

java -Djava.naming.factory.initial=

weblogic.jndi.TengahInitialContextFactory

-Djava.naming.provider.url=

t3://localhost:7001

com.wiley.compBooks.roman.session.count.CountClient

客户端输出:

Instantiating beans...

1

2

3

Calling count() on beans...

2

3

4

服务端输出:

ejbCreate()

count()

ejbCreate()

count()

ejbCreate()

ejbPassivate()

count()

ejbPassivate()

ejbActivate()

count()

ejbPassivate()

ejbActivate()

count()

ejbPassivate()

ejbActivate()

count()

ejbPassivate()

ejbActivate()

ejbRemove()

ejbActivate()

ejbRemove()

ejbRemove()

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有