Skip to content

Commit 1568b3a

Browse files
authored
add Retry example to README
1 parent 8c0527f commit 1568b3a

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A lightweight wrapper to the [JDK 11+ Java Http Client](http://openjdk.java.net/
2424
<dependency>
2525
<groupId>io.avaje</groupId>
2626
<artifactId>avaje-http-client</artifactId>
27-
<version>1.17</version>
27+
<version>${avaje.client.version}</version>
2828
</dependency>
2929
```
3030

@@ -63,7 +63,7 @@ From HttpClientContext:
6363

6464

6565
## Limitations:
66-
- NO support for POSTing multipart-form currently
66+
- No support for POSTing multipart-form currently
6767
- Retry (when specified) does not apply to `async` response processing`
6868

6969

@@ -299,7 +299,39 @@ HttpResponse<Void> res = clientContext.request()
299299
assertThat(res.statusCode()).isEqualTo(201);
300300
```
301301

302+
## Retry (Sync Requests Only)
303+
To add Retry funtionality, use `.retryHandler(yourhandler)` on the builder to provide your retry handler. The `RetryHandler` interface provides two methods, one for status exceptions (e.g. you get a 4xx/5xx from the server) and another for exceptions thrown by the underlying client (e.g. server times out or client couldn't send request). Here is example implementation of `RetryHandler`.
302304

305+
```
306+
public final class ExampleRetry implements RetryHandler {
307+
private static final int MAX_RETRIES = 2;
308+
@Override
309+
public boolean isRetry(int retryCount, HttpResponse<?> response) {
310+
311+
final var code = response.statusCode();
312+
313+
if (retryCount >= MAX_RETRIES || code >= 400) {
314+
315+
return false;
316+
}
317+
318+
return true;
319+
}
320+
321+
@Override
322+
public boolean isExceptionRetry(int retryCount, HttpException response) {
323+
//unwrap the exception
324+
final var cause = response.getCause();
325+
if (retryCount >= MAX_RETRIES) {
326+
return false;
327+
}
328+
if (cause instanceof ConnectException) {
329+
return true;
330+
}
331+
332+
return false;
333+
}
334+
```
303335

304336
## Async processing
305337

0 commit comments

Comments
 (0)