Retry logic with CompletableFuture

后端 未结 7 2157
别跟我提以往
别跟我提以往 2021-01-31 18:26

I need to submit a task in an async framework I\'m working on, but I need to catch for exceptions, and retry the same task multiple times before \"aborting\".

The code I

7条回答
  •  感情败类
    2021-01-31 19:12

    I recently solved a similar problem using the guava-retrying library.

    Callable callable = new Callable() {
        public Result call() throws Exception {
            return executeMycustomActionHere();
        }
    };
    
    Retryer retryer = RetryerBuilder.newBuilder()
            .retryIfResult(Predicates.isNull())
            .retryIfExceptionOfType(IOException.class)
            .retryIfRuntimeException()
            .withStopStrategy(StopStrategies.stopAfterAttempt(MAX_RETRIES))
            .build();
    
    CompletableFuture.supplyAsync( () -> {
        try {
            retryer.call(callable);
        } catch (RetryException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
           e.printStackTrace();
        }
    });
    

提交回复
热议问题