Giter VIP home page Giter VIP logo

java-retrying's Introduction

java-retrying

License FOSSA Status Build Status codecov

java重试, 支持同步/异步, 简单灵活可配, 不依赖第三方库.

基于guava-retrying改造, 增加了异步重试, 同时去掉了第三方依赖, 使用方法基本一致.

名称 JDK 第三方依赖 同步重试 异步重试
guava-retrying 大于等于6 guava,findbugs Y N
java-retrying 大于等于8 Y Y

Quickstart

  • 依赖
<dependency>
    <groupId>com.github.lowzj</groupId>
    <artifactId>java-retrying</artifactId>
    <version>1.2</version>
</dependency>
  • 同步重试
Retryer<Integer> retryer = RetryerBuilder.<Integer>newBuilder()
    .withWaitStrategy(WaitStrategies.fixedWait(100L, TimeUnit.MILLISECONDS))
    .retryIfResult(num -> num != 5)
    .retryIfExceptionOfType(RuntimeException.class)
    .withStopStrategy(StopStrategies.stopAfterAttempt(7))
    .build();

try {
    retryer.call(noRuntimeExceptionAfter(4));
} catch (ExecutionException | RetryException e) {
    e.printStackTrace();
}
  • 异步重试
AsyncRetryer<Integer> asyncRetryer = RetryerBuilder.<Integer>newBuilder()
    .withWaitStrategy(WaitStrategies.fixedWait(100L, TimeUnit.MILLISECONDS))
    .retryIfResult(num -> num != 4)
    .retryIfExceptionOfType(RuntimeException.class)
    .withStopStrategy(StopStrategies.stopAfterAttempt(7))
    .withExecutor(ExecutorsUtil.scheduledExecutorService("example", 1))
    .buildAsyncRetryer();

CompletableFuture<Integer> future = asyncRetryer.call(noRuntimeExceptionAfter(3));

// get the result asynchronously
future.whenComplete((result, error) -> System.out.println(result));

// or get the result synchronously
try {
    System.out.println(future.get());
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

其中函数noRuntimeExceptionAfter如下:

private Callable<Integer> noRuntimeExceptionAfter(final int attemptNumber) {
    return new Callable<Integer>() {
        private int count = 0;
        @Override
        public Integer call() throws Exception {
            if (count++ < attemptNumber) {
                throw new RuntimeException("count[" + (count - 1) + "] < attemptNumber[" + attemptNumber + "]");
            }
            return count;
        }
    };
}

LICENSE

Java-retrying is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

java-retrying's People

Contributors

lowzj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

java-retrying's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.