2

Possible Duplicate:
How to make an make a web request in an async manner

A very lightweight way in Java to make a synchronous HTTP call is using HttpURLConnection.

Is there an equally easy way to make a non-blocking, 'asynchronous' HTTP call?

I have already looked into: Java NIO, Netty Http Client, Apache Commons HttpClient 4.0 but the solutions are either complex (Java NIO) or introduce a dependency, which I would like to avoid.

Community
  • 1
  • 1
  • Did you also mean non-blocking http calls? You can make a blocking http call in a thread and that would still be asynchronous, but wasn't sure if that's what you wanted – Sripathi Krishnan Aug 30 '12 at 20:07
  • Non-blocking is what I am looking for! I clarified the question above in that regard. Starting up a thread for every request is exactly what I would like to avoid! – chaosmonkey Aug 30 '12 at 20:16

1 Answers1

0

HTTP is a synchronous protocol. In this respect I don't think you should see any significant difference from the client side between these 2 cases. But this depends on also what you mean by lightweight

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • I would assume starting up a thread for every request (honoring the synchronous nature of http in the application) on the client side vs using something based on NIO or the like which handles multiple requests in one thread could make quite a difference? – chaosmonkey Aug 30 '12 at 20:20
  • Lightweight in terms of: easy to integrate into various applications, using as few dependencies as possible – chaosmonkey Aug 30 '12 at 20:21
  • Depends on what you are trying to do.If your client wants to send concurrent requests to the server (i.e. the requests are unrelated to each other) then perhaps with NIO would be better for resources.But if you need to receive the response before sending the next request (which is the usual `HTTP` flow) then you can reuse a thread for that (don't see how NIO would help you here) – Cratylus Aug 30 '12 at 20:31
  • True! That's a good point. Possibly its always a good idea to rather try to batch calls from a client to an HTTP server together and send them in a synchronous way... Still, this is not always possible/desirable. For instance, it could reduce latency for some operations to 'fast-track' calls to the server without having waited out the batch-process (or all pending responses from the server). Also, in my case, one client can have pending requests with _multiple_ servers. – chaosmonkey Aug 31 '12 at 09:19
  • Now I think you are trying to compare NIO with blocking IO and I don't see how the question on HTTP is relevant – Cratylus Aug 31 '12 at 15:52