1 28 29 package com.caucho.jmx; 30 31 import com.caucho.loader.ClassLoaderListener; 32 import com.caucho.loader.DynamicClassLoader; 33 import com.caucho.loader.Environment; 34 import com.caucho.log.Log; 35 import com.caucho.util.Alarm; 36 import com.caucho.util.L10N; 37 38 import java.util.ArrayList ; 39 import java.util.TimerTask ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 43 46 class JobThread implements Runnable { 47 private static final L10N L = new L10N(JobThread.class); 48 private static final Logger log = Log.open(JobThread.class); 49 50 private static JobThread _job = new JobThread(); 51 52 private ArrayList <Job> _jobs = new ArrayList <Job>(); 53 private ArrayList <Job> _runJobs = new ArrayList <Job>(); 54 55 private JobThread() 56 { 57 } 58 59 62 public static void queue(TimerTask task, long time) 63 { 64 synchronized (_job._jobs) { 65 ArrayList <Job> jobs = _job._jobs; 66 67 for (int i = jobs.size() - 1; i >= 0; i--) { 68 Job oldJob = jobs.get(i); 69 70 if (oldJob.getTask() == task) { 71 if (time < oldJob.getTime()) 72 oldJob.setTime(time); 73 74 return; 75 } 76 } 77 78 Job job = new Job(task); 79 job.setTime(time); 80 81 _job._jobs.add(job); 82 } 83 } 84 85 88 public static void dequeue(TimerTask job) 89 { 90 _job.remove(job); 91 } 92 93 void remove(TimerTask task) 94 { 95 synchronized (_jobs) { 96 for (int i = _jobs.size() - 1; i >= 0; i--) { 97 Job job = _jobs.get(i); 98 99 if (job.getTask() == task) 100 _jobs.remove(i); 101 } 102 } 103 } 104 105 public void run() 106 { 107 Thread thread = Thread.currentThread(); 108 109 while (true) { 110 long now = Alarm.getCurrentTime(); 111 112 _runJobs.clear(); 113 114 synchronized (_jobs) { 115 for (int i = _jobs.size() - 1; i >= 0; i--) { 116 Job job = _jobs.get(i); 117 118 if (job.getTime() <= now) { 119 _runJobs.add(job); 120 _jobs.remove(i); 121 } 122 } 123 } 124 125 for (int i = _runJobs.size() - 1; i >= 0; i--) { 126 Job job = _runJobs.get(i); 127 128 try { 129 job.run(); 130 } catch (Throwable e) { 131 log.log(Level.WARNING, e.toString(), e); 132 } 133 } 134 135 try { 136 thread.sleep(500); 137 } catch (Throwable e) { 138 } 139 } 140 } 141 142 static class Job implements ClassLoaderListener { 143 private TimerTask _task; 144 private ClassLoader _loader; 145 private long _time; 146 private boolean _isDead; 147 148 Job(TimerTask task) 149 { 150 _task = task; 151 152 _loader = Thread.currentThread().getContextClassLoader(); 153 154 155 Environment.addClassLoaderListener(this); 156 } 157 158 public TimerTask getTask() 159 { 160 return _task; 161 } 162 163 166 public void setTime(long time) 167 { 168 _time = time; 169 } 170 171 174 public long getTime() 175 { 176 return _time; 177 } 178 179 182 public void classLoaderInit(DynamicClassLoader loader) 183 { 184 } 185 186 189 public void classLoaderDestroy(DynamicClassLoader loader) 190 { 191 _isDead = true; 192 JobThread.dequeue(_task); 193 } 194 195 public void run() 196 { 197 if (! _isDead) { 198 Thread thread = Thread.currentThread(); 199 ClassLoader oldLoader = thread.getContextClassLoader(); 200 201 try { 202 thread.setContextClassLoader(_loader); 203 _task.run(); 204 } finally { 205 thread.setContextClassLoader(oldLoader); 206 } 207 } 208 } 209 } 210 211 static { 212 try { 213 Thread thread = new Thread (_job, "jmx-job-thread"); 214 thread.setDaemon(true); 215 thread.start(); 216 } catch (Throwable e) { 217 log.log(Level.WARNING, e.toString(), e); 218 } 219 } 220 } 221 222 | Popular Tags |