GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - lowzj/java-retrying: java retry module, based on guava-retrying, support sync/async retry

BranchesTags
Open more actions menu

Latest commit

 

History

22 Commits
22 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pom.xml
pom.xml
 
 

Repository files navigation

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.

About

java retry module, based on guava-retrying, support sync/async retry

Topics

async-retryjavajava-retryretry

Resources

Readme
Apache-2.0 license
Activity

Stars

29 stars

Watchers

1 watching

Forks

13 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.