I know this is an old question, but people keep asking about it all the time, trying to do it (explicitly spawning threads when processing a servlet request) all the time ... This is a very erroneous approach - for more than one reason ... Just saying that Java EE containers frowned in such a practice is not enough, although overall it's true ...
Most importantly, you can never predict how many concurrent requests a servlet will receive at any given time. A web application, a servlet, is, by definition, designed to process multiple requests at a given endpoint at a time. If you program, you request processing logic to explicitly run a certain number of parallel threads, you run the risk of any, but inevitable situation, when available threads end and suffocate your application. Your task performer is always set up to work with a thread pool that is limited to a limited reasonable size. Most often, it does not exceed 10-20 (you do not want too many threads to execute your logic - depending on the nature of the task, the resources for which they are competing, the number of processors on your server, etc.). Say your request handler (for example, an MVC controller method) calls one or more @ Async-annotated methods (in this case, Spring abstracts the task executor and makes the task easier for you) or explicitly uses the task executor. As the code executes, it begins to capture available threads from the pool. This is great if you always process one request at a time without immediate subsequent requests. (In this case, you are probably trying to use the wrong technology to solve your problem.) However, if it is a web application that is exposed to arbitrary (or even famous) clients that can clog the endpoint with requests, you will quickly exhaust the thread pool , and requests will begin to accumulate, waiting for threads to appear. For this reason, you should understand that you can be wrong if you are considering such a design.
The best solution may be the stage of processing data that will be processed asynchronously (it can be a queue or any other type of temporary / intermediate data storage) and return a response. Have an external, independent application or even several instances of it (deployed outside your web container) polling the endpoint (s) of the intermediate process and processing the data in the background, possibly using a finite number of parallel threads. Not only will this solution give you the advantage of asynchronous / parallel processing, but it will also scale, because you can run as many instances of such a prowl as you need, and they can be distributed by pointing to the endpoint of the intermediate level. Hth
cvnew Apr 30 '15 at 17:10 2015-04-30 17:10
source share