12
12
13
13
public class LockingExternalTaskListener implements ExternalTaskListener {
14
14
15
+ public static final String WORKER_ID = "PushedToWorkerInsideSameJVM" ;
16
+
15
17
@ Override
16
18
public void notify (ExternalTask externalTask ) {
17
19
String externalTaskId = externalTask .getId ();
18
20
System .out .println ("New External Task: " + externalTaskId );
19
21
20
22
// lock the task in the same TX that creates it
21
23
ExternalTaskEntity entity = (ExternalTaskEntity ) externalTask ;
22
- String workerId = "PushedToWorkerInsideSameJVM" ;
23
- entity .setWorkerId (workerId );
24
- entity .setLockExpirationTime (new DateTime ().plusMinutes (5 ).toDate ());
24
+ lockTask (entity , WORKER_ID );
25
25
26
26
// get variables
27
27
VariableMap variables = entity .getExecution ().getVariables ();
28
28
VariableMap localVariables = entity .getExecution ().getVariablesLocal ();
29
29
30
30
ProcessEngineConfigurationImpl processEngineConfiguration = Context .getProcessEngineConfiguration ();
31
31
32
+ workOnTask (externalTaskId , WORKER_ID , variables , localVariables , processEngineConfiguration );
33
+ }
34
+
35
+ public void lockTask (ExternalTaskEntity entity , String workerId ) {
36
+ entity .setWorkerId (workerId );
37
+ entity .setLockExpirationTime (new DateTime ().plusMinutes (5 ).toDate ());
38
+ }
39
+
40
+ public void workOnTask (String externalTaskId , String workerId , VariableMap variables , VariableMap localVariables ,
41
+ ProcessEngineConfigurationImpl processEngineConfiguration ) {
32
42
CompletableFuture .runAsync (() -> {
33
- // give the database enough time to commit the TX that creates the external task
43
+ // give the database enough time to commit the TX that creates the external task OR NOT?
44
+ // work on the task outside a TX
34
45
try {
35
46
Thread .sleep (200L ); // TODO use ScheduledExecutorService and/or Queue
36
47
} catch (InterruptedException e ) {
@@ -41,6 +52,7 @@ public void notify(ExternalTask externalTask) {
41
52
String variableValue = (String ) variables .get ("foo" );
42
53
variables .put ("foo" , variableValue + " changed by worker" );
43
54
55
+ // complete task in new TX
44
56
System .out .println ("Completing External Task: " + externalTaskId );
45
57
processEngineConfiguration .getCommandExecutorTxRequiresNew ()
46
58
.execute (
0 commit comments