Skip to content

Commit

Permalink
Replace retry middleware with policy middleware in docs (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran authored Mar 26, 2024
1 parent 8f42e1a commit 1a456b5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 7 additions & 9 deletions Best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,18 @@ ApiRootBuilder.of().withClientCredentialsFlow(
);
```

#### RetryMiddleware
The RetryMiddleware can be used to retry failed requests based on the given options. It will use an incremental backoff strategy for retries. By default it will retry on HTTP status code 500 and 503
```java
ApiRootBuilder.of().withRetryMiddleware(3);
```
#### PolicyMiddleware
The PolicyBuilder allows the combination of different policies for failing requests. You can specify the error codes to retry, different retry strategy, timeout etc.

The middleware can retry on specific HTTP status codes or exception types

```java
ApiRootBuilder.of().withRetryMiddleware(3, Arrays.asList(500, 503));
ApiRootBuilder.of().withRetryMiddleware(3, Arrays.asList(500, 503),
Collections.singletonList(InterruptedException.class));
ApiRootBuilder.of().withPolicies(policyBuilder -> policyBuilder.withRetry(retry -> retry.maxRetries(3)
.statusCodes(Arrays.asList(HttpStatusCode.SERVICE_UNAVAILABLE_503,
HttpStatusCode.INTERNAL_SERVER_ERROR_500))))
```

For more examples see [the PolicyMiddleware test](https://github.com/commercetools/commercetools-sdk-java-v2/blob/0251e8aa150af60c6ff6253e41180bfe0a10036b/rmf/rmf-java-base/src/test/java/io/vrap/rmf/base/client/http/PolicyMiddlewareTest.java).

#### UserAgentMiddleware
The UserAgentMiddleware adds a User-Agent header to every request. It will be added by using the `defaultClient` builder method. But can also be customized
```java
Expand Down
4 changes: 3 additions & 1 deletion Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ After that, we will have our request <strong>CategoryPagedQueryResponse</strong>
.defaultClient(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl(),
ClientCredentials.of().withClientId("clientId").withClientSecret("clientSecret").build(),
ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl())
.withRetryMiddleware(5, Arrays.asList(BAD_GATEWAY_502, SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504))
.withPolicies(
policyBuilder -> policyBuilder.withRetry(retry -> retry.maxRetries(5)
.statusCodes(Arrays.asList(BAD_GATEWAY_502, SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504))))
.buildForProject("projectKey");

final CategoryPagedQueryResponse body = projectClient.categories().get().executeBlocking().getBody();
Expand Down

0 comments on commit 1a456b5

Please sign in to comment.