1 23 24 package com.sun.enterprise.util.scheduler; 31 32 import com.sun.enterprise.util.ApproximateClock; 33 import java.util.logging.Logger ; 35 import java.util.logging.Level ; 36 import com.sun.logging.LogDomains; 37 39 52 public class PeriodicEventScheduler 53 implements Runnable 54 { 55 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 57 61 62 protected Thread _thread; 63 64 private static PeriodicEventScheduler _instance = null; 65 66 private static Object instanceLock = new Object (); 67 68 protected TimedTaskList sortedList; 69 70 71 protected boolean bRun; 72 73 protected boolean bDebug=false; 74 75 protected transient long counter=0; 76 77 protected long delay_time_approx = 100; 78 79 protected ApproximateClock clock = null; 80 81 static 82 { 83 _instance = new PeriodicEventScheduler(); 84 } 85 86 90 public static PeriodicEventScheduler getInstance() 91 { 92 return _instance; 93 } 94 95 96 99 private PeriodicEventScheduler() 100 { 101 super(); 102 bRun = true; 103 sortedList = new TimedTaskList(); 104 clock = new ApproximateClock (delay_time_approx); 105 counter = getTime(); 106 _thread = new Thread (this, "PeriodicEventScheduler"); 107 _thread.setDaemon(true); 108 _thread.start(); 109 } 110 111 115 private long getTime() 116 { 117 return clock.getActualTime(); } 119 120 129 public synchronized boolean addTimeRepeatableTask(PeriodicallyServicable obj, int startingTime) 130 { 131 if(startingTime < 0 || obj.getFrequency() < 1) 132 { 133 if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"PeriodicEventScheduler::addTimeRepeatableTask() rejected task" + obj.toString()); 136 138 return false; 139 } 140 boolean bool = sortedList.addTask(obj, startingTime, counter); 141 synchronized(instanceLock) 142 { 143 instanceLock.notify(); 144 } 145 return bool; 146 } 147 148 153 public synchronized boolean removeTimeRepeatableTask(PeriodicallyServicable obj) 154 { 155 if(executingTask.equals(obj)) 156 { 157 removeExecutingTask=true; 158 return true; 159 } 160 else 161 return sortedList.removeTask(obj); 162 } 163 164 169 protected synchronized boolean insertSorted(TaskData taskObj) 170 { 171 return sortedList.insertTask(taskObj); 172 } 173 174 175 private PeriodicallyServicable executingTask=null; 176 177 private boolean removeExecutingTask=false; 178 179 182 public void run() 183 { 184 TaskData task=null; 185 186 while(bRun) 187 { 188 try 189 { 190 task = sortedList.getFirstTask(); 195 if(null==task) 200 { 201 synchronized(instanceLock) 202 { 203 instanceLock.wait(); 204 continue; } 206 } 207 208 executingTask=task.obj; 209 210 counter = getTime(); 212 long sleepingTime = task.abs_execute_time - counter; 213 if(sleepingTime > 0L) 214 { 215 try 216 { 217 if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"Current time=" + (int)(counter/1000) + ", Sleeping for " + sleepingTime + " msec."); 220 Thread.sleep( sleepingTime ); 222 } 223 catch(InterruptedException ieInner) 224 { 225 if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"PeriodicEventScheduler::run() > " + ieInner); 228 } 230 }else 231 { 232 if (!task.obj.getExecutionTolerance(Math.abs(sleepingTime)) ) 235 { 236 if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"Missed scheduling for " + task.obj.toString()); 239 continue; 241 } else 242 { 243 if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"Executing after missing scheduling for " + task.obj.toString()); 246 } 248 } 249 250 task.obj.service(); 253 254 }catch(InterruptedException ieOuter) 255 { 256 if(com.sun.enterprise.util.logging.Debug.enabled) _logger.log(Level.FINE,"PeriodicEventScheduler::run() > " + ieOuter); 259 } 261 catch(Exception e) 262 { 263 System.out.println("PeriodicEventScheduler::run() > " + e); 264 _logger.log(Level.WARNING,"iplanet_util.generic_exception",e); 266 } 268 finally 269 { 270 if (null!=task) 271 { 272 counter = getTime(); 273 task.abs_execute_time = counter; 275 if(!removeExecutingTask) 280 insertSorted(task); 281 else 282 removeExecutingTask=false; 283 executingTask=null; 284 } 285 } 286 } } 288 289 public String toString() 290 { 291 return "[PeriodicEventScheduler: " + sortedList.toString() + "]"; 292 } 293 } 294 295 | Popular Tags |