gpt4 book ai didi

jsf - 如何以及何时从 JSF 中的 LRU 缓存中删除 View 范围 bean?

转载 作者:行者123 更新时间:2023-12-02 10:29:35 25 4
gpt4 key购买 nike

当 View 作用域 bean 被销毁时,我读到了一个很好的答案。 (参见 How and when is a @ViewScoped bean destroyed in JSF? )并且我自动假设被破坏的 bean 也从 View 范围缓存中删除。但我可以看到该 bean 仍然在缓存中,所以我想知道被破坏的 View 范围 bean 是否也应该从 LRU View 范围缓存中删除(如果有的话)?

在我们的应用程序中,我们在单独的选项卡/窗口中打开所有详细信息。在一些打开/关闭之后(取决于 numberOfViewsInSession),如果第一个详细信息窗口仍然打开并且用户已经打开和关闭另一个详细信息窗口并且在一段时间后他想要在第一个窗口中执行某些操作,我们可以看到 ViewExpiredException 。我做了一些调试,我可以看到关闭的 View 没有从 LRU 缓存中删除。

那么这是预期的行为还是我的代码有问题?如果这是预期的行为,是否有任何有用的策略如何使用多选项卡/多窗口,而不会导致 LRU 缓存引起大量 ViewExpiredException?我知道我可以更改 numberOfViewsInSession 但这是我想使用的最后一个选择。

我准备了一个简单的测试用例,当我多次打开/关闭 view.xhtml 时,我可以看到 LRUMap 正在增长。

环境:JDK7、mojarra 2.2.4、tomcat 7.0.47

提前致谢

view.xhtml

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html">
<head>
<title>View Bean</title>
<meta charset="UTF-8"/>
</head>
<body>
<h:form id="viewForm">
<div>#{viewBean.text}</div>
<h:commandButton id="closeButton" value="Close" action="/ClosePage.xhtml"/>
</h:form>
</body>
</html>

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Session bean</title>
</h:head>
<h:body>
<h:form id="sessionForm">
<h:outputText value="#{sessionBean.text}"/>
<br/>
<h:link id="linkView" value="Open view.xhmtl" outcome="/view.xhtml" target="_blank"/>
</h:form>
</h:body>
</html>

ClosePage.xhmtl

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head><title>Window will be closed</title></head>
<body>
<script>window.close();</script>
</body>
</html>

ViewBean.java

package com.mycompany.mavenproject2;

import com.sun.faces.util.LRUMap;
import java.io.Serializable;
import java.util.Map;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "viewBean")
@ViewScoped
public class ViewBean implements Serializable {

private static final long serialVersionUID = 13920902390329L;

private int lruMapSize;

/**
* Creates a new instance of ViewBean
*/
public ViewBean() {

Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
LRUMap<String, LRUMap> lruMap = (LRUMap) sessionMap.get("com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap");
lruMapSize = lruMap == null ? 0 : lruMap.size();
}

@PreDestroy
void destroyed() {
System.out.println("View bean destroyed");
}

public String getText() {
return "ViewBean LRU cache size:" + Integer.toString(lruMapSize);
}

}

SessionBean.java

package com.mycompany.mavenproject2;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "sessionBean")
@SessionScoped
public class SessionBean implements Serializable {
private static final long serialVersionUID = 1777489347L;

/**
* Creates a new instance of SessionBean
*/
public SessionBean() {
}

public String getText() {
return "Session bean text";
}

}

最佳答案

我认为每个 JSF 开发人员最终都会遇到这个问题。真正的问题在于,您无法设计一个真正可靠的有状态系统,在该系统中,浏览器将向 ViewScoped bean 发回信号,表明页面已完成,从而允许支持 bean 自行销毁。这就是为什么 JSF 实现具有 LRU 缓存来限制 session 使用的内存,这对于日常应用程序来说是一个很好的包罗万象的解决方案。

在某些情况下,您知道您已经完成了 ViewScoped bean,例如从该 bean 进行的重定向。对于这些,您可以编写自己的 View 处理程序来执行更智能的缓存系统,但这不是一项简单的任务,坦率地说,不值得付出努力。

我想到的最简单的解决方案是使用 JavaScript 计时器通过 ViewScoped bean 在每个页面上执行 ajax 回发到服务器。 (将此计时器设置为每 30 秒执行一次似乎是合理的。)这会将与页面关联的 ViewScoped bean 移动到 LRU 缓存的底部,确保它们不会过期。

特别是,我使用 primefaces poll 组件来完成此操作,并将其粘贴到供所有 ViewScoped beans 使用的模板中。通过将此组件放置在其自己的表单中,请求大小仍然很小。

关于jsf - 如何以及何时从 JSF 中的 LRU 缓存中删除 View 范围 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com