Search code examples
javacamunda

Camunda Executionlistener implementation with timestamp


hey guys im very new to camunda, i want to measure the execution time for my process from start to end but i can't find a proper coding example for the listener. All i could come up with is

public void notify (DelegateExecution execution) throws Exception {

    // What happens when Start Event is executed
    long startTime = System.currentTimeMillis();

    // What happens when token has reached End Event
    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    System.out.println(elapsedTime);
    }

i set the class in the modeler for the start and end event which i hope is right.

Thanks in advance :)


Solution

  • The engine measure for you. But you can't print the duration of a process instance with a listener into the process, because the process instance is still running. You can check the duration in the database table act_hi_procinst.

    Also you can use the HistoryService from the engine:

    public class ProcessInstanceHistory {
    
    @Inject
    HistoryService historyService;
    
    public List<HistoricProcessInstance> historicProcessInstances() {
        return historyService.createHistoricProcessInstanceQuery().list();
    }
    
    public void printDurations() {
        for (HistoricProcessInstance historicProcessInstance : historicProcessInstances()) {
            System.out.println("elapsedTime :" + historicProcessInstance.getDurationInMillis());
        }
    }
    }