Xpages @PreDestroy [英] Xpages @PreDestroy

查看:18
本文介绍了Xpages @PreDestroy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:有谁知道如何让@PreDestroy 在 Application Scope 托管 bean 的回收\超时时触发?

Summary: does anyone know how to get @PreDestroy to get triggered on recycling\ timeout of an Application Scope managed bean?

几周前我发布了一个关于预定代理"的问题:30 秒周期性任务轮询外部 Web 服务并缓存数据...到目前为止,我使用线程成功实现了(目前使用此方法作为数据库设计中包含的所有逻辑),我可以从我的应用程序范围支持 bean 成功启动\取消\暂停\重新启动线程.但是一个副作用是,当启动 Thread 的 backing bean 被回收时,Thread 会继续运行.我有一个方法在我的 Application Scope bean 中使用 @PreDestroy 调用我的取消线程方法,但它似乎没有被调用.

I posted a question a couple of weeks ago about "scheduled agents": 30 sec periodic task to poll external web service and cache data ...which I implemented successfully so far using a Thread (at present went with this method as all the logic contained within the design of the database), I can start\ cancel\ pause\ restart the Thread successfully from my Application Scope backing bean. But a side effect is that when the backing bean that initiates the Thread is recycled, the Thread keeps running. I have a method that calls my cancel Thread method using @PreDestroy in my Application Scope bean, but it appears this does not get called.

我确实从 IBM 找到了这个链接:LO67255:托管 Bean 注释 - @POSTCONSTRUCT 和 @PREDESTROY 未按预期工作.http://www-01.ibm.com/support/docview.wss?crawler=1&uid=swg1LO67255...但我无权阅读那篇文章,所以不确定结果是否...它不起作用.

I did find this link from IBM: LO67255: MANAGED BEANS ANNOTATION - @POSTCONSTRUCT AND @PREDESTROY IS NOT WORKING AS EXPECTED. http://www-01.ibm.com/support/docview.wss?crawler=1&uid=swg1LO67255 ...but I do not have access to get to that article, so am not sure if the outcome is...it doesn't work.

我有一个非常简单的测试类来演示,我在顶部导入了一些冗余库,因为在这里找到的最后一篇文章:https://community.jboss.org/thread/179819 但无法访问 XPage 中的 javax.enterprise.*.

I have a very simple test class to demonstrate, I imported some redundant libs at top, because of last post found here: https://community.jboss.org/thread/179819 but do not have access to javax.enterprise.* in XPages.

出于测试目的,我将 DB XPage 属性Application Timeout"中的recycling"设置为 1.通过一个简单的页面调用(见下文)...如果你等待 1 分钟,你可以看到构造函数被触发,但 @PreDestroy 和 PostConstruct 永远不会被调用.

I set the "recycling" in the DB XPage properties "Application Timeout" to 1 for testing purposes. With a simple page calling (see below)...if you wait 1 min, you can see the constructor firing, but the @PreDestroy and PostConstruct never get called.

如有任何意见或建议...提前致谢.

For any comments or suggestions...thanks in advance.

尼克

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>

推荐答案

提供了三种类型的 JSF 侦听器工件,它们提供了手动清理存储在作用域中的对象(包括托管 bean)的机会:

There are three types of JSF listener artifacts that provide an opportunity to manually clean up objects stored in scope (including managed beans):

  1. FacesContextListener:它的 beforeContextReleased() 方法是在任何请求终止之前绝对最后一次调用,所以这是一个清理 requestScope 的理想场所.
  2. SessionListener:它的sessionDestroyed() 方法提供了一个机会来清理sessionScope.
  3. ApplicationListener:它的 applicationDestroyed()方法提供了清理 applicationScope 的机会.
  1. FacesContextListener: its beforeContextReleased() method is the absolute last call before any request terminates, so this is an ideal place to clean up the requestScope.
  2. SessionListener: its sessionDestroyed() method provides a chance to clean up the sessionScope.
  3. ApplicationListener: its applicationDestroyed() method provides a chance to clean up the applicationScope.

ApplicationListener 必须在 OSGi XSP 库中定义;前两个可以在库中定义,也可以在特定 NSF 本地定义.

An ApplicationListener must be defined in an OSGi XSP Library; the first two can be defined either in a library or local to a specific NSF.

这篇关于Xpages @PreDestroy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆