Skip to content

Instantly share code, notes, and snippets.

@edmondop
Created February 3, 2019 17:47
Show Gist options
  • Save edmondop/ee60692b1c0d6fcaaf6ce1df7f1fd94c to your computer and use it in GitHub Desktop.
Save edmondop/ee60692b1c0d6fcaaf6ce1df7f1fd94c to your computer and use it in GitHub Desktop.
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.externaltask.LockedExternalTask;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class ExternalTaskBehaviour {
public static final String TOPIC_NAME = "org.edmondo1984.example";
public static void main(String[] args) throws InterruptedException {
ProcessEngine processEngine = ProcessEngineConfiguration
.createStandaloneInMemProcessEngineConfiguration().buildProcessEngine();
BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("repro")
.name("Transaction and External Tasks?")
.startEvent()
.name("Start event")
.serviceTask()
.name("Assign Approver")
.camundaType("External")
.camundaTopic(TOPIC_NAME)
.endEvent()
.name("End process")
.done();
// deploy process model
processEngine.getRepositoryService().createDeployment().addModelInstance("repro.bpmn", modelInstance).deploy();
// start consumer
AtomicBoolean terminateLock = new AtomicBoolean(false);
Thread thread = buildThread(processEngine,terminateLock);
thread.start();
// start process model
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("repro");
Thread.sleep(1000);
System.out.println("Process variable " + processEngine.getRuntimeService().getVariable(processInstance.getId(),"variable-1"));
terminateLock.set(true);
processEngine.close();
}
public static Thread buildThread(ProcessEngine processEngine, AtomicBoolean lock){
return new Thread(() -> {
try {
if(!lock.get()){
Thread.sleep(100);
List<LockedExternalTask> externalTasks = processEngine.getExternalTaskService().fetchAndLock(1, "worker-id-1").topic(TOPIC_NAME, 10000).execute();
System.out.println("Found " + externalTasks.size() + " tasks, processing");
if(externalTasks.size() >0 ){
LockedExternalTask externalTask = externalTasks.get(0);
processEngine.getRuntimeService().setVariable(externalTask.getExecutionId(),"variable-1","value-1");
}
}
else{
System.out.println("End lock set, terminating");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
@edmondop
Copy link
Author

edmondop commented Feb 3, 2019

What is surprising is that line 42 will log a process variable which has been set asynchronously without any transaction commit (the process engine has not reached the next transaction boundary because externalTaskServce.complete() has not been called by the async thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment