-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Lance,

On 7/18/17 9:56 AM, Campbell, Lance 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.

You want an ExecutorService (provided by the Java standard library),
and you can limit the number of simultaneous jobs to 1. Submit the
jobs to the service as they arrive, and then make sure you call
service.shutdown() when the servlet (or better yet, context) is being
taken out of service. You might want to provide a way to cancel
in-process jobs, otherwise you may stall container shutdown which
might be inconvenient for you.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJZbn3uAAoJEBzwKT+lPKRYCTUP/1Ho+5z49FntCBrEevVxnYiO
He331XbbPN7x6XLzO8nqf82pZcjnH1DGPtZXhPdZ2DKOjqPKmSeoI3az1+s1gu6X
Igff6xoHpARdyWdVElIxHYO93QOJdIa0DtuaSgaO4HXwpZYgwxs/QwbuN9VWLK5f
4afXp0gE5VaUxZoLHb9TTVIV8t0IABXdq+m9tuKZtrihfVwyc79w0HTwDCzqHlYH
39p70KQEagzhj/ZNqUHZENvFZ+2vMHD8zGcnWtAoVzOaseNth0FtZY5aqO+2WThl
k1VpcCGIK6hsytcBXuYAtfp/H6o2ircpwa66+O1nbNuP3OLT9x4IHqe5I0KoG/zG
hPOA/ydauaTps4xoYVLPjIVfVLx4D3+Rcrsti2tlo80hRK4Mv3SEeZu5eH1Zxz/N
W1MSLrVa5QOdWQ1rLMUbfWyNFXbm3xT8DrlQ3WvsAlMsRCrHkBIPqihnN7cPdRKp
m7QsQGYk/QNJ4U45ZeF/n5e/P62Dw34dHEbsefXNozvAOzykRSmwSG+6guCVXiiL
tfxC72zOiPxHUFMO8+mfXUrbsODNUjAUQA8O5dphwHGkTIni9xAL3/38dxkyaVy3
1jU2CFGaou5rdvjeL/Ixwqs4LzsY4Li0DrA5MwJVjEJG62MGK9x65NcpQ/3AtvZ4
T6lRQ5ZjJH99h9b2/ijL
=SVOd
-----END PGP SIGNATURE-----

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

Reply via email to