1 package org.xvsm.internal;
2
3 import java.util.concurrent.ExecutorService;
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.TimeUnit;
6
7 import org.xvsm.configuration.ConfigurationManager;
8 import org.xvsm.internal.exceptions.FatalException;
9 import org.xvsm.internal.tasks.Task;
10
11 /***
12 *
13 * @author Christian Schreiber, Michael Proestler
14 *
15 */
16 public final class ReplySenderPool {
17
18 /***
19 * True if the Pool is still running. False otherwise.
20 */
21 private static boolean running = true;
22
23 /***
24 * The maximal amount of Threads in the EventProcessingPool.
25 */
26 private static int maxThreads;
27
28 /***
29 * The ExectorService of the EventProcessingPool.
30 */
31 private static ExecutorService service;
32
33 /***
34 * Initialize the ReplySenderPool
35 */
36 static {
37 init();
38 }
39
40 /***
41 * Default constructor. Private because utility classes must not have a
42 * public default constructor.
43 */
44 private ReplySenderPool() {
45 }
46
47 /***
48 * Tries to dispatch the given runnable to a thread. If all threads are
49 * working, the runnable will be put in the internal queue.
50 *
51 * @param task
52 * The task to be run by an thread.
53 */
54 public static void execute(Task task) {
55 service.execute(task);
56 }
57
58 /***
59 * Initiates an orderly shutdown in which previously submitted tasks are
60 * executed, but no new tasks will be accepted.
61 *
62 */
63 public static void shutdown() {
64 if (running) {
65 service.shutdown();
66 try {
67
68 if (!service.awaitTermination(60, TimeUnit.SECONDS)) {
69 service.shutdownNow();
70 running = false;
71
72 if (!service.awaitTermination(60, TimeUnit.SECONDS)) {
73 throw new FatalException("Pool did not terminate");
74 }
75 }
76 running = false;
77 } catch (InterruptedException ie) {
78
79 service.shutdownNow();
80 running = false;
81
82 Thread.currentThread().interrupt();
83 }
84 }
85 }
86
87 /***
88 * Persudes the EventProcessingPool to re-read the configuration file and
89 * restart the pool with the new configuration.
90 *
91 */
92 public static void init() {
93 maxThreads = ConfigurationManager.getInstance().getIntegerSetting(
94 "ReplySenderThreads");
95
96 service = Executors.newCachedThreadPool();
97
98 }
99
100 }