-----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

Reply via email to