摘要:有人知道如何让@PreDestroy在回收\超时应用程序范围的托管bean时触发吗?
我在几周前发布了一个关于“预定代理”的问题:到目前为止,我使用线程成功实现了30 sec periodic task to poll external web service and cache data ...which (目前使用此方法作为数据库设计中包含的所有逻辑),我可以从我的应用程序作用域支持bean成功地启动\取消\暂停\重新启动线程。但一个副作用是,当启动Thread的支持bean被回收时,Thread将继续运行。我有一个方法,它在我的Application作用域bean中使用@PreDestroy调用我的cancel Thread方法,但似乎没有调用它。
我确实在IBM中找到了这个链接: LO67255: MANAGED BEANS ANNOTATION - @POSTCONSTRUCT和@PREDESTROY没有像预期的那样工作。http://www-01.ibm.com/support/docview.wss?crawler=1&uid=swg1LO67255 ...but我无法访问该文章,因此不确定outcome is...it是否不起作用。
我有一个非常简单的测试类要演示,我在顶部导入了一些多余的库,因为在这里找到了最后一篇文章:https://community.jboss.org/thread/179819,但不能访问XPages中的javax.enterprise.*。
出于测试目的,我将DB XPage属性“应用程序超时”中的“回收”设置为1。通过一个简单的页面调用(见下文),您等待了1分钟,您可以看到构造函数被触发,但是@PreDestroy和PostConstruct从未被调用过。
为任何评论或suggestions...thanks提前。
尼克
import javax.annotation.*;
import java.util.Date;
import javax.annotation.PreDestroy;
import javax.faces.context.*;
import javax.faces.lifecycle.*;
public class Junk {
public Junk(){
System.out.println("Junk.constructor()");
}
@PostConstruct
public void afterOpen(){
System.out.println("Junk.afterOpen() Resource after open...");
}
/**
*
* @return
*/
public String getJunkDate(){
String res = "";
Date d = new Date();
try{
System.out.println("Junk.getJunkDate()==e");
res = d.toLocaleString();
}catch(Exception e){
e.printStackTrace();
}
return res;
}
@PreDestroy
public void destroy(){
System.out.println("Junk.destroy()...!");
}
public void finalize(){
System.out.println("Junk.finalize()...!");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:panel id="panel1">
<xp:button value="Label" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="partial" refreshId="panel1">
</xp:eventHandler>
</xp:button>
<xp:br></xp:br>
<xp:text escape="true" id="computedField1" value="#{javascript:Junk.junkDate}">
</xp:text></xp:panel>
</xp:view>发布于 2012-04-04 05:01:09
有三种类型的JSF侦听器构件提供了手动清理存储在作用域中的对象(包括托管bean)的机会:
的机会它的sessionDestroyed()方法提供了清理applicationScope.的机会
ApplicationListener必须在OSGi XSP Library中定义;前两个可以在库中定义,也可以在特定NSF的本地定义。
发布于 2012-04-03 19:57:45
我可能错了,但XPages是基于JSF1.2构建的,而托管bean批注只能从JSF2.0获得。
https://stackoverflow.com/questions/9992359
复制相似问题