KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > workqueue > WorkQueueManagerImpl


1 package org.objectweb.celtix.bus.workqueue;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import org.objectweb.celtix.Bus;
6 import org.objectweb.celtix.bus.busimpl.ComponentCreatedEvent;
7 import org.objectweb.celtix.bus.busimpl.ComponentRemovedEvent;
8 import org.objectweb.celtix.workqueue.AutomaticWorkQueue;
9 import org.objectweb.celtix.workqueue.WorkQueueManager;
10
11 public class WorkQueueManagerImpl implements WorkQueueManager {
12
13     private static final Logger JavaDoc LOG =
14         Logger.getLogger(WorkQueueManagerImpl.class.getName());
15
16     ThreadingModel threadingModel = ThreadingModel.MULTI_THREADED;
17     AutomaticWorkQueue autoQueue;
18     boolean inShutdown;
19     Bus bus;
20
21     public WorkQueueManagerImpl(Bus b) {
22         bus = b;
23     }
24
25     /*
26      * (non-Javadoc)
27      *
28      * @see org.objectweb.celtix.workqueue.WorkQueueManager#getAutomaticWorkQueue()
29      */

30     public synchronized AutomaticWorkQueue getAutomaticWorkQueue() {
31         if (autoQueue == null) {
32             autoQueue = createAutomaticWorkQueue();
33             bus.sendEvent(new ComponentCreatedEvent(this));
34         }
35         return autoQueue;
36     }
37
38     /*
39      * (non-Javadoc)
40      *
41      * @see org.objectweb.celtix.workqueue.WorkQueueManager#getThreadingModel()
42      */

43     public ThreadingModel getThreadingModel() {
44         return threadingModel;
45     }
46
47     /*
48      * (non-Javadoc)
49      *
50      * @see org.objectweb.celtix.workqueue.WorkQueueManager#setThreadingModel(
51      * org.objectweb.celtix.workqueue.WorkQueueManager.ThreadingModel)
52      */

53     public void setThreadingModel(ThreadingModel model) {
54         threadingModel = model;
55     }
56
57     /*
58      * (non-Javadoc)
59      *
60      * @see org.objectweb.celtix.workqueue.WorkQueueManager#shutdown(boolean)
61      */

62     public synchronized void shutdown(boolean processRemainingTasks) {
63         inShutdown = true;
64         if (autoQueue != null) {
65             autoQueue.shutdown(processRemainingTasks);
66         }
67
68         //sent out remove event.
69

70         bus.sendEvent(new ComponentRemovedEvent(this));
71        
72         synchronized (this) {
73             notifyAll();
74         }
75     }
76
77     public void run() {
78         synchronized (this) {
79             while (!inShutdown) {
80                 try {
81                     wait();
82                 } catch (InterruptedException JavaDoc ex) {
83                     // ignore
84
}
85             }
86             while (autoQueue != null && !autoQueue.isShutdown()) {
87                 try {
88                     Thread.sleep(100);
89                 } catch (InterruptedException JavaDoc ex) {
90                     // ignore
91
}
92             }
93         }
94         for (java.util.logging.Handler JavaDoc h : LOG.getHandlers()) {
95             h.flush();
96         }
97
98         //sent out creation event.
99

100         
101     }
102
103     private AutomaticWorkQueue createAutomaticWorkQueue() {
104       
105         // Configuration configuration = bus.getConfiguration();
106

107         // configuration.getInteger("threadpool:initial_threads");
108
int initialThreads = 1;
109
110         // int lwm = configuration.getInteger("threadpool:low_water_mark");
111
int lwm = 5;
112
113         // int hwm = configuration.getInteger("threadpool:high_water_mark");
114
int hwm = 25;
115
116         // configuration.getInteger("threadpool:max_queue_size");
117
int maxQueueSize = 10 * hwm;
118
119         // configuration.getInteger("threadpool:dequeue_timeout");
120
long dequeueTimeout = 2 * 60 * 1000L;
121
122         return new AutomaticWorkQueueImpl(maxQueueSize, initialThreads, hwm, lwm, dequeueTimeout);
123                
124     }
125
126 }
127
Popular Tags