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,11 +65,12 @@ 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() {
try {
if (cache == null) { 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;
} }
@ -86,6 +91,9 @@ class DBBeanCache {
} }
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);
}
} }
} }