Question

is it possible to get all process or task variables using TaskService:

processEngine.getTaskService.createTaskQuery().list();

I know there is an opportunity to get variables via processEngine.getTaskService().getVariable()

or

processEngine.getRuntimeService().getVariable()

but every of operation above goes to database. If I have list of 100 tasks I'll make 100 queries to DB. I don't want to use this approach. Is there any other way to get task or process related variables?

Was it helpful?

Solution

Unfortunately, there is no way to do that via the "official" query API! However, what you could do is writing a custom MyBatis query as described here:

https://app.camunda.com/confluence/display/foxUserGuide/Performance+Tuning+with+custom+Queries (Note: Everything described in the article also works for bare Activiti, you do not need the fox engine for that!)

This way you could write a query which selects tasks along with the variables in one step. At my company we used this solution as we had the exact same performance problem.

A drawback of this solution is that custom queries need to be maintained. For instance, if you upgrade your Activiti version, you will need to ensure that your custom query still fits the database schema (e.g., via integration tests).

OTHER TIPS

If it is not possible to use the API as elsvene says, you can query yourself the database. Activiti has several tables on the database.

You have act_ru_variable, were the currently running processes store the variables. For the already finished processess you have act_hi_procvariable. Probably you can find a detailed explanation on what is on each table in activiti userguide.

So you just need to make queries like

SELECT *
FROM act_ru_variable
WHERE *Something*

The following Test, sends a value object (Person) to a process which just adds a few tracking infos for demonstration.

I had the same problem, to get the value object after execution the service to do some validation in my test.

The following piece of code shows the execution and the gathering of the task varaible after the execution was finished.

@Test
public void justATest() {
    Map<String, Object> inVariables = new HashMap<String, Object>();

    Person person = new Person();
    person.setName("Jens");
    inVariables.put("person", person);

    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("event01", inVariables);

    String processDefinitionId = processInstance.getProcessDefinitionId();
    String id = processInstance.getId();
    System.out.println("id " + id + " " + processDefinitionId);


    List<HistoricVariableInstance> outVariables =
            historyService.createHistoricVariableInstanceQuery().processInstanceId(id).list();

    for (HistoricVariableInstance historicVariableInstance : outVariables) {
        String variableName = historicVariableInstance.getVariableName();
        System.out.println(variableName);
        Person person1 = (Person) historicVariableInstance.getValue();
        System.out.println(person1.toString());
    }

}  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top