...
The HTTP Base Client is an abstraction of the OkHttpClient. It provides features such as retry handlers (see Retries) and automated paginated requests.
...
@Singleton
public class MyCustomService {
private Counter counter;
@PostConstruct
void init(MeterRegistry meterRegistry) {
counter = Counter.builder("pdp.mycustomservice.count")
.tag("key", "value")
.register(meterRegistry);
}
public void count() {
counter.increment();
}
}
Retries
RetryUtils
, as its name may suggest, provide utils or methods to handle the retry of a Runnable
or Supplier
. It also handles the exceptions might occur during the run.
By default, the RetryUtils
will use a ConstantDelayRetry
, wich will retry 3 times with a delay of 5 seconds.
...
Retries are managed by resilience4j, see more. There is a backoff policy for retries: pdp.core-common.config.BackoffPolicyProperties
, example of implementation:
final var random = new AtomicIntegerRandom();
RetryUtils.retry(atomicInteger::getAndIncrement)
You can also specify a custom retry policy passing it through parameters.
...
AtomicInteger atomicInteger = new AtomicInteger();
RetryUtils.retry(atomicInteger::getAndIncrement, new ConstantDelayRetry(2, 100))
...
final int upperbound = 25;
final var backoffPolicy = new BackoffPolicyPropertiesImpl();
// Retry configuration
Predicate<Integer> isOdd = number -> number % 2 == 0;
var retryConfig = backoffPolicy.<Integer>getRetryConfigBuilder()
.retryOnResult(isOdd)
.failAfterMaxAttempts(true)
.build();
var retry = Retry.of("Get odd number", retryConfig);
return Retry.decorateFunction(retry, f -> random.nextInt(upperbound)).apply(isOdd);
Scheduler
Scheduled tasks are handled by Quartz. Any application can inject the org.quartz.Scheduler
bean to register/unregister its own cron jobs.
...