1 18 package org.objectweb.perseus.cache.lib; 19 20 import org.objectweb.perseus.cache.replacement.api.ReplacementManager; 21 import org.objectweb.perseus.cache.api.CacheException; 22 import org.objectweb.util.monolog.api.Logger; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 25 26 32 public class BackgroundCleaner implements Runnable { 33 34 private Object wakeup; 35 private boolean runnning; 36 private ReplacementManager rm; 37 private Thread thread; 38 private int size; 39 private Logger logger; 40 41 public BackgroundCleaner(ReplacementManager rm, Logger logger) { 42 this.rm = rm; 43 wakeup = new Object (); 44 runnning = true; 45 this.logger = logger; 46 thread = new Thread (this); 47 thread.setDaemon(true); 48 thread.start(); 49 } 50 51 public Thread getThread() { 52 return thread; 53 } 54 55 public void restart() { 56 if (!runnning) { 57 synchronized(wakeup) { 58 if (!runnning) { 59 if (logger != null && logger.isLoggable(BasicLevel.INFO)) { 60 logger.log(BasicLevel.INFO, "BackgroundCleaner restart"); 61 } 62 runnning = true; 63 wakeup.notify(); 64 } 65 } 66 } 67 } 68 69 public void stop() { 70 if (runnning) { 71 synchronized(wakeup) { 72 if (runnning) { 73 if (logger != null && logger.isLoggable(BasicLevel.INFO)) { 74 logger.log(BasicLevel.INFO, "BackgroundCleaner stop"); 75 } 76 runnning = false; 77 wakeup.notify(); 78 } 79 } 80 } 81 } 82 83 public void backgroungCleanup(int size) { 84 synchronized(wakeup) { 85 this.size = size; 86 wakeup.notify(); 87 } 88 } 89 90 public void run() { 91 while(runnning) { 92 synchronized(wakeup) { 93 try { 94 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 95 logger.log(BasicLevel.DEBUG, "Wait next wake up"); 96 } 97 wakeup.wait(); 98 } catch (InterruptedException e) { 99 } 100 } 101 if (runnning) { 102 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 103 logger.log(BasicLevel.DEBUG, 104 "Cleanup the cache in background (" + size +")"); 105 } 106 try { 107 rm.forceFree(size); 108 } catch (CacheException e) { 109 if (logger != null) { 110 logger.log(BasicLevel.ERROR, 111 "Impossible to cleanup the cache (" 112 + size + "): ", e); 113 } 114 } 115 } 116 } 117 if (logger != null && logger.isLoggable(BasicLevel.INFO)) { 118 logger.log(BasicLevel.INFO, "BackgroundCleaner end"); 119 } 120 } 121 } 122 | Popular Tags |