Repository navigation
Expand file tree
/
Copy pathAppJobManager.java
More file actions
270 lines (222 loc) · 8.76 KB
/
Copy pathAppJobManager.java
File metadata and controls
270 lines (222 loc) · 8.76 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package act.job;
import act.ActComponent;
import act.Destroyable;
import act.app.App;
import act.app.AppServiceBase;
import act.app.AppThreadFactory;
import act.app.event.AppEventId;
import act.event.AppEventListenerBase;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import org.osgl.$;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import org.rythmengine.utils.Time;
import java.util.EventObject;
import java.util.concurrent.*;
import static act.app.App.logger;
@ActComponent
public class AppJobManager extends AppServiceBase<AppJobManager> {
private ScheduledThreadPoolExecutor executor;
private ConcurrentMap<String, _Job> jobs = new ConcurrentHashMap<String, _Job>();
static String appEventJobId(AppEventId eventId) {
return S.builder("__act_app_").append(eventId.toString().toLowerCase()).toString();
}
public AppJobManager(App app) {
super(app);
initExecutor(app);
for (AppEventId appEventId : AppEventId.values()) {
createAppEventListener(appEventId);
}
}
@Override
protected void releaseResources() {
for (_Job job : jobs.values()) {
job.destroy();
}
jobs.clear();
executor.shutdown();
executor.getQueue().clear();
}
public <T> Future<T> now(Callable<T> callable) {
return executor().submit(callable);
}
public void now(Runnable runnable) {
executor().submit(wrap(runnable));
}
public <T> Future<T> delay(Callable<T> callable, long delay, TimeUnit timeUnit) {
return executor().schedule(callable, delay, timeUnit);
}
public void delay(Runnable runnable, long delay, TimeUnit timeUnit) {
executor().schedule(wrap(runnable), delay, timeUnit);
}
public <T> Future<T> delay(Callable<T> callable, String delay) {
int seconds = parseTime(delay);
return executor().schedule(callable, seconds, TimeUnit.SECONDS);
}
public void delay(Runnable runnable, String delay) {
int seconds = parseTime(delay);
executor().schedule(wrap(runnable), seconds, TimeUnit.SECONDS);
}
public void every(String id, Runnable runnable, String interval) {
JobTrigger.every(interval).schedule(this, _Job.multipleTimes(id, runnable, this));
}
public void every(Runnable runnable, String interval) {
JobTrigger.every(interval).schedule(this, _Job.multipleTimes(runnable, this));
}
public void every(Runnable runnable, long interval, TimeUnit timeUnit) {
JobTrigger.every(interval, timeUnit).schedule(this, _Job.multipleTimes(runnable, this));
}
public void every(String id, Runnable runnable, long interval, TimeUnit timeUnit) {
JobTrigger.every(interval, timeUnit).schedule(this, _Job.multipleTimes(id, runnable, this));
}
public void fixedDelay(Runnable runnable, String interval) {
JobTrigger.every(interval).schedule(this, _Job.multipleTimes(runnable, this));
}
public void fixedDelay(String id, Runnable runnable, String interval) {
JobTrigger.every(interval).schedule(this, _Job.multipleTimes(id, runnable, this));
}
public void fixedDelay(Runnable runnable, long interval, TimeUnit timeUnit) {
JobTrigger.fixedDelay(interval, timeUnit).schedule(this, _Job.multipleTimes(runnable, this));
}
public void fixedDelay(String id, Runnable runnable, long interval, TimeUnit timeUnit) {
JobTrigger.fixedDelay(interval, timeUnit).schedule(this, _Job.multipleTimes(id, runnable, this));
}
private int parseTime(String timeDuration) {
if (timeDuration.startsWith("${") && timeDuration.endsWith("}")) {
timeDuration = (String) app().config().get(timeDuration.substring(2, timeDuration.length() - 1));
}
return Time.parseDuration(timeDuration);
}
public void on(DateTime instant, Runnable runnable) {
DateTime now = DateTime.now();
E.illegalArgumentIf(instant.isBefore(now));
Seconds seconds = Seconds.secondsBetween(now, instant);
executor().schedule(wrap(runnable), seconds.getSeconds(), TimeUnit.SECONDS);
}
public <T> Future<T> on(DateTime instant, Callable<T> callable) {
DateTime now = DateTime.now();
E.illegalArgumentIf(instant.isBefore(now));
Seconds seconds = Seconds.secondsBetween(now, instant);
return executor().schedule(callable, seconds.getSeconds(), TimeUnit.SECONDS);
}
public void on(AppEventId appEvent, final Runnable runnable) {
on(appEvent, runnable, false);
}
public void on(AppEventId appEvent, final Runnable runnable, boolean runImmediatelyIfEventDispatched) {
_Job job = jobById(appEventJobId(appEvent));
if (null == job) {
processDelayedJob(wrap(runnable), runImmediatelyIfEventDispatched);
} else {
job.addPrecedenceJob(_Job.once(runnable, this));
}
}
public void post(AppEventId appEvent, final Runnable runnable) {
post(appEvent, runnable, false);
}
public void post(AppEventId appEvent, final Runnable runnable, boolean runImmediatelyIfEventDispatched) {
_Job job = jobById(appEventJobId(appEvent));
if (null == job) {
processDelayedJob(wrap(runnable), runImmediatelyIfEventDispatched);
} else {
job.addFollowingJob(_Job.once(runnable, this));
}
}
public void on(AppEventId appEvent, String jobId, final Runnable runnable) {
on(appEvent, jobId, runnable, false);
}
public void on(AppEventId appEvent, String jobId, final Runnable runnable, boolean runImmediatelyIfEventDispatched) {
_Job job = jobById(appEventJobId(appEvent));
if (null == job) {
processDelayedJob(wrap(runnable), runImmediatelyIfEventDispatched);
} else {
job.addPrecedenceJob(_Job.once(jobId, runnable, this));
}
}
public void post(AppEventId appEvent, String jobId, final Runnable runnable) {
post(appEvent, jobId, runnable, false);
}
public void post(AppEventId appEvent, String jobId, final Runnable runnable, boolean runImmediatelyIfEventDispatched) {
_Job job = jobById(appEventJobId(appEvent));
if (null == job) {
processDelayedJob(wrap(runnable), runImmediatelyIfEventDispatched);
} else {
job.addFollowingJob(_Job.once(jobId, runnable, this));
}
}
private void processDelayedJob(final Runnable runnable, boolean runImmediatelyIfEventDispatched) {
if (runImmediatelyIfEventDispatched) {
try {
runnable.run();
} catch (Exception e) {
logger.error(e, "Error running job");
}
} else {
now(runnable);
}
}
public void beforeAppStart(final Runnable runnable) {
on(AppEventId.START, runnable);
}
public void afterAppStart(final Runnable runnable) {
post(AppEventId.START, runnable);
}
public void beforeAppStop(final Runnable runnable) {
on(AppEventId.STOP, runnable);
}
C.List<_Job> jobs() {
return C.list(jobs.values());
}
_Job jobById(String id) {
_Job job = jobs.get(id);
if (null == job) {
logger.warn("cannot find job by id: %s", id);
}
return job;
}
void addJob(_Job job) {
jobs.put(job.id(), job);
}
void removeJob(_Job job) {
jobs.remove(job.id());
}
ScheduledThreadPoolExecutor executor() {
return executor;
}
private void initExecutor(App app) {
int poolSize = app.config().jobPoolSize();
executor = new ScheduledThreadPoolExecutor(poolSize, new AppThreadFactory("jobs"), new ThreadPoolExecutor.AbortPolicy());
executor.setRemoveOnCancelPolicy(true);
}
private void createAppEventListener(AppEventId appEventId) {
String jobId = appEventJobId(appEventId);
_Job job = new _Job(jobId, this);
addJob(job);
app().eventBus().bind(appEventId, new _AppEventListener(jobId, job));
}
private static class _AppEventListener extends AppEventListenerBase {
private Runnable worker;
_AppEventListener(String id, Runnable worker) {
super(id);
this.worker = $.NPE(worker);
}
@Override
public void on(EventObject event) throws Exception {
worker.run();
}
@Override
protected void releaseResources() {
if (null != worker && worker instanceof Destroyable) {
((Destroyable) worker).destroy();
}
}
}
private Runnable wrap(Runnable runnable) {
if (runnable instanceof _Job) {
return runnable;
} else {
return _Job.once(runnable, this);
}
}
}