- 浏览: 96532 次
文章分类
最新评论
-
jXee:
lgs0626 写道求源码,兄弟给共享下吧 "jee ...
jee6 学习笔记 4 - CRUD 2: View Details, Primefaces row selection -
lgs0626:
求源码,兄弟给共享下吧
jee6 学习笔记 4 - CRUD 2: View Details, Primefaces row selection
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:
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.
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 "flash" 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.
发表评论
-
ActiveMQ and Spring JMS Framework Message Loss
2019-06-28 07:15 29Java Message Service (JMS) prov ... -
how to proxy to k8s web console
2018-06-28 07:16 531### how to access k8s web conso ... -
Call Stored Procedure with JPA 2.1
2018-06-27 10:57 619JPA 2.1 introduces APIs to call ... -
Send response and then process - async processing
2017-10-12 09:35 514If your request processing take ... -
java 8 time api test
2017-08-29 05:40 443public class ParseUtcDateTime ... -
Setup ApiKey in header with Swagger generated client code
2017-08-23 06:41 439@Value("${api.base.path} ... -
Simple tool to monitor jvm memory usage and garbage collection
2016-10-13 06:06 320JDK has built-in tool to moni ... -
Externalize Application Config properties with JBoss 7.1
2017-06-02 12:09 307If you have configuration pro ... -
JPA native query does not support setting list parameters
2014-03-27 06:45 974you might want to do the ... -
Owning Side and Inverse Side of JPA entity relationships
2013-09-10 07:08 763Entity relationships may be b ... -
avoid setParameter for "order by" in JPQL
2013-03-07 05:55 754you might want to create a JP ... -
JPA Path Expression, operator IN and Collection properties
2013-01-23 16:25 1342If we want to select the Orde ... -
与JEE6/EJB3.1相比, Spring framework 丧失了几乎所有的优势
2013-01-19 13:13 999The Spring framework was a ma ... -
Simple EasyMock tutorial
2012-12-20 11:57 625http://veerasundar.com/blog/20 ... -
Servlet 3.0 @WebFilter and @WebServlet
2012-12-04 07:09 2635Servlet 3.0 provides new annota ... -
Why JSF2 @ViewScoped not working?
2012-12-03 06:55 1343javax.faces.bean.ViewScoped sai ... -
When to configure an XA datasource?
2012-11-16 12:58 1228If you ever came across this wa ... -
java ee transaction and datasource concepts
2012-11-10 13:48 9911. What is a transaction? A tra ... -
pass params to primefaces confirmation dialog box
2012-09-28 19:30 1301<p:dataTable id="idStuD ... -
Handle Big Dataset with Real Pagination with Primefaces 3.3 LazyDataModel
2012-09-21 13:41 5586If you have millions of record ...
相关推荐
eclipse-jee-mars-1-win32-x86_64.7z eclipse-jee-mars-1-win32-x86_64.zip 我打的 7z 压缩包 关于有 Alt + / 不起作用解决办法: window -> General -> Keys -> Content Assist -> 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-2020-12-R-win32-x86_64,最新版本eclipse4.18,需要其他版本eclipse可以查询我资源列表
eclipse-jee-2018-09-win32-x86_64.zip
NULL 博文链接:https://jxee.iteye.com/blog/1608820
eclipse-jee-2022-03-R-win32-x86_64.zip
NULL 博文链接:https://jxee.iteye.com/blog/1575432
mvn archetype:generate -Dfilter=de.dplatz:jee8-maven-archetype 使用以下命令运行完整的版本,包括集成测试: mvn verify 该命令不仅将构建项目,还将部署它并运行集成/系统测试。 有关各种应用程序服务器,...
文件名称: eclipse-jee-2019-09-R-win32-x86_64.zip 文件大小: 352 MB (369,940,487 字节) MD5: fc821116680da0bca7bd7a1d9e0b48b7 SHA1: 14be627915d2e11b618434d48ddf9e3ec3849392 SHA256: 3808c394d16153ceb429...
eclipse-jee-mars-R-win32-x86_64.zip 截至2015.06.30 eclipse-j2ee 最新版
sqoop-1.4.6.2.3.99.0-195.jar org.restlet-2.4.3.jar org.restlet.ext.servlet-2.4.3.jar
Restlet所需要的所有jar包 一次下载,以后高枕无忧!
eclipse-jee-2020-06-R-win32-x86_64
eclipse-jee-2022-09-R-win32-x86_64.zip 适用于Windows x86_64
eclipse-jee-2018-09 Java开发软件,已经集成SVN插件,方便使用SVN的同学进行项目开发
JEE企业应用笔记
开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32开发工具 eclipse-jee-mars-2-win32...
eclipse-jee-oxygen-3a-win32-x86_64.zip eclipse-jee-oxygen-3a-win32-x86_64.zip eclipse-jee-oxygen-3a-win32-x86_64.zip 官网原包
主要用于安装VS2015关于设置API19~21的错误和API22,API23的错误和Jave SE 开发工具包(7.0.550.13)的错误,请先安装对应的JDK:jdk-7u80-windows-x64.exe