1 package org.xvsm.internal;
2
3 import java.util.concurrent.ScheduledFuture;
4 import java.util.concurrent.ScheduledThreadPoolExecutor;
5 import java.util.concurrent.TimeUnit;
6
7 import org.xvsm.configuration.ConfigurationManager;
8
9 /***
10 *
11 * @author Christian Schreiber, Michael Proestler
12 *
13 */
14 public final class TimeoutSchedulerPool {
15
16 /***
17 * True if the Pool is still running. False otherwise.
18 */
19 private static boolean running = true;
20
21 /***
22 * Default constructor.
23 *
24 */
25 private TimeoutSchedulerPool() {
26
27 }
28
29 /***
30 * The delaytime of the scheduler.
31 */
32 private static long delay;
33
34 /***
35 * number of threads in the pool.
36 */
37 private static int threadCount;
38
39 /***
40 * The thread pool for timeout management.
41 */
42 private static ScheduledThreadPoolExecutor timeoutScheduler;
43
44 /***
45 * Initialize the TimeoutSchedulerPool.
46 */
47 static {
48 init();
49 }
50
51 /***
52 * Schedules a new task.
53 *
54 * @param r
55 * the task to schedule.
56 */
57 public static ScheduledFuture<?> schedule(Runnable r) {
58 return TimeoutSchedulerPool.timeoutScheduler.scheduleWithFixedDelay(r,
59 delay, delay, TimeUnit.MILLISECONDS);
60 }
61
62 /***
63 * Removes a task.
64 *
65 * @param r
66 * the task to remove
67 */
68 public static void remove(Runnable r) {
69 TimeoutSchedulerPool.timeoutScheduler.remove(r);
70 }
71
72 /***
73 * Initiates an orderly shutdown in which previously submitted tasks are
74 * executed, but no new tasks will be accepted.
75 *
76 */
77 public static void shutdown() {
78 if (running) {
79 TimeoutSchedulerPool.timeoutScheduler.shutdownNow();
80 if (TimeoutSchedulerPool.timeoutScheduler.isTerminated()) {
81 running = false;
82 }
83 }
84 }
85
86 /***
87 * Persudes the TimeoutSchedularPool to re-read the configuration file and
88 * restart the pool with the new configuration.
89 *
90 */
91 public static synchronized void init() {
92 threadCount = ConfigurationManager.getInstance().getIntegerSetting(
93 "SchedulerMaxThreads");
94 delay = ConfigurationManager.getInstance().getLongSetting(
95 "SchedulerTimeoutDelay");
96 timeoutScheduler = new ScheduledThreadPoolExecutor(threadCount);
97 }
98 }