import com.github.javafaker.Faker;
import com.github.javafaker.service.FakeValuesService;
import com.github.javafaker.service.RandomService;
import me.tongfei.progressbar.ProgressBar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import java.time.Duration;
import java.util.concurrent.*;
public class DeclineExample {
private static final Logger logger = LoggerFactory.getLogger(DeclineExample.class);
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
RetryConfig config = RetryConfig.custom()
.maxAttempts(3)
.waitDuration(Duration.ofMillis(100))
.retryOnResult(result -> result == null)
.build();
Retry retry = Retry.of("example", config);
Decline<String> decline = Decline.of(retry);
Future<String> future = executor.submit(() -> {
FakeValuesService fakeValuesService = new FakeValuesService(
new Locale("zh-CN"), new RandomService());
String fakeName = fakeValuesService.resolve("name.full_name", new Faker());
Thread.sleep(500);
return fakeName;
});
String result = decline.getOrThrow(future, Duration.ofMillis(200), () -> "Timeout");
logger.info("Result: " + result);
executor.shutdown();
}
}