精通ejb【四】(转载)

类别:Java 点击:0 评论:0 推荐:

转载自: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()

本文地址:http://com.8s8s.com/it/it15559.htm