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