我正在尝试了解Stateful Session bean的生命周期。我在调用激活和钝化生命周期回调时特别感兴趣。我创建了一个演示来理解这个场景。这是我的代码:
我的远程界面是:
package com.orbit.stateful;
import javax.ejb.Remote;
import java.util.List;
@Remote
public interface ShoppingCart
{
public void addItem(String itemName);
public void removeItem(String item);
public List<String> getAllItems();
public void finishShopping();
}
我的有状态会话bean类如下:
package com.orbit.stateful;
import javax.ejb.Stateful;
import javax.ejb.Remove;
import javax.ejb.PrePassivate;
import javax.ejb.PostActivate;
import javax.ejb.StatefulTimeout;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.ArrayList;
@Stateful
//@StatefulTimeout(value=5L,unit=TimeUnit.MINUTES)
public class ShoppingCartBean implements ShoppingCart
{
private List<String> itemList;
public ShoppingCartBean(){
System.out.println("Stateful SessionBean Constructor Called");
}
@PostConstruct
public void intialize(){
System.out.println("Initializing shopping Cart");
itemList=new ArrayList<String>();
}
public void addItem(String itemName){
itemList.add(itemName);
}
public void removeItem(String item){
itemList.remove(item);
}
public List<String> getAllItems(){
return itemList;
}
@Remove
public void finishShopping(){
System.out.println("I am finished with Shopping");
}
@PreDestroy
public void tidyUp(){
System.out.println("Remove Shopping Bean");
}
@PrePassivate
public void logPassivation(){
System.out.println("Passivating Now");
}
@PostActivate
public void logActivation(){
System.out.println("Activating Now");
}
}
此bean的独立客户端是:
package com.orbit.stateful;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;
import java.util.List;
public class ShoppingCartClient
{
public static void main(String[] args) throws Exception
{
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
Context ctx=new InitialContext(props);
ShoppingCart sc=(ShoppingCart)ctx.lookup("com.orbit.stateful.ShoppingCart");
sc.addItem("J2SE");
//Thread.sleep(17000);
sc.addItem("J2EE");
sc.addItem("JDBC");
List<String> books=sc.getAllItems();
for (String str: books )
{
System.out.println("Books is : "+ str);
}
//sc.finishShopping();
}
}
现在我在GlassFish 3.1.2中设置了Cache Idle Timeout
= 20
但是我的有状态会话bean在20秒后仍然没有被钝化。我在Glashfish做错了什么或者没有正确发生什么事情?帮助我理解这一点。
在此先感谢