Retry Task for Error Handling

Hello, everybody.

I am struggling to find a solution to my problem. I have a service task, that might fail, however, I would like to retry a few times in intervals to see if it gets to work. Nevertheless, when my retry times is achieved, I would like to proceed to a different place and due it manually.

I have tried the asynchronous continuation, which I can specify the retry time cycle. The retry action works, however I don’t have a way to specify what it should do when it is finished. I would like to throw a specif error, in which I could get by using an Error Boundary Event and send it to other direction. Following is part of the bpmn, in the way that I would like to have.

I know that I could do all this using many service tasks, and timers, however, I would like to have a better solution to this.

If anyone could share with me possible solutions to get the moment of the last retry, I would be eternally grateful.!

1 Like

You can use the below piece of code.

	try {
           //do something
        } catch (Exception ex) {
            ifLastRetryThrowBpmnError("myErrorCode", "MyDesc");
    }
	
	private boolean isLastRetry() {
        if (Context.getJobExecutorContext() != null && Context.getJobExecutorContext().getCurrentJob() != null) {
            int noOfRetries = Context.getJobExecutorContext().getCurrentJob().getRetries();
            LOGGER.debug("{} has {} retries remaining", Context.getJobExecutorContext().getCurrentJob().getActivityId(),
                    noOfRetries - 1);
            return noOfRetries <= 1;
        }
        return false;
    }

    private void throwBpmnError(String errorCode, String message) {
        throw new BpmnError(StringUtils.isEmpty(errorCode) ? DEFAULT_ERROR : errorCode,
                StringUtils.isEmpty(message) ? DEFAULT_ERROR : message);
    }
	
	private void ifLastRetryThrowBpmnError(String errorCode, String message) {
        if (isLastRetry()) {
            throwBpmnError(errorCode, message);
        }
    }
'''
3 Likes

It did the trick, thank you!