Package io.vrap.rmf.base.client.http
Class RequestPolicyBuilder
java.lang.Object
io.vrap.rmf.base.client.http.RequestPolicyBuilder
RequestPolicyBuilder
The RequestPolicyBuilder allows the combination of different policies for failing requests and apply them to matching requests.
The order of policies matters. For example applying a timeout
before
PolicyBuilder.withRetry(RetryPolicyBuilder)
retry} will time out across all requests whereas applying a timeout after the retry count
against every single request even the retried ones.
Retry
Retrying on HTTP status codes
final ApiHttpClient build = ClientBuilder.of() // ... .withRequestPolicies(policyBuilder -> policyBuilder .withAllOtherRequests(request -> request.withRetry(retry -> retry.maxRetries(3) .statusCodes(Arrays.asList(HttpStatusCode.SERVICE_UNAVAILABLE_503, HttpStatusCode.INTERNAL_SERVER_ERROR_500))))) .build();
See the test code.
Retrying specific exceptions
final ApiHttpClient build = ClientBuilder.of() // ... .withRequestPolicies(policyBuilder -> policyBuilder.withAllOtherRequests(request -> request .withRetry(retry -> retry.maxRetries(3).failures(singletonList(JsonException.class))))) .build();
See the test code.
Timeout
final ApiHttpClient build = ClientBuilder.of() // ... .withRequestPolicies(policyBuilder -> policyBuilder.withAllOtherRequests( request -> request.withTimeout(Duration.ofSeconds(10), TimeoutBuilder::withInterrupt))) .build();
See the test code.
Bulkhead
Implementation of a Queue to limit the number of concurrent requests handled by the client
final ApiHttpClient build = ClientBuilder.of() // ... .withRequestPolicies(policyBuilder -> policyBuilder .withAllOtherRequests(request -> request.withBulkhead(64, Duration.ofSeconds(10)))) .build();
See the test code.
-
Constructor Summary
ConstructorsConstructorDescriptionRequestPolicyBuilder
(dev.failsafe.spi.Scheduler scheduler, Map<Predicate<ApiHttpRequest>, List<dev.failsafe.Policy<ApiHttpResponse<byte[]>>>> policies) RequestPolicyBuilder
(Map<Predicate<ApiHttpRequest>, List<dev.failsafe.Policy<ApiHttpResponse<byte[]>>>> policies) -
Method Summary
Modifier and TypeMethodDescriptionbuild()
static RequestPolicyBuilder
of()
withRequestMatching
(Predicate<ApiHttpRequest> predicate, Function<PolicyBuilder, PolicyBuilder> fn) withScheduler
(dev.failsafe.spi.Scheduler scheduler) withScheduler
(ExecutorService scheduler) withScheduler
(ScheduledExecutorService scheduler)
-
Constructor Details
-
RequestPolicyBuilder
public RequestPolicyBuilder() -
RequestPolicyBuilder
public RequestPolicyBuilder(Map<Predicate<ApiHttpRequest>, List<dev.failsafe.Policy<ApiHttpResponse<byte[]>>>> policies) -
RequestPolicyBuilder
public RequestPolicyBuilder(dev.failsafe.spi.Scheduler scheduler, Map<Predicate<ApiHttpRequest>, List<dev.failsafe.Policy<ApiHttpResponse<byte[]>>>> policies)
-
-
Method Details
-
withScheduler
-
withScheduler
-
withScheduler
-
withRequestMatching
public RequestPolicyBuilder withRequestMatching(Predicate<ApiHttpRequest> predicate, Function<PolicyBuilder, PolicyBuilder> fn) -
withAllOtherRequests
-
build
-
of
-