microsoft / durabletask-java

Java SDK for Durable Functions and the Durable Task Framework
MIT License
13 stars 7 forks source link

Custom retry handlers #29

Closed cgillum closed 2 years ago

cgillum commented 2 years ago

This PR completes the retry policy feature for Java by allowing developers to implement custom retry policies using code. It's intended for use-cases where declarative retry policies aren't flexible enough.

Example syntax

TaskOptions optionsWithRetryHandler = TaskOptions.FromRetryHandler(retryCtx -> {
    // Retries MyException or any sub-class of MyException
    if (retryCtx.getLastFailure().isCausedBy(MyException.class)) {
        return true;
    } else {
        // For any other exception type, retry up to 4 times
        return retryCtx.getLastAttemptNumber() < 5;
    }    
});

TaskOrchestration  = ctx -> {
    ctx.callActivity("MyFlakeyActivity1", null, optionsWithRetryHandler).get();
    ctx.callActivity("MyFlakeyActivity2", null, optionsWithRetryHandler).get();
};

Code changes:

Resolves https://github.com/microsoft/durabletask-java/issues/18