On 18/07/17 18:07, Campbell, Lance wrote:
> Also if tomcat is shut down and a thread is running how does tomcat 
> communicate with my software to end a thread?

It can't. The thread will either just stop if it is a Daemon thread or
the Java process won't exit until your thread does.

>  Is this where you were referring to the servlet context listener? 

Yes. That is a good way to start/stop threads.

> 
> -----Original Message-----
> From: Campbell, Lance [mailto:la...@illinois.edu] 
> Sent: Tuesday, July 18, 2017 12:00 PM
> To: Tomcat Users List <users@tomcat.apache.org>
> Subject: RE: run thread from servlet
> 
> 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.

Memory leaks are likely because the Thread will almost certainly load
classes from the web application.

A (possibly single threaded) Executor started and stopped via a
ServletContextListener is the solution that springs to mind.

Mark


> 
> -----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
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to