Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach to run a long process from a java servlet?

I would like to ask what is the best approach to run a long process from a java servlet. I have a webapp and when the client do a request it runs a servlet. This servlet should get some parameters from the request and then runs a process. This process may take a long time so I need to run it separately. When this process executed finish, it send an email with the results.

Thanks in advance.

like image 301
Esteban S Avatar asked Jan 22 '13 12:01

Esteban S


1 Answers

Use a thread pool. Each time you receive a request, create a task and submit it to the thread pool. This will ensure too many requests don't bring the server to its knees, because you'e in control of how many concurrent threads you can have, and how many tasks can wait in the thread pool's queue of waiting tasks.

See the javadoc for Executors and ThreadPoolExecutor.

like image 159
JB Nizet Avatar answered Sep 21 '22 06:09

JB Nizet