Xpages @PreDestroy
Posted
技术标签:
【中文标题】Xpages @PreDestroy【英文标题】: 【发布时间】:2012-04-03 11:31:39 【问题描述】:总结:有谁知道如何让@PreDestroy 在 Application Scope 托管 bean 的回收\超时时触发?
几周前我发布了一个关于“预定代理”的问题:30 sec periodic task to poll external web service and cache data ...到目前为止,我使用线程成功实现了(目前使用此方法作为数据库设计中包含的所有逻辑),我可以从我的应用程序范围支持 bean 成功启动\取消\暂停\重新启动线程.但一个副作用是,当启动 Thread 的 backing bean 被回收时,Thread 会继续运行。我有一个方法在我的 Application Scope bean 中使用 @PreDestroy 调用我的取消线程方法,但它似乎没有被调用。
我确实从 IBM 找到了这个链接: LO67255:托管 Bean 注释 - @POSTCONSTRUCT 和 @PREDESTROY 未按预期工作。 http://www-01.ibm.com/support/docview.wss?crawler=1&uid=swg1LO67255 ...但我无权访问那篇文章,所以不确定结果是否......它不起作用。
我有一个非常简单的测试类要演示,我在顶部导入了一些冗余库,因为在这里找到了最后一篇文章:https://community.jboss.org/thread/179819 但无法访问 XPages 中的 javax.enterprise.*。
出于测试目的,我将 DB XPage 属性“应用程序超时”中的“回收”设置为 1。通过一个简单的页面调用(见下文)...如果您等待 1 分钟,您可以看到构造函数被触发,但 @PreDestroy 和 PostConstruct 永远不会被调用。
对于任何 cmet 或建议...在此先感谢。
尼克
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>
【问题讨论】:
【参考方案1】:有三种类型的 JSF 侦听器工件提供了手动清理存储在作用域中的对象(包括托管 bean)的机会:
-
FacesContextListener:它的 beforeContextReleased() 方法是
任何请求终止之前的绝对最后一次调用,所以这是一个
清理 requestScope 的理想场所。
SessionListener: 它的
sessionDestroyed() 方法提供了清理
sessionScope。
ApplicationListener: 它的 applicationDestroyed()
方法提供了清理 applicationScope 的机会。
ApplicationListener 必须在OSGi XSP Library 中定义;前两个可以在库中定义,也可以在特定 NSF 本地定义。
【讨论】:
【参考方案2】:我可能错了,但是 XPages 是基于 JSF1.2 构建的,而托管 bean 注释只能从 JSF2.0 中获得。
【讨论】:
以上是关于Xpages @PreDestroy的主要内容,如果未能解决你的问题,请参考以下文章