Changed to Executor service from timer

This commit is contained in:
Ziver Koc 2017-08-02 14:55:32 +02:00
parent f1352cb58a
commit 59097efcdb

View file

@ -8,6 +8,10 @@ import java.util.Map;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap; 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; import java.util.logging.Logger;
/** /**
@ -20,7 +24,7 @@ class DBBeanCache {
/** A cache for detecting recursion **/ /** A cache for detecting recursion **/
private static Map<Class<?>, Map<Long, CacheItem>> cache = private static Map<Class<?>, Map<Long, CacheItem>> cache =
new ConcurrentHashMap<>(); new ConcurrentHashMap<>();
private static Timer timer; private static ScheduledExecutorService executor;
static { static {
@ -41,18 +45,18 @@ class DBBeanCache {
* This function cancels the internal cache garbage collector in DBBean. * This function cancels the internal cache garbage collector in DBBean.
* GBC is enabled by default * GBC is enabled by default
*/ */
public static void enableBeanGBC(boolean enable){ public static synchronized void enableBeanGBC(boolean enable){
if(enable){ if(enable){
if( timer == null ){ if( executor == null ){
timer = new Timer( true ); // Run as daemon executor = Executors.newSingleThreadScheduledExecutor();
timer.schedule( new DBBeanGarbageCollector(), CACHE_DATA_TTL, CACHE_DATA_TTL *2 ); executor.scheduleAtFixedRate( new DBBeanGarbageCollector(), CACHE_DATA_TTL, CACHE_DATA_TTL *2, TimeUnit.MILLISECONDS );
logger.fine("Bean garbage collection daemon enabled"); logger.fine("Bean garbage collection daemon enabled");
} }
} }
else { else {
if (timer != null) { if (executor != null) {
timer.cancel(); executor.shutdown();
timer = null; executor = null;
logger.fine("Bean garbage collection daemon disabled"); logger.fine("Bean garbage collection daemon disabled");
} }
} }
@ -61,31 +65,35 @@ class DBBeanCache {
/** /**
* This class acts as an garbage collector that removes old DBBeans * This class acts as an garbage collector that removes old DBBeans
*/ */
private static class DBBeanGarbageCollector extends TimerTask { private static class DBBeanGarbageCollector implements Runnable {
public void run(){ public void run() {
if( cache == null ){ try {
if (cache == null) {
logger.severe("DBBeanSQLResultHandler not initialized, stopping DBBeanGarbageCollector timer."); logger.severe("DBBeanSQLResultHandler not initialized, stopping DBBeanGarbageCollector timer.");
this.cancel(); enableBeanGBC(false);
return; return;
} }
int removed = 0; int removed = 0;
for(Object classKey : cache.keySet()){ for (Object classKey : cache.keySet()) {
if( classKey == null ) continue; if (classKey == null) continue;
Map<Long, CacheItem> class_cache = cache.get(classKey); Map<Long, CacheItem> class_cache = cache.get(classKey);
for(Iterator<Map.Entry<Long, CacheItem>> it = class_cache.entrySet().iterator(); it.hasNext(); ) { for (Iterator<Map.Entry<Long, CacheItem>> it = class_cache.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Long, CacheItem> entry = it.next(); Map.Entry<Long, CacheItem> entry = it.next();
if( entry.getKey() == null ) continue; if (entry.getKey() == null) continue;
// Check if session is still valid // Check if session is still valid
if( entry.getValue().bean.get() == null ){ if (entry.getValue().bean.get() == null) {
it.remove(); it.remove();
removed++; removed++;
} }
} }
} }
if (removed > 0) if (removed > 0)
logger.info("DBBean GarbageCollector has cleared "+removed+" beans from cache."); logger.info("DBBean GarbageCollector has cleared " + removed + " beans from cache.");
} catch (Exception e) {
logger.log(Level.SEVERE, "DBBeanGarbageCollector thread has crashed", e);
}
} }
} }