A simple failover library for Java
- jdk1.8 only
<dependency>
<groupId>com.github.phantomthief</groupId>
<artifactId>simple-failover</artifactId>
<version>0.1.16</version>
</dependency>
class MyClientFailoverHolder {
private final Failover<MyClient> failover = WeightFailover.<MyClient> newGenericBuilder()
.checker(this::checkAlive)
.build(allClients(), 100);
private List<MyClient> allClients() {
// ...
}
private double checkAlive(MyClient myClient) {
if (checkAlive(myAlive)) {
return 1.0D;
} else {
return 0.0D;
}
}
public void foo() {
MyClient client = failover.getOneAvailable();
try {
client.doSomething();
failover.success(client);
} catch (Throwable e) {
failover.fail(client);
throw e;
}
}
public void foo2() {
failover.runWithRetry(MyClient::doSomething);
}
}