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.ResultCacheEntryEager; 34 35 42 public final class EagerCacheThread extends Thread 43 { 44 private final ResultCache cache; 45 private long threadWakeUpTime = 0; 46 int refreshCacheRate = 60; 47 int refreshCacheTime = 60 / refreshCacheRate; 48 private Trace logger = Trace 49 .getLogger(EagerCacheThread.class 50 .getName()); 51 private boolean isKilled = false; 52 53 58 public EagerCacheThread(ResultCache cache) 59 { 60 super("EagerCacheThread"); 61 this.cache = cache; 62 } 63 64 69 public long getThreadWakeUpTime() 70 { 71 return threadWakeUpTime; 72 } 73 74 77 public void run() 78 { 79 ResultCacheEntryEager entry; 80 long now; 81 long sleep; 82 ArrayList toRemoveFromEagerCache = new ArrayList (); 84 while (!isKilled) 85 { 86 synchronized (this) 87 { 88 try 89 { 90 threadWakeUpTime = 0; 91 if (cache.getEagerCache().isEmpty()) 92 { if (logger.isDebugEnabled()) 94 logger.debug(Translate.get("cachethread.cache.empty.sleeping")); 95 wait(); 96 } 97 else 98 { now = System.currentTimeMillis(); 100 for (Iterator iter = cache.getEagerCache().iterator(); iter 101 .hasNext();) 102 { 103 entry = (ResultCacheEntryEager) iter.next(); 104 if (entry.getDeadline() < now) 105 { toRemoveFromEagerCache.add(entry); 107 continue; 108 } 109 110 if ((threadWakeUpTime == 0) 112 || (entry.getDeadline() < threadWakeUpTime)) 113 threadWakeUpTime = entry.getDeadline(); 114 } 115 116 int size = toRemoveFromEagerCache.size(); 118 for (int i = 0; i < size; i++) 119 { 120 entry = (ResultCacheEntryEager) toRemoveFromEagerCache.get(i); 121 if (logger.isDebugEnabled()) 122 logger.debug(Translate.get( 123 "cachethread.remove.entry.from.cache", entry.getRequest() 124 .getSQL())); 125 try 126 { 127 cache.removeFromCache(entry.getRequest()); 128 } 129 catch (Exception e) 130 { 131 logger.warn("cachethread.remove.entry.error", e); 132 } 133 try 134 { 135 cache.getEagerCache().remove(entry); 136 } 137 catch (Exception e) 138 { 139 logger.warn("cachethread.remove.entry.error", e); 140 } 141 } 142 toRemoveFromEagerCache.clear(); 143 if (threadWakeUpTime == 0) 144 { continue; 147 } 148 else 149 { sleep = (threadWakeUpTime - now) / 1000 + refreshCacheTime; 151 if (logger.isDebugEnabled()) 152 { 153 logger.debug(Translate.get("cachethread.sleeping", sleep)); 154 } 155 sleep = (sleep) * 1000; 156 wait(sleep); 157 } 158 } 159 } 160 catch (Exception e) 161 { 162 logger.warn(e.getMessage(), e); 163 } 164 } 165 } 166 } 167 168 171 public synchronized void shutdown() 172 { 173 isKilled = true; 174 notify(); 175 } 176 177 } | Popular Tags |