Code example for Stopwatch

0
    public void waitForProcessEnd(String processInstanceId) throws Exception {
        waitForProcessEnd(processInstanceId, 60000 /* milliseconds */);
    } 
 
    public void waitForProcessEnd(final String processInstanceId, int timeoutInMilliseconds) throws Exception {
        Stopwatch stopwatch = new Stopwatch().start();
        while (isProcessNotEnded(processInstanceId)) {
            if (stopwatch.elapsedMillis() > timeoutInMilliseconds) {
                throw new TimeoutException(String.format("Process %s not ended in %d milliseconds.",
                    processInstanceId, timeoutInMilliseconds));
            } 
            LOG.info(String.format("Process instance %s not ended. Waiting 1s.", processInstanceId));
            TimeUnit.SECONDS.sleep(1);
        } 
        LOG.info(String.format("Process instance %s ended as expected in less than %d milliseconds",
            processInstanceId, timeoutInMilliseconds));
    } 
 
    private boolean isProcessNotEnded(final String processInstanceId) throws InterruptedException {
        ProcessInstance localInstance = getProcessInstanceById(processInstanceId);
Stop searching for code, let great code find you!  Add Codota to your java IDE