Re: run thread from servlet

2017-07-19 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Lance,

On 7/19/17 7:35 AM, Campbell, Lance wrote:
> Thanks for your information.  So when I have a process that I want
> to run as a thread I would assume I need to implement the
> interface ServletContextListener.   I would also assume that the
> servlet that creates the process will call the following method:
> 
> this.getServletContext().addListener(myProcess)
> 
> That way the Tomcat container can send a message to myProcess to
> tell it to end itself because Tomcat is trying to stop.
> 
> Did I understand that correctly?

I'm not entirely sure I understand what /you/ are saying above. I'll
explain.

During context ("webapp") startup, Tomcat will call init() on any
registered ServletContextListeners. You can register them either via
configuration in web.xml or via code annotations.

During context shutdown, Tomcat will call destroy() on those same
listeners.

Your servlets do not need to do anything to interact with those
listeners: no need to call addListener or anything like that.

You can do anything you want in those two methods.

Here's my recommendation:

1. Write a ServletContextListener that does the following:
  a. on init(), creates an ExecutorService with as many threads as you
want (you want one-at-a-time semantics, so that would be only a single
thread).

  b. places itself into the ServletContext ("application") under a
well-known attribute name (e.g. "MyJobRunnerService").

  c. on destroy(), shuts-down the ExecutorService, potentially
cancelling any in-progress operations it's performing (in order to
shut-down quickly)

2. In your servlet that accepts jobs, grab the JobRunnerService from
the ServletContext and call a method like "submit" on the job and
stop. The ExecutorService will handle things from there.

This should give you a solution that nicely-separates your work into
two operations: submitting jobs (the servlet) and actually running the
jobs (the JobRunnerService). The ExecutorService handles the
heavy-lifting of managing threads and actually running the jobs.

Makes sense?

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

iQIcBAEBCAAGBQJZb28OAAoJEBzwKT+lPKRYc/8P/i3LnqPe/SNFPPqbhcK6o557
9ZmP4n2n8Ywt8hs4MOf2Oe+2E1Xxr46Pj73jxfpJgRkpXfCt7yRHLIRteCa5Vxt3
/aecIn7yxM3bfGJUHr8OnAnAipnN+80sUKLvjnov5lO2Mj0+GOomqtWKkSkoZGvr
itGIYuOYFBT8eYIhEaOoVgXCIBuK0s0qpNlTMxfyTnLZ6TWZSOsM8unDSWTpeHUs
G8ut9fJ8tp8Xi0seMAu4crMLEg7d3FiALhtGFrDIS8UzB9xYCWuY797D2+s8673u
u9bIWtIXwjrEzmAuIWRZm7MEO7R0IY8a5iMK+qvNMPNEVIAglMZPI2//YllmNwq+
tQDrvsS6zKzgMgSS1GCa45OoKphOl2ea7bR7Y7rmf1zT1UviKqIJ9EBFXUliE9dQ
zub87m7aRdQNovgU2h/ZGUYoTjFPW2WJzyAA7/hGFlsXSzMPAs3OeVNOL11EY1Pb
g97/+fkG2uY5eVTORdSvEgPulOZ0TOBc79i80hn9Kz7fG1svbSwC4F/z41Yi/rsP
K4yx2c6T+0+7DWwhxVy8Pn4vSbBAof0Vhj51jdpYYfJGExcNrmxSKsgxKftrrPOt
pjZymykGnpKzt6lNXMUseX9UOLpn1vAXzRSXIXWgno7AXNbpWfhwfuICy0f4eeq+
TrFpAV4NJdTklAZtImBm
=QyVQ
-END PGP SIGNATURE-

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



RE: run thread from servlet

2017-07-19 Thread Campbell, Lance
Chris,
Thanks for your information.  So when I have a process that I want to run as a 
thread I would assume I need to implement the interface ServletContextListener. 
  I would also assume that the servlet that creates the process will call the 
following method:

this.getServletContext().addListener(myProcess)

That way the Tomcat container can send a message to myProcess to tell it to end 
itself because Tomcat is trying to stop.

Did I understand that correctly?

Thanks,

Lance

-Original Message-
From: Christopher Schultz [mailto:ch...@christopherschultz.net] 
Sent: Tuesday, July 18, 2017 4:30 PM
To: users@tomcat.apache.org
Subject: Re: run thread from servlet

-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


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



Re: run thread from servlet

2017-07-18 Thread Christopher Schultz
-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



Re: run thread from servlet

2017-07-18 Thread Mark Thomas
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 
> 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 
> 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"  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



RE: run thread from servlet

2017-07-18 Thread Campbell, Lance
Also if tomcat is shut down and a thread is running how does tomcat communicate 
with my software to end a thread?  Is this where you were referring to the 
servlet context listener? 

-Original Message-
From: Campbell, Lance [mailto:la...@illinois.edu] 
Sent: Tuesday, July 18, 2017 12:00 PM
To: Tomcat Users List 
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.  

-Original Message-
From: Pradip Bhattacharya [mailto:pradip.bhattacha...@gmail.com] 
Sent: Tuesday, July 18, 2017 9:10 AM
To: Tomcat Users List 
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"  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


RE: run thread from servlet

2017-07-18 Thread Campbell, Lance
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 
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"  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


RE: run thread from servlet

2017-07-18 Thread Tran, Dung Minh
Hello Pradip,
   Thanks for the comments. Could you please clarify the statement "the servlet 
can enque the data in a concurrent queue, and then
notify the object " ? Would you assume that there would be two threads in the 
servlet ? One is context listener thread, and the other is enqueue data thread ?
Thanks,
Tom

From: Pradip Bhattacharya [pradip.bhattacha...@gmail.com]
Sent: Tuesday, July 18, 2017 10:09 AM
To: Tomcat Users List
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"  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



This e-mail message (including any attachments) is for the sole use of
the intended recipient(s) and may contain confidential and privileged
information. If the reader of this message is not the intended
recipient, you are hereby notified that any dissemination, distribution
or copying of this message (including any attachments) is strictly
prohibited.

If you have received this message in error, please contact
the sender by reply e-mail message and destroy all copies of the
original message (including attachments).

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



Re: run thread from servlet

2017-07-18 Thread Pradip Bhattacharya
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"  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