1 46 50 package org.mr.core.util; 51 52 import java.util.ArrayList ; 53 import java.util.HashMap ; 54 import java.util.Iterator ; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.mr.MantaAgent; 59 60 68 public class TimeoutTimer extends Thread { 69 70 private long minTimeToLive ; 72 private long systemMinTTL; 74 double ttlFactor; 76 77 int numOfTimeouts = 0; 78 HashMap timeouts = new HashMap (); 80 HashMap timeoutables = new HashMap (); 82 Log log ; 83 84 Timeoutable singleTimeoutable; 88 Object singleEvent; 89 long singleTtl; 90 91 94 public TimeoutTimer(String name) { 95 this(name, Long.parseLong(MantaAgent.getInstance().getSingletonRepository().getConfigManager().getStringProperty("timer.min_ttl","1000")) 96 ,Double.parseDouble(MantaAgent.getInstance().getSingletonRepository().getConfigManager().getStringProperty("timer.ttl_factor","0.1")) ); 97 98 } 99 100 105 public TimeoutTimer(String name ,long minTTL , double ttlFactor) { 106 log=LogFactory.getLog("TimeoutTimer"); 107 this.setName("TimeoutMonitor"+name); 108 109 this.systemMinTTL =minTTL; 110 this.ttlFactor = ttlFactor; 111 minTimeToLive = systemMinTTL; 112 } 113 114 120 public synchronized void addTimeout(Timeoutable timeoutable ,Object event , long ttl) { 121 if(timeoutable == null || event== null) 122 throw new IllegalArgumentException ("Timeoutable and event object must not be null "); 123 if(!this.isAlive()) 124 this.start(); 125 if(minTimeToLive > ttl ) this.setMinTimeToLive(ttl); 126 long now =SystemTime.currentTimeMillis(); 127 if(numOfTimeouts == 0){ 128 singleTimeoutable = timeoutable; 129 singleEvent = event; 130 singleTtl = ttl+now; 131 }else{ 132 timeouts.put(event , new Long (ttl+now)); 133 timeoutables.put(event , timeoutable); 134 } 135 numOfTimeouts ++; 136 137 } 138 141 public synchronized void removeTimeout(Object event) { 142 if(event == null) 143 return; 144 Object removed = null; 145 if(singleEvent == event){ 146 removed = event; 147 singleEvent = null; 148 singleTimeoutable = null; 149 }else{ 150 removed = timeouts.remove(event ); 151 timeoutables.remove(event ); 152 } 153 154 if(removed != null) 155 numOfTimeouts--; 156 } 157 158 161 public long getMinTimeToLive() { 162 return minTimeToLive; 163 } 164 165 168 private void setMinTimeToLive(long minTimeToLive) { 169 if(minTimeToLive < systemMinTTL){ 170 if(log.isErrorEnabled()){ 171 log.error(" Not setting 'time to live' because it seems too short: ttl = "+minTimeToLive + ", system minimal ttl ="+systemMinTTL+"."); 172 } 173 throw new IllegalArgumentException (" Time to live seems to short ttl = "+minTimeToLive + ", system minimal ttl ="+systemMinTTL+"."); 174 } 175 this.minTimeToLive = minTimeToLive; 176 } 177 178 179 public void run(){ 180 while(true){ 181 try{ 182 long sleep = (long)(minTimeToLive * ttlFactor); 183 Thread.sleep(sleep); 184 checkForTimeouts(); 185 }catch(Throwable t ){ 186 if(log.isFatalEnabled()) 187 log.fatal("Exception in connection timeout monitor, still running ",t); 188 } 189 } 190 } 192 ArrayList timeoutablesList =new ArrayList (); 193 ArrayList timeoutablesEventList =new ArrayList (); 194 195 199 public void checkForTimeouts(){ 200 201 timeoutablesList.clear(); 202 timeoutablesEventList.clear(); 203 synchronized(this){ 204 Iterator iter = this.timeouts.keySet().iterator(); 206 long now =SystemTime.currentTimeMillis(); 207 while(iter.hasNext()){ 208 Object event = iter.next(); 209 Long ttl =(Long ) this.timeouts.get(event); 210 if(ttl.longValue() < now){ 211 timeoutablesEventList.add(event); 212 timeoutablesList.add(this.timeoutables.remove(event)); 213 numOfTimeouts--; 214 } 215 } int size =timeoutablesEventList.size(); 219 for(int index = 0 ;index<size ; index++){ 220 this.timeouts.remove(timeoutablesEventList.get(index)); 221 } 222 223 if(singleEvent != null && singleTtl < now){ 225 timeoutablesEventList.add(singleEvent); 226 timeoutablesList.add(singleTimeoutable); 227 singleEvent =null; 228 singleTimeoutable = null; 229 numOfTimeouts--; 230 } 231 232 } 234 int size =timeoutablesEventList.size(); 237 for(int index = 0 ;index<size ; index++){ 238 Timeoutable timeoutable = (Timeoutable)timeoutablesList.get(index); 239 if(timeoutable != null) 240 timeoutable.timeout(timeoutablesEventList.get(index)); 241 } 242 243 } 245 246 247 } 248 | Popular Tags |