快速开始
安装
确保当前的Java
版本为JDK 1.8+
。引入依赖:
xml
<dependency>
<groupId>cn.vlts</groupId>
<artifactId>chance</artifactId>
<version>0.0.2</version>
</dependency>
properties
implementation 'cn.vlts:chance:0.0.2'
TIP
Chance 的最新版本可以通过 Maven 中央仓库查询:https://central.sonatype.com/artifact/cn.vlts/chance
开始使用
java
public static void main(String[] args) {
Chance<String> chance = Chance.<String, Throwable>newBuilder()
.withMaxRetryTimes(3)
.withRetryableExceptions(Collections.singletonMap(RuntimeException.class, Boolean.TRUE))
.build();
try {
String value = chance.call(() -> "HERE IS THE RESULT");
System.out.printf("Result: %s\n", value);
} catch (ChanceException ce) {
Attempt<?, ?> lastFailedAttempt = ce.getAttempt();
Throwable cause = ce.getCause();
// Do something with the lastFailedAttempt or the cause
}
}
输出结果:
shell
Result: HERE IS THE RESULT