Retrieving process variable in case of a Timeout Exception

Hi everyone,

I start a process instance in this way:

				RuntimeService runtimeService = ProcessEngines.getDefaultProcessEngine().getRuntimeService();
				runtimeService.startProcessInstanceByKey("MyProcess.bpmn", processParameter);

Inside the process I am collectiong informations, which I save in a process variable:

paramDelegateExecution.setVariable("names", "Peter");
...
paramDelegateExecution.setVariable("names", "Michael");

At the end of the process, I do persisting all the names in database.

Lets say, at the point of time, I have collected “Peter”. And TimedOutException occurs. I am looking for a solution, how I can still retrieve the process variable “names”, so I can still persist “Peter”.

Thank you for the ideas.

Regards, Hadi

Looks like you’re executing the workflow in a synchronous manner. In the synchronous way of execution, to complete the entire process, the flow will take time and is directly proportional to the number of tasks and each task’s execution time. Enabling asynchronous execution of workflow gives savepoints between tasks which can avoid timeout errors and also data will be persisted at each savepoint/waiting state. So querying the variables would be easier even if an exception occurs at some step in the workflow.

2 Likes

I indeed have a synchronous workflow. Each collecting happens through a Camunda-Listener with REQUIRES_NEW. I can try to change it to NONE. After that, which method from org.camunda.bpm.engine.HistoryService could help me to retrieve the process variable? In fact, I only have the processInstanceId as parameter.

@hingiswiss Does your process has any user tasks?

Another possible solution would be retrieving the ‘names’-Processvariable in the catch-Method.

runtimeService.startProcessInstanceByKey(“MyProcess.bpmn”, processParameter);

I am trying if startProcessInstanceByKey(…,…) does call-by-reference. If it does, it would be possible to just retrieve the variable

@hingiswiss you can query using historic variable instance query like below:

List<HistoricVariableInstance> historicVariableInstances = historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();
1 Like

You can convert to DTO if you’re sending back to rest API response.

//inside loop
HistoricVariableInstanceDto historicVariableInstanceDto = HistoricVariableInstanceDto
						.fromHistoricVariableInstance(historicVariableInstance);

Just discussed the possible solution using ur advice with my lead-developer. Unfortunately, we dont have the HistoricService in production-mode. Another solution would be:

Are there alternatives to start ProcessInstance which do call-by-reference?

@hingiswiss Having an ExecutionListener in the end event as a callback will do the job.

1 Like

Thank you for the advice. I tried this and it does do the job, even though I still dont understand how come the ExecutionListener is being called even though a TimeoutException occured. I would have thought, that the workflow would just stop in case of TimeoutException, so that ExecutionListener or BoundaryEvent would not get called?

@hingiswiss Do you have any waiting states in the process?

ExecutionListener configured at the process level or at the EndEvent? For example, here you can see ExecutionListener configured for class TestClass at multiple places.

  <bpmn:process id="Process_0qpz1oz" isExecutable="true">
    <bpmn:extensionElements>
      <camunda:executionListener class="TestClass" event="end" />
    </bpmn:extensionElements>
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>Flow_1x82xd6</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:task id="Activity_0o210ix">
      <bpmn:incoming>Flow_1x82xd6</bpmn:incoming>
      <bpmn:outgoing>Flow_0m73dg4</bpmn:outgoing>
    </bpmn:task>
    <bpmn:sequenceFlow id="Flow_1x82xd6" sourceRef="StartEvent_1" targetRef="Activity_0o210ix" />
    <bpmn:endEvent id="Event_0nsxsct">
      <bpmn:extensionElements>
        <camunda:executionListener class="TestClass" event="end" />
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_0m73dg4</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow_0m73dg4" sourceRef="Activity_0o210ix" targetRef="Event_0nsxsct" />
  </bpmn:process>
1 Like