`
tapestry
  • 浏览: 186934 次
社区版块
存档分类
最新评论

在Tapestry4中使用SoftReference实现ObjectPool

阅读更多

Tapestry4.0.x版本的PagePool实现很简单,只是使用一个map容器作为缓存,高并发的情况下容易导致OutOfMemoryException,下面是邮件列表中的相关内容,里边也提到了相关建议,估计会作为一个bug修改,在未修改之前,我会给出一个简单实现。

PagePool doesnt remove idle pages, heap memory doens't get reallocated
> ----------------------------------------------------------------------
>
> Key: TAPESTRY-1151
> URL: http://issues.apache.org/jira/browse/TAPESTRY-1151
> Project: Tapestry
> Issue Type: Bug
> Components: Framework
> Affects Versions: 4.0
> Environment: java 1.4, apache tomcat 5.0 IBM websphere 5.0
> Reporter: lionel gomez
> Assigned To: Jesse Kuhnert
> Priority: Minor
>
> This may not qualify as a bug since its so easy to provide override using hivemind, but instead an improvement for future releases. Also provided an optional page pool implementation.
> Tapestry 4.0 PagePool implementation doesnt remove idle pages. When having hundred of pages with a lot of components and high user concurrency, tapestry generates many instances of each page. These instances are pooled and never unreferenced and never garbage collected. Our 1GB heap eventually fills up and reduces the memory available to other parts of the application. Eventually causes OutOfMemoryException.
> We ensured caching is enabled and config change made to use unique locale, but still, heap eventually fills up.
> With Hivemind its very easy to override the pagePool and provide different implementation. A page pool that uses softReferences is a good option.
> Acording to java api:
> All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError.
> This prevents OEM due to heap filling up.
> New SoftPagePool only changes a couple of lines to use soft references.
> public synchronized Object get(Object key)
> {
> List pooled = (List) _pool.get(key);
> if (pooled == null || pooled.isEmpty())
> return null;
> _count--;
> SoftReference reference = (SoftReference) pooled.remove(0);
> //returns null if has been cleared by GC same as pool where empty
> return reference.get();
> }
> public synchronized void store(Object key, Object value)
> {
> List pooled = (List) _pool.get(key);
> if (pooled == null)
> {
> pooled = new LinkedList();
> _pool.put(key, pooled);
> }
> SoftReference reference = new SoftReference(value);
> pooled.add(reference);
> _count++;
> }
> Additionally the page pool implementation can use a clean idle pages mechanism using same design as Tapestry 3.0 ThreadJanitor or setting a timestamp when storing pages and a TimerTask and Timer wich receives events on registry shutdown. All this by using hivemind overriding features.

 

java类实现代码:

java 代码
  1. public class SoftReferenceObjectPool implements ObjectPool, ResetEventListener,   
  2.         ReportStatusListener {   
  3.     private String _serviceId;   
  4.   
  5.     private int _count = 0;   
  6.   
  7.     private Map _pool = new HashMap();   
  8.   
  9.     public synchronized Object get(Object key) {   
  10.         List pooled = (List) _pool.get(key);   
  11.         if (pooled == null || pooled.isEmpty())   
  12.             return null;   
  13.         _count--;   
  14.         SoftReference reference = (SoftReference) pooled.remove(0);   
  15.         // returns null if has been cleared by GC same as pool where empty   
  16.         return reference.get();   
  17.     }   
  18.   
  19.     public synchronized void store(Object key, Object value) {   
  20.         List pooled = (List) _pool.get(key);   
  21.         if (pooled == null) {   
  22.             pooled = new LinkedList();   
  23.             _pool.put(key, pooled);   
  24.         }   
  25.         SoftReference reference = new SoftReference(value);   
  26.         pooled.add(reference);   
  27.         _count++;   
  28.     }   
  29.   
  30.     public synchronized void resetEventDidOccur() {   
  31.         _pool.clear();   
  32.   
  33.         _count = 0;   
  34.     }   
  35.   
  36.     public synchronized void reportStatus(ReportStatusEvent event) {   
  37.         event.title(_serviceId);   
  38.   
  39.         event.property("total count", _count);   
  40.   
  41.         event.section("Count by Key");   
  42.   
  43.         Iterator i = _pool.entrySet().iterator();   
  44.   
  45.         while (i.hasNext()) {   
  46.             Map.Entry entry = (Map.Entry) i.next();   
  47.   
  48.             String key = entry.getKey().toString();   
  49.   
  50.             List pooled = (List) entry.getValue();   
  51.   
  52.             event.property(key, pooled.size());   
  53.         }   
  54.     }   
  55.   
  56.     public void setServiceId(String serviceId) {   
  57.         _serviceId = serviceId;   
  58.     }   
  59.   
  60. }  

 

查找了一下hivemind中用到ObjectPool的地方,共有三处,分别替换之。

xml 代码
  1. <implementation service-id="tapestry.page.PagePool">  
  2.      <invoke-factory>  
  3.       <construct class="org.edynasty.tapestry.pool.SoftReferenceObjectPool">  
  4.       <event-listener service-id="tapestry.ResetEventHub"/>  
  5.       <event-listener service-id="tapestry.describe.ReportStatusHub"/>  
  6.       </construct>  
  7.       </invoke-factory>  
  8.   </implementation>  
  9.   
  10. <implementation service-id="tapestry.GlobalObjectPool">  
  11.      <invoke-factory>  
  12.       <construct class="org.edynasty.tapestry.pool.SoftReferenceObjectPool">  
  13.       <event-listener service-id="tapestry.ResetEventHub"/>  
  14.       <event-listener service-id="tapestry.describe.ReportStatusHub"/>  
  15.       </construct>  
  16.       </invoke-factory>  
  17.   </implementation>  
  18.   
  19.   <implementation service-id="tapestry.request.EnginePool">  
  20.      <invoke-factory>  
  21.       <construct class="org.edynasty.tapestry.pool.SoftReferenceObjectPool">  
  22.       <event-listener service-id="tapestry.ResetEventHub"/>  
  23.       <event-listener service-id="tapestry.describe.ReportStatusHub"/>  
  24.       </construct>  
  25.       </invoke-factory>  
  26.   </implementation>  
分享到:
评论

相关推荐

    Tapestry 4 官方文档中版本

    Tapestry 4 官方文档中版本 Tapestry 4 官方文档中版本 Tapestry 4 官方文档中版本

    Tapestry4开发指南.rar

    Tapestry4开发指南.rar Tapestry4开发指南.rar Tapestry4开发指南.rar

    Tapestry4开发指南

    Tapestry4的雏形是Tapestry3.1,Tapestry的作者Howard,不光开发了Tapestry,还同时开发了一个轻量级框架Hivemind。所以Tapestry3.1的开发一开始就处于Hivemind框架之下。后来由于种种原因,Howard没有将Tapestry3.1...

    tapestry官方中文文档及中文字典

    Tapestry 4 官方文档中文版本,包括Tapestry4 Quick Start(2)和Tapestry4 Users Guide(2)两个文档 还有tapestry中文字典等

    tapestry官方中文文档

    Tapestry 4 官方文档中文版本,现在中文资料比较少,和大家共享一下

    深入浅出Tapestry4一书源代码(2)

    Tapestry4 Tapestry4 深入浅出Tapestry4深入浅出Tapestry4深入浅出Tapestry4

    tapestry教程资料文档合集

    Tapestry5最新中文教程.doc 作者 Renat Zubairov & Igor Drobiazko译者 沙晓兰 发布于 2008年7月2日 下午9时30分 社区 Java 主题 Web框架 ----------------------------------------- Tapestry5.1实例教程.pdf ...

    tapestry4开发指南

    tapestry4开发指南,一本很实用的书籍,适合初学者

    tapestry4和5学习资料

    该包包含: Tapestry5最新中文教程.doc tapestry.pdf tapestry开发流程.docx Tapestry开发指南0.8.pdf tapestry用户手册.pdf

    tapestry5中文文档

    tapestry5组件说明使用及登陆修改等简单实例

    深入浅出Tapestry

    资源名称:深入浅出Tapestry内容简介:本书以循序渐进的方式,从Tapestry框架技术的基本概念入手,讲解Tapestry框架在J2EE Web应用程序中的整体架构实现。使读者在学习如何使用Tapestry框架技术的同时,还能够获得在...

    Maven + Tapestry5.3.8 + Spring4.0.5 + Oracle10g

    4)Tree组件的使用,Grid的各种使用(修改样式,排序,隔行换色等等),Loop组件的使用 5)集合Spring4.0.5实现的增删改查 等等 如果你是新接触Tapestry框架的话,会对你有很大的帮助的! 学习Tapestry 可以关注...

    深入浅出tapestry

    本书以循序渐进的方式,从Tapestry框架技术的基本概念入手,讲解Tapestry框架在J2EE Web应用程序中的整体架构实现。使读者在学习如何使用Tapestry框架技术的同时,还能够获得在J2EE Web应用程序中应用Tapestry框架的...

    Tapestry4环境搭建和应用

    Tapestry4开发指南,环境搭建和基本应用

    Tapestry5最新中文入门实例教程

    本文利用Tapestry 5开发一个简单的具有创建/读/更新/删除功能的应用,在创建这个应用的过程中,本文体会到Tapestry带来的开发效率的提升。从多方面来讲解 Tapestry应用,比如应用的页面导航(page navigation)、...

    深入浅出Tapestry4一书源代码(1)

    深入浅出Tapestry4一书源代码 需要自己调试

    MyTapestry4-quickStart.rar_Tapestry4_quickstart tapestry_tapestr

    基于Tapestry4,jdk1.5开发的入门例子

Global site tag (gtag.js) - Google Analytics