3

I am currently using HttpClient in JDK11 (not Apache's HttpClient). These two HttpClients are similar, but I noticed that Apache's HttpClient can retry the request failure by overriding the HttpRequestRetryHandler class. But I did not find a similar implementation in JDK's HttpClient. How should I implement a similar failure retry mechanism?

Anyone's suggestions would be greatly appreciated.

  • you get back a `CompletableFuture`, you can always write code to retry if that once fails. No such class exists by default, but it is not complicated to write it yourself – Eugene Jun 29 '21 at 14:29

1 Answers1

1

The HttpClient will retry idempotent requests by default. It also retries when failing to connect. It is also possible to alter this default behavior by setting a couple of system properties on the command line (to disable retry on connect failures or enable retry for non idem-potent requests) but that's mostly for testing.

You could also retry a request by using the asynchronous API and registering a dependent action that will retry the request in case of failures - or depending on the returned response code - for instance by using CompletableFuture::handle.

daniel
  • 2,665
  • 1
  • 8
  • 18