Class RequestPolicyBuilder

java.lang.Object
io.vrap.rmf.base.client.http.RequestPolicyBuilder

public class RequestPolicyBuilder extends Object

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.