|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| package org.objectledge.threads.impl; |
|
29 |
| |
|
30 |
| import org.jcontainer.dna.Logger; |
|
31 |
| import org.objectledge.context.Context; |
|
32 |
| import org.objectledge.pipeline.Valve; |
|
33 |
| import org.objectledge.threads.Task; |
|
34 |
| import org.picocontainer.Startable; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| public class Daemon |
|
43 |
| implements Runnable, Startable |
|
44 |
| { |
|
45 |
| private Thread thread; |
|
46 |
| |
|
47 |
| private Logger log; |
|
48 |
| |
|
49 |
| private Task task; |
|
50 |
| |
|
51 |
| private Context context; |
|
52 |
| |
|
53 |
| private Valve cleanup; |
|
54 |
| |
|
55 |
| private boolean shutdown = false; |
|
56 |
| |
|
57 |
| private boolean running = false; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
5060
| public Daemon(Task task, int priority,
|
|
70 |
| ThreadGroup threadGroup, Logger log, Context context, Valve cleanup) |
|
71 |
| { |
|
72 |
5060
| this.log = log;
|
|
73 |
5060
| this.context = context;
|
|
74 |
5060
| this.task = task;
|
|
75 |
5060
| this.cleanup = cleanup;
|
|
76 |
5060
| thread = new Thread(threadGroup, this, task.getName());
|
|
77 |
5060
| thread.setPriority(priority);
|
|
78 |
5060
| thread.setDaemon(true);
|
|
79 |
5060
| thread.start();
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
5056
| public void run()
|
|
86 |
| { |
|
87 |
5056
| log.info("starting "+task.getName());
|
|
88 |
5054
| try
|
|
89 |
| { |
|
90 |
5054
| running = true;
|
|
91 |
5054
| task.process(context);
|
|
92 |
| } |
|
93 |
| |
|
94 |
| catch(VirtualMachineError e) |
|
95 |
| { |
|
96 |
| throw e; |
|
97 |
| } |
|
98 |
| |
|
99 |
| catch(ThreadDeath e) |
|
100 |
| { |
|
101 |
0
| if(!shutdown)
|
|
102 |
| { |
|
103 |
0
| log.warn(task.getName()+" was forcibly stopped", e);
|
|
104 |
| } |
|
105 |
| else |
|
106 |
| { |
|
107 |
0
| log.info("finished "+task.getName());
|
|
108 |
| } |
|
109 |
0
| throw e;
|
|
110 |
| } |
|
111 |
| catch(Throwable e) |
|
112 |
| { |
|
113 |
46
| log.error("unhandled exception in "+task.getName(), e);
|
|
114 |
| } |
|
115 |
| finally |
|
116 |
| { |
|
117 |
323
| running = false;
|
|
118 |
| } |
|
119 |
| |
|
120 |
323
| if(shutdown)
|
|
121 |
| { |
|
122 |
231
| log.info("finished "+task.getName());
|
|
123 |
| } |
|
124 |
| else |
|
125 |
| { |
|
126 |
92
| log.error(task.getName()+" has quit");
|
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
323
| if(cleanup != null)
|
|
131 |
| { |
|
132 |
230
| try
|
|
133 |
| { |
|
134 |
230
| cleanup.process(context);
|
|
135 |
| } |
|
136 |
| |
|
137 |
| catch(VirtualMachineError e) |
|
138 |
| { |
|
139 |
| throw e; |
|
140 |
| } |
|
141 |
| catch(ThreadDeath e) |
|
142 |
| { |
|
143 |
| throw e; |
|
144 |
| } |
|
145 |
| |
|
146 |
| catch(Throwable e) |
|
147 |
| { |
|
148 |
46
| log.error("error in cleanup after "+task.getName(), e);
|
|
149 |
| } |
|
150 |
| } |
|
151 |
| } |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
0
| public void start()
|
|
157 |
| { |
|
158 |
| |
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
552
| public void stop()
|
|
165 |
| { |
|
166 |
552
| shutdown = true;
|
|
167 |
552
| if(running)
|
|
168 |
| { |
|
169 |
414
| log.info("asking "+task.getName()+" to terminate");
|
|
170 |
414
| task.terminate(thread);
|
|
171 |
| } |
|
172 |
| else |
|
173 |
| { |
|
174 |
138
| log.warn(task.getName()+" is not running");
|
|
175 |
| } |
|
176 |
| } |
|
177 |
| } |