Java – designed for multithreaded rest API clients
I am developing a program that receives a search request for a topic, calls the New York Times API to get articles related to the topic, then goes to the twitter API to get tweets about articles, and finally processes the results and returns them
I have to make this multithreaded I thought about using executorservice. Net with a fixed size thread pool Therefore, each incoming search request will be processed by a separate thread I also use the callable interface to submit tasks The class implementing callable performs API processing (making and receiving API requests / responses) Finally, the results are then obtained by future and displayed as output This happens every time an incoming request is made
Does that make sense? Or is there a better way?
Edit: I run it on my local machine and accept data from the command line interface
Solution
If this is a web application, it is multithreaded by default If not - you can still deploy it on the servlet container, that would be beneficial The thread pool is provided by the underlying container (such as Tomcat) Each request is serviced by a separate thread
The only thing to care about:
>Do not use synchronized > to clean up any ThreadLocal variables you use