Basically I have batch jobs that I need to run on many different web 
applications.  The batch jobs call servlets that then create threads to do 
whatever task I need done behind the scenes.  The servlets immediately return a 
message after starting the batch threaded job.    However my understanding is 
that there can be issues with Tomcat detecting memory leaks if you have a 
thread spawned by a servlet.  So I was wondering if there was a better way to 
trigger a thread.  

-----Original Message-----
From: Pradip Bhattacharya [mailto:pradip.bhattacha...@gmail.com] 
Sent: Tuesday, July 18, 2017 9:10 AM
To: Tomcat Users List <users@tomcat.apache.org>
Subject: Re: run thread from servlet

Hello,
I hope I understood the requirement correctly. I believe you can start 
EmailProcess thread by the context listener. This thread can wait on an object. 
And the servlet can enque the data in a concurrent queue, and then notify the 
object, on which EmailProcess thread is waiting.
Is this EmailProcess is an asynchronous service to send emails ?
Regards
Pradip.B

On Jul 18, 2017 7:27 PM, "Campbell, Lance" <la...@illinois.edu> wrote:

Tomcat 8.0.x
Question:
I am wanting to know the proper way to start a thread from a servlet.

Use Case:
A batch process will call a URL that is a servlet.  The servlet will call a 
processes that will trigger a thread to run to do a particular job.  The thread 
will run for a while.  The servlet will not wait on the thread to finish 
running.  Also the thread that is started is running a processes that requires 
there to only be one of these processes running at any given time.

What I have done:
I created a class I call EmailProcess.  I have a static Boolean flag in the 
class called isWorking that indicates if the process is running.  The class 
extends Runnable.  The class also has a static method to start the thread if 
isWorking flag is false.  I have a servlet that will call the static method in 
EmailProcess to start the thread if possible.

Note: If the Tomcat container decides to clean up the servlet that is called 
that triggers the thread I don't want it to mess with the thread.  I want it to 
keep on running.

Thanks for your advice.

Lance

Reply via email to