5

After some benchmarking I've found that AsyncHttpClient (https://github.com/AsyncHttpClient/async-http-client) seems to be the most stable and scalable async http client out there as it's based on NIO and seems to scale very well during load. I compared it against OkHttp and Apache Async and it seems to perform really well when simulating a backend with latency.

Unfortunately I have not yet found any way to expose it as a Spring AsyncRestTemplate, making a migration in our existing codebase a pain.

Does anyone know of any good bridge to RestTemplate's using the library, or if otherwise, how to create an issue in the Spring project to include it among the other Async http client factories?

Billybong
  • 697
  • 5
  • 18

2 Answers2

4

You can't use RestTemplate for async requests, that's what the AsyncRestTemplate is for. You'll need to implement your own AsyncClientHttpRequestFactory. I briefly looked into the link you provided in your post, and it looked like you could wrap a AsyncRestClient and return BoundRequestBuilder from AsyncClientHttpRequestFactory.createAsyncRequest. Then onwards, you basically need to delegate the calls from Spring-specific interfaces to AsyncRestClient-specific classes. It shouldn't be too hard.

That said, Spring 5 Web comes with a WebClient that does async and more. I suggest seriously considering it before building your own async library, albeit on top of another one.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
  • Thanks for the answer. The reason for using AsyncHttpClient is that I've found it to be the best client by far when operating during heavy concurrent load out of the http clients I've tried. – Billybong Sep 05 '17 at 10:11
  • @Billybong ok, but I don't remember asking why you chose what you chose – Abhijit Sarkar Sep 05 '17 at 16:43
  • Fair deal, but you hinted at the WebClient which I've not yet performance tested and I'm simply stating the reason why I haven't chosen that route. – Billybong Sep 06 '17 at 10:03
  • @Billybong I see. My point stays though, that if you're able to use already written code instead if rolling your own, that's always a win. Of course, if already written code doesn't meet your requirements, then you're left with no choice but to customize it or create new software. – Abhijit Sarkar Sep 06 '17 at 17:50
-4

Here is an official java doc of spring RestTemplate.

Note: by default the RestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp through the HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) property.

EDIT: OK here you go with spoon feeded answer:

AsyncRestTemplate template = new AsyncRestTemplate(
                new HttpComponentsAsyncClientHttpRequestFactory());

HttpComponentsAsyncClientHttpRequestFactory is part of spring since 4.0

kiran
  • 223
  • 4
  • 12
  • This doesn't answer OP's question at all. You didn't say how to support `AsyncHttpClient`, you merely proved you've internet connection. – Abhijit Sarkar Aug 27 '17 at 08:08
  • @AbhijitSarkar, Can u please mind removing negetive vote for this answer? – kiran Aug 31 '17 at 04:13
  • 2
    First of all, downvotes are anonymous, so you don't know if it was me or someone else, and I would downvote you now if I already didn't for assuming it was me. Second of all, if I did down vote you, there would have been a reason for that; what makes you think you can change my mind? It happens all the time that people may not like your answer; move on. – Abhijit Sarkar Aug 31 '17 at 04:20
  • Just so that you know, the class you referred to in your edit is from the http components library, which is not the same library the OP is referring to. – Abhijit Sarkar Aug 31 '17 at 04:26