forked from kiegroup/jbpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerManager.java
521 lines (417 loc) · 19.1 KB
/
TimerManager.java
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jbpm.process.instance.timer;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.drools.core.common.InternalKnowledgeRuntime;
import org.drools.core.common.InternalWorkingMemory;
import org.drools.core.marshalling.impl.MarshallerReaderContext;
import org.drools.core.marshalling.impl.MarshallerWriteContext;
import org.drools.core.marshalling.impl.ProtobufInputMarshaller;
import org.drools.core.marshalling.impl.ProtobufMessages;
import org.drools.core.marshalling.impl.ProtobufMessages.Timers.Timer;
import org.drools.core.marshalling.impl.ProtobufOutputMarshaller;
import org.drools.core.marshalling.impl.TimersInputMarshaller;
import org.drools.core.marshalling.impl.TimersOutputMarshaller;
import org.drools.core.time.Job;
import org.drools.core.time.JobContext;
import org.drools.core.time.JobHandle;
import org.drools.core.time.TimerService;
import org.drools.core.time.Trigger;
import org.drools.core.time.impl.CronTrigger;
import org.drools.core.time.impl.IntervalTrigger;
import org.jbpm.marshalling.impl.JBPMMessages;
import org.jbpm.marshalling.impl.ProtobufProcessMarshaller;
import org.jbpm.process.core.timer.impl.RegisteredTimerServiceDelegate;
import org.jbpm.process.instance.InternalProcessRuntime;
import org.jbpm.process.instance.ProcessInstance;
import org.jbpm.process.instance.ProcessRuntimeImpl;
import org.kie.api.runtime.KieSession;
import org.kie.api.time.SessionClock;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*/
public class TimerManager {
private static final Logger logger = LoggerFactory.getLogger(TimerManager.class);
private long timerId = 0;
private InternalKnowledgeRuntime kruntime;
private TimerService timerService;
private Map<Long, TimerInstance> timers = new ConcurrentHashMap<Long, TimerInstance>();
public static final Job processJob = new ProcessJob();
public static final Job startProcessJob = new StartProcessJob();
public TimerManager(InternalKnowledgeRuntime kruntime, TimerService timerService) {
this.kruntime = kruntime;
this.timerService = timerService;
}
public void registerTimer(final TimerInstance timer, ProcessInstance processInstance) {
try {
kruntime.startOperation();
timer.setId(++timerId);
timer.setProcessInstanceId(processInstance.getId());
timer.setSessionId(((KieSession) kruntime).getIdentifier());
timer.setActivated(new Date());
Trigger trigger = null;
if (timer.getCronExpression() != null) {
Date startTime = new Date(timerService.getCurrentTime() + 1000);
trigger = new CronTrigger(timerService.getCurrentTime(), startTime, null, -1, timer.getCronExpression(), null, null);
// cron timers are by nature repeatable
timer.setPeriod(1);
} else {
trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, timer.getRepeatLimit(),
timer.getDelay(), timer.getPeriod(), null, null);
}
ProcessJobContext ctx = new ProcessJobContext(timer, trigger, processInstance.getId(), this.kruntime);
JobHandle jobHandle = this.timerService.scheduleJob(processJob, ctx, trigger);
timer.setJobHandle(jobHandle);
timers.put(timer.getId(), timer);
} finally {
kruntime.endOperation();
}
}
public void registerTimer(final TimerInstance timer, String processId, Map<String, Object> params) {
try {
kruntime.startOperation();
timer.setId(++timerId);
timer.setProcessInstanceId(-1l);
timer.setSessionId(((StatefulKnowledgeSession) kruntime).getIdentifier());
timer.setActivated(new Date());
Trigger trigger = null;
if (timer.getCronExpression() != null) {
Date startTime = new Date(timerService.getCurrentTime() + 1000);
trigger = new CronTrigger(timerService.getCurrentTime(), startTime, null, -1, timer.getCronExpression(), null, null);
// cron timers are by nature repeatable
timer.setPeriod(1);
} else {
trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, timer.getRepeatLimit(), timer.getDelay(),
timer.getPeriod(), null, null);
}
StartProcessJobContext ctx = new StartProcessJobContext(timer, trigger, processId, params, this.kruntime);
JobHandle jobHandle = this.timerService.scheduleJob(startProcessJob, ctx, trigger);
timer.setJobHandle(jobHandle);
timers.put(timer.getId(), timer);
} finally {
kruntime.endOperation();
}
}
public void internalAddTimer(final TimerInstance timer) {
long delay;
Date lastTriggered = timer.getLastTriggered();
if (lastTriggered == null) {
Date activated = timer.getActivated();
Date now = new Date();
long timespan = now.getTime() - activated.getTime();
delay = timer.getDelay() - timespan;
if (delay < 0) {
delay = 0;
}
} else {
Date now = new Date();
long timespan = now.getTime() - lastTriggered.getTime();
delay = timespan - timer.getPeriod();
if (delay < 0) {
delay = 0;
}
}
Trigger trigger = new IntervalTrigger(timerService.getCurrentTime(), null, null, -1, delay, timer.getPeriod(), null, null);
ProcessJobContext ctx = new ProcessJobContext(timer, trigger, timer.getProcessInstanceId(), this.kruntime);
JobHandle jobHandle = this.timerService.scheduleJob(processJob, ctx, trigger);
timer.setJobHandle(jobHandle);
timers.put(timer.getId(), timer);
}
public void cancelTimer(long timerId) {
try {
kruntime.startOperation();
TimerInstance timer = timers.remove(timerId);
if (timer != null) {
timerService.removeJob(timer.getJobHandle());
}
} finally {
kruntime.endOperation();
}
}
public void dispose() {
// for ( TimerInstance timer : timers.values() ) {
// timerService.removeJob( timer.getJobHandle() );
// }
if (timerService instanceof RegisteredTimerServiceDelegate) {
timers.clear();
return;
}
for (Iterator<TimerInstance> it = timers.values().iterator(); it.hasNext();) {
TimerInstance timer = it.next();
timerService.removeJob(timer.getJobHandle());
it.remove();
}
timerService.shutdown();
}
public TimerService getTimerService() {
return this.timerService;
}
public Collection<TimerInstance> getTimers() {
return timers.values();
}
public Map<Long, TimerInstance> getTimerMap() {
return this.timers;
}
public long internalGetTimerId() {
return timerId;
}
public void internalSetTimerId(long timerId) {
this.timerId = timerId;
}
public void setTimerService(TimerService timerService) {
this.timerService = timerService;
}
public static class ProcessTimerOutputMarshaller implements TimersOutputMarshaller {
public Timer serialize(JobContext jobCtx, MarshallerWriteContext outputCtx) {
// do not store StartProcess timers as they are registered whenever session starts
if (jobCtx instanceof StartProcessJobContext) {
return null;
}
ProcessJobContext pctx = (ProcessJobContext) jobCtx;
return ProtobufMessages.Timers.Timer
.newBuilder()
.setType(ProtobufMessages.Timers.TimerType.PROCESS)
.setExtension(
JBPMMessages.procTimer,
JBPMMessages.ProcessTimer.newBuilder()
.setTimer(ProtobufProcessMarshaller.writeTimer(outputCtx, pctx.getTimer()))
.setTrigger(ProtobufOutputMarshaller.writeTrigger(pctx.getTrigger(), outputCtx)).build())
.build();
}
}
public static class ProcessTimerInputMarshaller implements TimersInputMarshaller {
public void deserialize(MarshallerReaderContext inCtx, Timer timer) throws ClassNotFoundException {
JBPMMessages.ProcessTimer ptimer = timer.getExtension(JBPMMessages.procTimer);
TimerService ts = inCtx.wm.getTimerService();
long processInstanceId = ptimer.getTimer().getProcessInstanceId();
Trigger trigger = ProtobufInputMarshaller.readTrigger(inCtx, ptimer.getTrigger());
TimerInstance timerInstance = ProtobufProcessMarshaller.readTimer(inCtx, ptimer.getTimer());
TimerManager tm = ((InternalProcessRuntime) inCtx.wm.getProcessRuntime()).getTimerManager();
// check if the timer instance is not already registered to avoid duplicated timers
if (!tm.getTimerMap().containsKey(timerInstance.getId())) {
ProcessJobContext pctx = new ProcessJobContext(timerInstance, trigger, processInstanceId,
inCtx.wm.getKnowledgeRuntime(), false);
Date date = trigger.hasNextFireTime();
if (date != null) {
long then = date.getTime();
long now = pctx.getKnowledgeRuntime().getSessionClock().getCurrentTime();
// overdue timer
if (then < now) {
trigger = new OverdueTrigger(trigger);
}
}
trigger.initialize(pctx.getKnowledgeRuntime());
JobHandle jobHandle = ts.scheduleJob(processJob, pctx, trigger);
timerInstance.setJobHandle(jobHandle);
pctx.setJobHandle(jobHandle);
tm.getTimerMap().put(timerInstance.getId(), timerInstance);
}
}
}
public static class ProcessJob implements Job, Serializable {
private static final long serialVersionUID = 6004839244692770390L;
public void execute(JobContext c) {
ProcessJobContext ctx = (ProcessJobContext) c;
Long processInstanceId = ctx.getProcessInstanceId();
InternalKnowledgeRuntime kruntime = ctx.getKnowledgeRuntime();
try {
kruntime.startOperation();
if (processInstanceId == null) {
throw new IllegalArgumentException("Could not find process instance for timer ");
}
ctx.getTimer().setLastTriggered(
new Date(ctx.getKnowledgeRuntime().<SessionClock> getSessionClock().getCurrentTime()));
// if there is no more trigger reset period on timer so its node instance can be removed
if (ctx.getTrigger().hasNextFireTime() == null) {
ctx.getTimer().setPeriod(0);
}
((InternalProcessRuntime) kruntime.getProcessRuntime()).getSignalManager().signalEvent(processInstanceId,
"timerTriggered", ctx.getTimer());
TimerManager tm = ((InternalProcessRuntime) ctx.getKnowledgeRuntime().getProcessRuntime()).getTimerManager();
if (ctx.getTimer().getPeriod() == 0) {
tm.getTimerMap().remove(ctx.getTimer().getId());
tm.getTimerService().removeJob(ctx.getJobHandle());
}
} catch (Throwable e) {
logger.error("Error when executing timer job", e);
throw new RuntimeException(e);
} finally {
kruntime.endOperation();
}
}
}
public static class StartProcessJob implements Job, Serializable {
private static final long serialVersionUID = 1039445333595469160L;
public void execute(JobContext c) {
StartProcessJobContext ctx = (StartProcessJobContext) c;
InternalKnowledgeRuntime kruntime = ctx.getKnowledgeRuntime();
InternalProcessRuntime processRuntime = ((InternalProcessRuntime) ctx.getKnowledgeRuntime().getProcessRuntime());
TimerManager tm = processRuntime.getTimerManager();
if (!((ProcessRuntimeImpl) processRuntime).isActive()) {
logger.debug("Timer for starting process {} is ignored as the deployment is in deactivated state", ctx.getProcessId());
tm.getTimerMap().remove(ctx.getTimer().getId());
tm.getTimerService().removeJob(ctx.getJobHandle());
return;
}
try {
kruntime.startOperation();
ctx.getTimer().setLastTriggered(
new Date(ctx.getKnowledgeRuntime().<SessionClock> getSessionClock().getCurrentTime()));
// if there is no more trigger reset period on timer so its node instance can be removed
if (ctx.getTrigger().hasNextFireTime() == null) {
ctx.getTimer().setPeriod(0);
}
((ProcessRuntimeImpl)kruntime.getProcessRuntime()).startProcess(ctx.getProcessId(), ctx.getParamaeters(), "timer");
if (ctx.getTimer().getPeriod() == 0) {
tm.getTimerMap().remove(ctx.getTimer().getId());
tm.getTimerService().removeJob(ctx.getJobHandle());
}
} catch (Throwable e) {
logger.error("Error when executing start process " + ctx.getProcessId() + " timer job", e);
} finally {
kruntime.endOperation();
}
}
}
public static class ProcessJobContext implements JobContext {
private static final long serialVersionUID = 476843895176221627L;
private Long processInstanceId;
private transient InternalKnowledgeRuntime kruntime;
private TimerInstance timer;
private Trigger trigger;
private JobHandle jobHandle;
private Long sessionId;
private boolean newTimer;
public ProcessJobContext(final TimerInstance timer, final Trigger trigger, final Long processInstanceId,
final InternalKnowledgeRuntime kruntime) {
this.timer = timer;
this.trigger = trigger;
this.processInstanceId = processInstanceId;
this.kruntime = kruntime;
this.sessionId = timer.getSessionId();
this.newTimer = true;
}
public ProcessJobContext(final TimerInstance timer, final Trigger trigger, final Long processInstanceId,
final InternalKnowledgeRuntime kruntime, boolean newTimer) {
this.timer = timer;
this.trigger = trigger;
this.processInstanceId = processInstanceId;
this.kruntime = kruntime;
this.sessionId = timer.getSessionId();
this.newTimer = newTimer;
}
public Long getProcessInstanceId() {
return processInstanceId;
}
public InternalKnowledgeRuntime getKnowledgeRuntime() {
return kruntime;
}
public Trigger getTrigger() {
return trigger;
}
public JobHandle getJobHandle() {
return this.jobHandle;
}
public void setJobHandle(JobHandle jobHandle) {
this.jobHandle = jobHandle;
}
public TimerInstance getTimer() {
return timer;
}
public Long getSessionId() {
return sessionId;
}
public void setKnowledgeRuntime(InternalKnowledgeRuntime kruntime) {
this.kruntime = kruntime;
}
@Override
public InternalWorkingMemory getWorkingMemory() {
return kruntime instanceof InternalWorkingMemory ? (InternalWorkingMemory)kruntime : null;
}
public boolean isNewTimer() {
return newTimer;
}
}
public static class StartProcessJobContext extends ProcessJobContext {
private static final long serialVersionUID = -5219141659893424294L;
private String processId;
private Map<String, Object> paramaeters;
public StartProcessJobContext(TimerInstance timer, Trigger trigger, String processId, Map<String, Object> params,
InternalKnowledgeRuntime kruntime) {
super(timer, trigger, null, kruntime);
this.processId = processId;
this.paramaeters = params;
}
public String getProcessId() {
return processId;
}
public void setProcessId(String processId) {
this.processId = processId;
}
public Map<String, Object> getParamaeters() {
return paramaeters;
}
public void setParamaeters(Map<String, Object> paramaeters) {
this.paramaeters = paramaeters;
}
@Override
public boolean isNewTimer() {
return false;
}
}
/**
* Overdue aware trigger that introduces fixed delay to allow completion of session initialization
*
*/
public static class OverdueTrigger implements Trigger {
private static final long serialVersionUID = -2368476147776308013L;
public static final long OVERDUE_DELAY = Long.parseLong(System.getProperty("jbpm.overdue.timer.delay", "2000"));
private Trigger orig;
private transient InternalKnowledgeRuntime kruntime;
public OverdueTrigger(Trigger orig) {
this.orig = orig;
}
@Override
public void initialize(InternalKnowledgeRuntime kruntime) {
this.kruntime = kruntime;
}
public Date hasNextFireTime() {
Date date = orig.hasNextFireTime();
if (date == null) {
return null;
}
long then = date.getTime();
long now = kruntime.getSessionClock().getCurrentTime();
// overdue timer
if (then < now) {
return new Date((now + OVERDUE_DELAY));
} else {
return orig.hasNextFireTime();
}
}
public Date nextFireTime() {
return orig.nextFireTime();
}
}
}