I am in the process of writing a web-app that uses multiple web APIs.
For a single request of a single user, my app may need to perform up to 30 HTTP requests to other sites. The site housing the web-app can have hundreds of concurrent users.
I’ve been looking around trying to figure out which library should I use. I’m looking for a mature project that has detailed documentation and tested code, one that will still be around in years to come. Not sure if something like that exists (!)
Couple of questions :
-
In a case such as described above, should I be using an asynchronous HTTP client (without threading), or a regular (possibly pooled) HTTP client (with threading)? Asynchronicity relieves my app from using threads, but makes the code more scattered – will the above mentioned number of requests burden my server too much? (it says here that asynchronous is more scalable)
-
Which library is the common one to use? Is it Apache HttpComponenets HttpClient or its asynch couterpart HttpAsynchClient – which is in Alpha…)? How about jfarcand’s AsyncHttpClient?
Okay, let’s say I will use threads.
After digging around I realize that spawning threads from within a servlet (in my case – a Struts action), may be a big No No :
related questions:
What is recommended way for spawning threads from a servlet in Tomcat
Need help with java web app design to perform background tasks
Can i spawn a thread from a servlet ?
The way I see it, these are my options:
- use my own thread pool (container doesn’t manage my threads)
- use a WorkManager such as CommonJ (seems like an inactive product)
- use a 3rd party scheduler such as Quartz (may be an overkill … ?)
I would appreciate any recommendations for this specific use case – aggregating lotsa data from different web services (this aggregation is invoked by a single user’s single request).
Good question. I would try an asynchronous solution first to see how everything works. The asynchronous solution would be the simplest to implement.
If that doesn’t work, try a more threaded model.
I would use HttpClient for making your requests. I’ve worked with it a lot and use it for any http work that I have to do.