简体   繁体   中英

The failure retry mechanism of HttpClient in JDK11

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM