1 24 25 package org.objectweb.cjdbc.controller.cache.result.threads; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 30 import org.objectweb.cjdbc.common.i18n.Translate; 31 import org.objectweb.cjdbc.common.log.Trace; 32 import org.objectweb.cjdbc.controller.cache.result.ResultCache; 33 import org.objectweb.cjdbc.controller.cache.result.entries.ResultCacheEntryRelaxed; 34 35 42 public final class RelaxedCacheThread extends Thread 43 { 44 private long threadWakeUpTime = 0; 45 private final ResultCache cache; 46 int refreshCacheRate = 60; 47 int refreshCacheTime = 60 / refreshCacheRate; 48 private Trace logger = Trace 49 .getLogger(RelaxedCacheThread.class 50 .getName()); 51 52 private boolean isKilled = false; 53 54 59 public RelaxedCacheThread(ResultCache cache) 60 { 61 super("RelaxedCacheThread"); 62 this.cache = cache; 63 } 64 65 71 public RelaxedCacheThread(ResultCache cache, int refreshCacheRate) 72 { 73 this(cache); 74 this.refreshCacheRate = refreshCacheRate; 75 } 76 77 82 public long getThreadWakeUpTime() 83 { 84 return threadWakeUpTime; 85 } 86 87 90 public void run() 91 { 92 ResultCacheEntryRelaxed entry; 93 long now; 94 long sleep; 95 ArrayList toRemoveFromRelaxedCache = new ArrayList (); 97 while (!isKilled) 98 { 99 synchronized (this) 100 { 101 try 102 { 103 threadWakeUpTime = 0; 104 if (cache.getRelaxedCache().isEmpty()) 105 { if (logger.isDebugEnabled()) 107 logger.debug(Translate.get("cachethread.cache.empty.sleeping")); 108 wait(); 109 } 110 else 111 { now = System.currentTimeMillis(); 113 for (Iterator iter = cache.getRelaxedCache().iterator(); iter 114 .hasNext();) 115 { 116 entry = (ResultCacheEntryRelaxed) iter.next(); 117 if (entry.getDeadline() < now) 118 { if (entry.isDirty() || !entry.getKeepIfNotDirty()) 120 { toRemoveFromRelaxedCache.add(entry); 122 continue; 123 } 124 else 125 entry.setDeadline(now + entry.getTimeout()); 127 } 128 129 if (threadWakeUpTime == 0 131 || (entry.getDeadline() < threadWakeUpTime)) 132 threadWakeUpTime = entry.getDeadline(); 133 } 134 135 int size = toRemoveFromRelaxedCache.size(); 137 for (int i = 0; i < size; i++) 138 { 139 entry = (ResultCacheEntryRelaxed) toRemoveFromRelaxedCache.get(i); 140 if (logger.isDebugEnabled()) 141 logger.debug(Translate.get( 142 "cachethread.remove.entry.from.cache", entry.getRequest() 143 .getSQL())); 144 this.cache.removeFromCache(entry.getRequest()); 145 cache.getRelaxedCache().remove(entry); 146 } 147 toRemoveFromRelaxedCache.clear(); 148 if (threadWakeUpTime == 0) 149 { continue; 152 } 153 else 154 { sleep = (threadWakeUpTime - now) / 1000 + refreshCacheTime; 156 if (logger.isDebugEnabled()) 157 { 158 logger.debug(Translate.get("cachethread.sleeping", sleep)); 159 } 160 sleep = (sleep) * 1000; 161 wait(sleep); 162 } 163 } 164 } 165 catch (Exception e) 166 { 167 logger.warn(e.getMessage(), e); 168 } 169 } 170 } 171 } 172 173 176 public synchronized void shutdown() 177 { 178 isKilled = true; 179 notify(); 180 } 181 182 } | Popular Tags |