`

jee6 学习笔记 8: Handle big data set - without real pagination

    博客分类:
  • JEE
阅读更多
Problem:

There is a problem when dealing with big data set. For instance, if we have millions of records in database to search and display, we cannot just use a @SessionScoped backing bean to put the search result list in memory. This would quickly exhaust server memory if many users are online.

The alternative to @SessionScoped is to use @ViewScoped. It is said that the same @ViewScoped bean would be available as long as user stays on the same view(page). The @ViewScoped backing bean would be garbage collected after user leaves the current page. That's what we want to handle big data set.

However, when testing with JBoss 6.1 and JSF2.1, with Primefaces 3.3.1, annotation @ViewScoped seemed not work as expected: JSF created different instances. This would make Primefaces sorting and paging not working at all. Or put it another way, it did not use the same instance at all for different request. It might be caused by Primefaces, or caused by the JSF2.1 implementation.


Solution:


If you do want to use @SessionScoped backing bean, a workaround is to provide searching filters, ie, enforce user to input searching parameters and thus limit the returned data set in an acceptable size.

If you want to use @ViewScoped backing bean, my experiment shows that you can use the "Flash" scope to store the searching result and pre-load this search result list when initializing the @ViewScoped bean. This way, it solves the big data set problem as well as enables Primefaces sorting and paging to work fine.

The following is the example source code of a @ViewScoped backing bean StudentSearch2:
package com.jxee.action.student;

import java.io.Serializable;
import java.util.List;
import java.util.TimeZone;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.inject.Inject;

import org.apache.log4j.Logger;

import com.jxee.ejb.student.StudentDAO;
import com.jxee.model.student.Student;

@ManagedBean(name="ss")
@ViewScoped
public class StudentSearch2 implements Serializable {

  private static final Logger log = Logger.getLogger(StudentSearch2.class);
  private static final String SEARCH_PAGE = "/student/studentSearch.xhtml";
  
  private List<Student> searchResultList;
  private @EJB StudentDAO dao;

  private String nameFilter;
  private int maxRows = 50;
  
  /**
   * property to flag is cached result list needs reload, after add/update/delete etc.
   * set from views like this: <f:setPropertyActionListener value="true" target="#{ss.refresh}" />
   */
  private boolean refresh;
  
  private @Inject Flash flash;  // @Inject seems not working at all :twisted: 
    
  
  @PostConstruct
  public void init() {
    log.debug(this + ": restore from flash...");
    
    // if flash is not injected properly, get it manually
    ExternalContext cntxt = FacesContext.getCurrentInstance().getExternalContext();  
    
    // lets set logged in user into to &quot;flash&quot; scope.   
    // 1. it can be accessed like this: "#{flash.USER-key.username}"; in the JSF2 pages.  
    // 2. or in the backing bean code: User user = (User) flash.get("USER-key");  
    this.flash = cntxt.getFlash();
    
    this.getFromFlash();
  }

  public boolean isRefresh() {
    return refresh;
  }

  public void setRefresh(boolean refresh) {
    log.debug("-- setting refresh: " + refresh);
    this.refresh = refresh;
  }

  public String findByName() {
    log.debug(this.toString() + "-- 21. search student by nameFilter: " + nameFilter + "; refresh=" + this.isRefresh());
    searchResultList = dao.find(this.nameFilter, maxRows);
    log.debug(this.toString() + "-- 22. search found: " + this.searchResultList.size());
    
    this.save2Flash();

    return SEARCH_PAGE;
  }
  
  //put search result in Flash scope for later reference
  private void save2Flash() {
    flash.put("ssList2", this.searchResultList);
    flash.put("sKey", this.nameFilter);
  }
  
  // get search result list from Flash scope
  private void getFromFlash() {
    this.searchResultList = (List<Student>) flash.get("ssList2");
    this.nameFilter = (String) flash.get("sKey");
  }
  
  public String getNameFilter() {
    return nameFilter;
  }

  public void setNameFilter(String afilter) {
    this.nameFilter = afilter;
  }

  public int getMaxRows() {
    return maxRows;
  }

  public void setMaxRows(int maxRows) {
    this.maxRows = maxRows;
  }

  public List<Student> getSearchResultList() {
    if(this.searchResultList == null) {
      this.getFromFlash();
    }
    
    if(this.isRefresh() || this.searchResultList == null) {
      this.findByName();
      this.refresh = false;
    }
    
    return this.searchResultList;
  }

  public void setSearchResultList(List<Student> searchResultList) {
    this.searchResultList = searchResultList;
  }
  
  public int getSize() {
    int size = getSearchResultList().size();
    log.debug("searched result list size: " + size);
    return size;
  }
  
  // use default TimeZone to display time properly
  public TimeZone getTimeZone() {
    TimeZone tz = TimeZone.getDefault();
    return tz;
  }
  
}


Screenshot of the student search with @ViewScoped backing bean and use of "Flash" scope:



Server log shows the @ViewScoped backing bean is not View Scoped at all. For each clicking on sorting/paging of the Primefaces data table, it generated a new instance, gosh!

To enable Primefaces data table working with a @ViewScoped backing bean, the "Flash" scope was used to store the search result list, which was pre-loaded when initializing the @ViewScoped bean.
  • 大小: 70.8 KB
分享到:
评论

相关推荐

    eclipse-jee-mars-1-win32-x86_64.7z

    eclipse-jee-mars-1-win32-x86_64.7z eclipse-jee-mars-1-win32-x86_64.zip 我打的 7z 压缩包 关于有 Alt + / 不起作用解决办法: window -&gt; General -&gt; Keys -&gt; Content Assist -&gt; Binding: 改为 Alt + / When:...

    eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64 eclipse-jee-2021-12-R-win32-x86_64

    eclipse-jee-2022-06-R-win32-x86_64.zip

    在解压eclipse-jee-2022-06-R-win32-x86_64.zip后,我们会得到一个名为“eclipse”的文件夹,这个文件夹包含了整个IDE的所有组件和配置。启动Eclipse IDE,用户会看到熟悉的界面,包括工作区(Workspace)、透视图...

    eclipse-jee-2020-12-R-win32-x86_64

    《Eclipse IDE for Java开发者:深入解析eclipse-jee-2020-12-R-win32-x86_64》 Eclipse IDE,全称集成开发环境(Integrated Development Environment),是全球广泛使用的开源Java开发工具。该版本"eclipse-jee-...

    eclipse-jee-2018-09-win32-x86_64.zip

    标题 "eclipse-jee-2018-09-win32-x86_64.zip" 提供的信息表明这是一款针对Java企业级开发的Eclipse集成开发环境(IDE)的2018年9月版本,适用于Windows 32位操作系统、x86_64架构的计算机。Eclipse是一个开源的、跨...

    Atlas2.3.0依赖: org.restlet/sqoop-1.4.6.2.3.99.0-195

    在IT行业中,我们经常涉及到各种库和框架的集成与使用,这次我们关注的是"Atlas2.3.0"依赖的组件:"org.restlet/sqoop-1.4.6.2.3.99.0-195"。这个依赖包含了三个关键的JAR文件:`sqoop-1.4.6.2.3.99.0-195.jar`,`...

    jee6 学习笔记 6.3 - @Asynchronous

    在Java企业版(Java EE)6中,`@Asynchronous`注解是一个非常重要的特性,它使得开发者可以方便地在应用程序中实现异步处理。这个注解是Java EE并发编程的一部分,主要应用于EJB(Enterprise JavaBeans)环境,用于...

    JEE企业应用笔记

    ### JEE企业应用笔记 #### 一、JSP与Servlet **JSP (Java Server Pages)** 和 **Servlet** 是Java Web开发中的两个核心组件。它们共同构建了动态Web应用程序的基础。 ##### JSP基本语法 在JSP页面中,可以通过...

    eclipse-jee-2023-09-R-linux-gtk-x86-64.tar.gz

    "eclipse-jee-2023-09-R-linux-gtk-x86_64.tar.gz" 文件是Eclipse专为Java企业版(Java EE)开发者设计的2023年9月版本,适用于64位的Linux操作系统。这个版本包含了对Java EE开发所需的全部工具和功能,如Web服务器...

    Restlet所需要的所有jar包

    接着,根据你的需求,可以参考Restlet官方文档或者示例代码,学习如何创建和配置`Application`和`Component`,定义路由规则,并处理HTTP请求。 在使用过程中,要注意版本兼容性问题,确保Restlet框架及其依赖库与你...

    jee6 学习笔记 1 - 开发环境的配置

    NULL 博文链接:https://jxee.iteye.com/blog/1575432

    eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip

    Eclipse-jee-mars-R-win32-x86_64位官方绿色版.zip是一个针对Windows平台的64位版本的Eclipse集成开发环境(IDE)的压缩包,特别为Java企业级(J2EE)应用程序开发设计。该版本发布于2015年6月30日,是当时Eclipse ...

    eclipse-jee-2023-09-R-win32-x86-64.zip

    "eclipse-jee-2023-09-R-win32-x86_64.zip" 是Eclipse针对2023年9月发布的一个更新版,专为Windows 64位操作系统设计。 在Windows系统上安装和使用Eclipse-JEE,你需要了解以下关键知识点: 1. **安装步骤**: - ...

    jee-maven-archetype:Java EE Quickstarter-使用Docker进行系统测试

    mvn archetype:generate -Dfilter=de.dplatz:jee8-maven-archetype 使用以下命令运行完整的版本,包括集成测试: mvn verify 该命令不仅将构建项目,还将部署它并运行集成/系统测试。 有关各种应用程序服务器,...

    eclipse-jee-2022-03-R-win32-x86_64.zip

    Eclipse-jee-2022-03-R-win32-x86_64.zip是一个专为Windows 64位系统设计的压缩包,其中包含了Eclipse集成开发环境(IDE)的Java和R语言版本。这个版本的Eclipse是2022年3月发布(R版本)的,针对Java开发者以及R...

    eclipse-jee-oxygen-2-win32-x86_64.zip

    Eclipse IDE for Java EE Developers Tools for Java developers creating Java EE and Web applications, including a Java IDE, tools for Java EE, JPA, JSF, Mylyn, EGit and others.

    eclipse-jee-2019-09-R-win32-x86_64

    文件名称: eclipse-jee-2019-09-R-win32-x86_64.zip 文件大小: 352 MB (369,940,487 字节) MD5: fc821116680da0bca7bd7a1d9e0b48b7 SHA1: 14be627915d2e11b618434d48ddf9e3ec3849392 SHA256: 3808c394d16153ceb429...

    eclipse-jee-mars-1-win32-x86_64.rar

    这个版本,即"eclipse-jee-mars-1-win32-x86_64.rar",是Eclipse Mars系列的1.0版本,它集成了Java企业版(Java EE)的开发工具,为Web应用程序和企业级项目提供了全面的支持。 1. **Eclipse IDE介绍**: Eclipse...

    MyEclipse 8.6官方下载地址+注册机+中英互换工具

    - MD5校验码:32Bit:b4baeea6dccc713df78bf77f231a0b57 - **标准版 64位**: `myeclipse-8.6.0-linux-gtk-x86_64.tgz` - 文件大小:865.09MB - MD5校验码:64Bit:79699ff515cb1a7b1d15df46d8337495 - **Spring版 ...

Global site tag (gtag.js) - Google Analytics