KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > JobThread


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
39 import java.util.TimerTask JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Static convenience methods.
45  */

46 class JobThread implements Runnable JavaDoc {
47   private static final L10N L = new L10N(JobThread.class);
48   private static final Logger JavaDoc log = Log.open(JobThread.class);
49
50   private static JobThread _job = new JobThread();
51
52   private ArrayList JavaDoc<Job> _jobs = new ArrayList JavaDoc<Job>();
53   private ArrayList JavaDoc<Job> _runJobs = new ArrayList JavaDoc<Job>();
54
55   private JobThread()
56   {
57   }
58
59   /**
60    * Queues a task.
61    */

62   public static void queue(TimerTask JavaDoc task, long time)
63   {
64     synchronized (_job._jobs) {
65       ArrayList JavaDoc<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   /**
86    * Dequeues a task.
87    */

88   public static void dequeue(TimerTask JavaDoc job)
89   {
90     _job.remove(job);
91   }
92
93   void remove(TimerTask JavaDoc 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 JavaDoc 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 JavaDoc e) {
131       log.log(Level.WARNING, e.toString(), e);
132     }
133       }
134
135       try {
136     thread.sleep(500);
137       } catch (Throwable JavaDoc e) {
138       }
139     }
140   }
141
142   static class Job implements ClassLoaderListener {
143     private TimerTask JavaDoc _task;
144     private ClassLoader JavaDoc _loader;
145     private long _time;
146     private boolean _isDead;
147
148     Job(TimerTask JavaDoc task)
149     {
150       _task = task;
151
152       _loader = Thread.currentThread().getContextClassLoader();
153       
154
155       Environment.addClassLoaderListener(this);
156     }
157
158     public TimerTask JavaDoc getTask()
159     {
160       return _task;
161     }
162
163     /**
164      * Sets the time the job should execute.
165      */

166     public void setTime(long time)
167     {
168       _time = time;
169     }
170
171     /**
172      * Gets the time the job should execute.
173      */

174     public long getTime()
175     {
176       return _time;
177     }
178     
179     /**
180      * Handles the case where a class loader has completed initialization
181      */

182     public void classLoaderInit(DynamicClassLoader loader)
183     {
184     }
185   
186     /**
187      * Handles the case where a class loader is dropped.
188      */

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 JavaDoc thread = Thread.currentThread();
199     ClassLoader JavaDoc 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 JavaDoc thread = new Thread JavaDoc(_job, "jmx-job-thread");
214       thread.setDaemon(true);
215       thread.start();
216     } catch (Throwable JavaDoc e) {
217       log.log(Level.WARNING, e.toString(), e);
218     }
219   }
220 }
221
222
Popular Tags