Skip to content

Commit c20ec0a

Browse files
committed
Small refactoring for extensibility
1 parent 118b902 commit c20ec0a

File tree

1 file changed

+16
-4
lines changed
  • snippets/engine-plugin-external-task-listener/src/main/java/org/camunda/bpm/consulting/snippet/engine_plugin_external_task_listener

1 file changed

+16
-4
lines changed

snippets/engine-plugin-external-task-listener/src/main/java/org/camunda/bpm/consulting/snippet/engine_plugin_external_task_listener/LockingExternalTaskListener.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,36 @@
1212

1313
public class LockingExternalTaskListener implements ExternalTaskListener {
1414

15+
public static final String WORKER_ID = "PushedToWorkerInsideSameJVM";
16+
1517
@Override
1618
public void notify(ExternalTask externalTask) {
1719
String externalTaskId = externalTask.getId();
1820
System.out.println("New External Task: " + externalTaskId);
1921

2022
// lock the task in the same TX that creates it
2123
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);
2525

2626
// get variables
2727
VariableMap variables = entity.getExecution().getVariables();
2828
VariableMap localVariables = entity.getExecution().getVariablesLocal();
2929

3030
ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
3131

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) {
3242
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
3445
try {
3546
Thread.sleep(200L); // TODO use ScheduledExecutorService and/or Queue
3647
} catch (InterruptedException e) {
@@ -41,6 +52,7 @@ public void notify(ExternalTask externalTask) {
4152
String variableValue = (String) variables.get("foo");
4253
variables.put("foo", variableValue + " changed by worker");
4354

55+
// complete task in new TX
4456
System.out.println("Completing External Task: " + externalTaskId);
4557
processEngineConfiguration.getCommandExecutorTxRequiresNew()
4658
.execute(

0 commit comments

Comments
 (0)