https://github.com/lowzj/java-retrying
java retry module, based on guava-retrying, support sync/async retry
https://github.com/lowzj/java-retrying
async-retry java java-retry retry
Last synced: 6 months ago
JSON representation
java retry module, based on guava-retrying, support sync/async retry
- Host: GitHub
- URL: https://github.com/lowzj/java-retrying
- Owner: lowzj
- License: apache-2.0
- Created: 2018-02-13T03:53:46.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-02T00:38:25.000Z (over 7 years ago)
- Last Synced: 2023-07-26T21:59:57.916Z (about 2 years ago)
- Topics: async-retry, java, java-retry, retry
- Language: Java
- Homepage:
- Size: 49.8 KB
- Stars: 22
- Watchers: 3
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# java-retrying
[](https://github.com/lowzj/java-retrying/blob/master/LICENSE)
[](https://app.fossa.io/projects/git%2Bgithub.com%2Flowzj%2Fjava-retrying?ref=badge_shield)
[](https://travis-ci.org/lowzj/java-retrying)
[](https://codecov.io/gh/lowzj/java-retrying)java重试, 支持同步/异步, 简单灵活可配, 不依赖第三方库.
基于[guava-retrying](https://github.com/rholder/guava-retrying)改造, 增加了异步重试, 同时去掉了第三方依赖, 使用方法基本一致.
名称 | JDK | 第三方依赖 | 同步重试 | 异步重试
---- | --- | ---- | -------- | --------
guava-retrying | 大于等于6 | guava,findbugs | Y | N
java-retrying | 大于等于8 | 无 | Y | Y## Quickstart
* 依赖
```xmlcom.github.lowzj
java-retrying
1.2```
* 同步重试
```java
Retryer retryer = RetryerBuilder.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();
}
```* 异步重试
```java
AsyncRetryer asyncRetryer = RetryerBuilder.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 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`如下:
```java
private Callable noRuntimeExceptionAfter(final int attemptNumber) {
return new Callable() {
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](https://github.com/lowzj/java-retrying/blob/master/LICENSE) for the full license text.