I was recently asked to modify a web application that initiates a very long running task that generally results in correspondingly long user wait time for a page to be displayed. The task itself consists of smaller, independent, individual tasks that, ideally, would be spawned as separate threads and aggregated in a parent task. However, I know creating threads in the servlet container is not recommended.
I have done quite a lot of research on the web and, frankly, while I have uncovered some possible solutions, there seems to be no consensus on the best way to implement a solution for such a scenario. It seems that I am not the only one who has faced such a problem. JSRs 236 and 237 called for a solution but appear to have gone nowhere. JSR 342 (Java EE 7) may resuscitate JSR 236 but I cannot wait. I need a Java EE 5/6 solution.
The
CommonJ Work Manager implementation may be a possible solution but is this the way to go? Has anyone else used the CommonJ Work Manager API? If so, can you recommend it?
The Quartz Scheduler seems to be highly recommended but this is NOT a task that may be scheduled. It is an on-demand task and may be requested perhaps 1 to 3 times per day or perhaps not all for a particular day.
Ideally, I would like to use the java.util.concurrent API introduced in Java SE 5. Has anyone else done used this API in Tomcat? If so, can you recommend it? Assuming Java EE 7 does, in fact, eventually implement JSR 236, it seems like this may be the best approach. When the Java EE 7 API becomes available, the code could simply be modified to use the corresponding
managed Java EE API classes.
FYI, I am NOT concerned about exceptionally long running tasks or renegade threads. While, as a developer, I would never say never, those scenarios will NOT occur in this particular application. Although RELATIVELY long running, each individual task would generally never exceed 1 second in elapsed time. What makes the parent task long running is that there may be thousands of the individual tasks. If not for the prohibition against creating threads in the servlet container, I would simply create a class that implements Runnable, execute a limited number of threads, perhaps 10, at any given time and aggregate the results in a parent class as each individual task completes.
Assuming I am able to implement a threading solution of some sort, what I would like to do is return the page immediately after submitting a job thread to improve user response time then use Ajax calls to periodically check the job status on the server and update the page as results become available.
I have actually seen applications that have created their own threads in Tomcat and not experience any problems (as long as the thread execution time is short and the thread does not fail). However, I have always been leery of such implementations because I know it is not recommended. I would like to implement a solution for this scenario using the recommended best practices. The only problems is I am having trouble ascertaining what those best practices are.
I would appreciate any recommendations on how to approach a solution to this problem.