java-retrying

A simple, flexible and configurable java retrying module

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.github.lowzj
ArtifactId

ArtifactId

java-retrying
Last Version

Last Version

1.2
Release Date

Release Date

Type

Type

jar
Description

Description

java-retrying
A simple, flexible and configurable java retrying module
Project URL

Project URL

https://github.com/lowzj/java-retrying
Source Code Management

Source Code Management

https://github.com/lowzj/java-retrying

Download java-retrying

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.lowzj/java-retrying/ -->
<dependency>
    <groupId>com.github.lowzj</groupId>
    <artifactId>java-retrying</artifactId>
    <version>1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.lowzj/java-retrying/
implementation 'com.github.lowzj:java-retrying:1.2'
// https://jarcasting.com/artifacts/com.github.lowzj/java-retrying/
implementation ("com.github.lowzj:java-retrying:1.2")
'com.github.lowzj:java-retrying:jar:1.2'
<dependency org="com.github.lowzj" name="java-retrying" rev="1.2">
  <artifact name="java-retrying" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.lowzj', module='java-retrying', version='1.2')
)
libraryDependencies += "com.github.lowzj" % "java-retrying" % "1.2"
[com.github.lowzj/java-retrying "1.2"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

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.

Versions

Version
1.2