From 59097efcdb9bca4adbaacf8db7fc37b2681f42be Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Wed, 2 Aug 2017 14:55:32 +0200 Subject: [PATCH] Changed to Executor service from timer --- src/zutil/db/bean/DBBeanCache.java | 64 +++++++++++++++++------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/zutil/db/bean/DBBeanCache.java b/src/zutil/db/bean/DBBeanCache.java index a2093f9..5f15a11 100755 --- a/src/zutil/db/bean/DBBeanCache.java +++ b/src/zutil/db/bean/DBBeanCache.java @@ -8,6 +8,10 @@ import java.util.Map; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -20,7 +24,7 @@ class DBBeanCache { /** A cache for detecting recursion **/ private static Map, Map> cache = new ConcurrentHashMap<>(); - private static Timer timer; + private static ScheduledExecutorService executor; static { @@ -41,18 +45,18 @@ class DBBeanCache { * This function cancels the internal cache garbage collector in DBBean. * GBC is enabled by default */ - public static void enableBeanGBC(boolean enable){ + public static synchronized void enableBeanGBC(boolean enable){ if(enable){ - if( timer == null ){ - timer = new Timer( true ); // Run as daemon - timer.schedule( new DBBeanGarbageCollector(), CACHE_DATA_TTL, CACHE_DATA_TTL *2 ); + if( executor == null ){ + executor = Executors.newSingleThreadScheduledExecutor(); + executor.scheduleAtFixedRate( new DBBeanGarbageCollector(), CACHE_DATA_TTL, CACHE_DATA_TTL *2, TimeUnit.MILLISECONDS ); logger.fine("Bean garbage collection daemon enabled"); } } else { - if (timer != null) { - timer.cancel(); - timer = null; + if (executor != null) { + executor.shutdown(); + executor = null; logger.fine("Bean garbage collection daemon disabled"); } } @@ -61,31 +65,35 @@ class DBBeanCache { /** * This class acts as an garbage collector that removes old DBBeans */ - private static class DBBeanGarbageCollector extends TimerTask { - public void run(){ - if( cache == null ){ - logger.severe("DBBeanSQLResultHandler not initialized, stopping DBBeanGarbageCollector timer."); - this.cancel(); - return; - } + private static class DBBeanGarbageCollector implements Runnable { + public void run() { + try { + if (cache == null) { + logger.severe("DBBeanSQLResultHandler not initialized, stopping DBBeanGarbageCollector timer."); + enableBeanGBC(false); + return; + } - int removed = 0; - for(Object classKey : cache.keySet()){ - if( classKey == null ) continue; + int removed = 0; + for (Object classKey : cache.keySet()) { + if (classKey == null) continue; - Map class_cache = cache.get(classKey); - for(Iterator> it = class_cache.entrySet().iterator(); it.hasNext(); ) { - Map.Entry entry = it.next(); - if( entry.getKey() == null ) continue; - // Check if session is still valid - if( entry.getValue().bean.get() == null ){ - it.remove(); - removed++; + Map class_cache = cache.get(classKey); + for (Iterator> it = class_cache.entrySet().iterator(); it.hasNext(); ) { + Map.Entry entry = it.next(); + if (entry.getKey() == null) continue; + // Check if session is still valid + if (entry.getValue().bean.get() == null) { + it.remove(); + removed++; + } } } + if (removed > 0) + logger.info("DBBean GarbageCollector has cleared " + removed + " beans from cache."); + } catch (Exception e) { + logger.log(Level.SEVERE, "DBBeanGarbageCollector thread has crashed", e); } - if (removed > 0) - logger.info("DBBean GarbageCollector has cleared "+removed+" beans from cache."); } }