Search code examples
javasqlcamunda

Accessing current process instance variable information from Camunda BPMN engine


I want all the information on the running process instance stored in H2 database under act_proc_in_ table.(like starttime, endtime, author..)

I am in the ExecutionListener method (in class implementing JavaDelegate interface) from where I need to forward the information further.

I know about RuntimeService interface with createExecutionQuery() method but in all the examples I have seen it seems to be mapped to some kind of entity class. I do not understand it. Sorry, but I am new to Camunda BPM engine.

    public class ProcessRequestDelegate implements JavaDelegate { 
    private final static Logger LOGGER = Logger.getLogger("LOAN-REQUESTS"); 
    public void execute(DelegateExecution execution) throws Exception { LOGGER.info("Processing request by '"+execution.getVariable("customerId")+"'...");
    System.out.println(execution.getVariable("amount")); 
    int Amount= ((Double) execution.getVariable("amount")).intValue(); System.out.println("Amountis"+Amount);

    ProcessEngine processEngine = BpmPlatform.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService(); 

    ResulstSet rs= runtimeService.createExecutionQuery("What to write here?"); 
 while (rs.next()) {
         String author=rs.getString("AUTHOR");
            Date start = rs.getDate("START_TIME");
            int sales = rs.getInt("SALES");

} }
       

Solution

  • Starting with Camunda BPM 7.2 you are able to use the method execution.getProcessEngineServices() to access the engines services in a java delegate class. Use the HistoryService or RuntimeService to create a (Historic-)ProcessInstanceQuery like

    HistoryService historyService = execution.getProcessEngineServices().getHistoryService(); HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();

    Then you can access the info on the HistoricProcessInstance.

    Be aware that you are querying the database through these services. Data changed in the current transaction is not available through the services until the transaction is committed.