This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens to threads started from a Servlet when Tomcat is stopped?  RSS feed

 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on an application that starts several threads from a Servlets' init() method. These threads check periodically for the availability of a local map servers by sending a request and examining the response code.

Lately we have been seeing way too many of this type of request coming to the map servers and I'm wondering if some threads from a previous startup could still be alive.

The Servlets destroy() method calls cancel() on the threads so I think the only possibility is that a non-typical shutdown or crash would somehow leave threads active.

Is there any scenario where this is possible?

Thanks for any help or ideas,
-=beeky
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the Threads to be alive they must be running in a JVM so if Tomcat completely exited there will be no Java Threads.

This does not mean that your pending map server requests have been properly shut down. You should ensure that the destroy() method can properly shut down the map server request.

If you read through the java.lang.Thread JavaDocs you will see lots of methods such as stop() that have been deprecated because of the difficulty in stopping a running Thread cleanly.

Bill
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be good if you make the threads (threads that you are starting from init of the servlet)as deamon.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be good if you make the threads (threads that you are starting from init of the servlet)as deamon.


Deamon Threads will be dropped automatically when the JVM exits, but is that really what we want in this case? The Thread may be part way through sending a request to the map server, can the map server recover from this?

Bill
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William : The Servlets destroy() method calls cancel() on the threads so I think the only possibility is that a non-typical shutdown or crash would somehow leave threads active.


Making the thread daemon would at least help in this situation.
 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if this situation could be caused by multiple instances of the servlet being created by Tomcat. The web.xml specifies one initial instance but could Tomcat be creating more? Each instance of this servlet would start another thread to do the mapper checking.

This scenario does not seem too likely. Given the number of requests, there would have to be a hell of a lot of instances of the servlet. One servlets map checker thread sends a request every 30min. We are seeing an average of 5 requests per minute which would equate to something like 150 servlet instances.

Any thoughts on this?

Thanks,
-=beeky
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat follows the spec so it will only create one (unless your servlet implements SingleThreadModel).
The spec does allow it to take servlets out of service and put them back in when needed. This would cause your destroy and init methods to be called and is a good reason to use a contextListener to start and stop these threads instead of a servlet.

But, when the JVM stops, all threads stop.



SRV.2.2 Number of Instances
The servlet declaration which is part of the deployment descriptor of theWeb application
containing the servlet, as described in Chapter SRV.13, �Deployment
Descriptor�, controls how the servlet container provides instances of the servlet.
For a servlet not hosted in a distributed environment (the default), the servlet
container must use only one instance per servlet declaration. However, for a servlet
implementing the SingleThreadModel interface, the servlet container may
instantiate multiple instances to handle a heavy request load and serialize requests
to a particular instance.

[ September 14, 2007: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this:

SRV.2.3.4 End of Service
The servlet container is not required to keep a servlet loaded for any particular
period of time. A servlet instance may be kept active in a servlet container for a
period of milliseconds, for the lifetime of the servlet container (which could be a
number of days, months, or years), or any amount of time in between.
When the servlet container determines that a servlet should be removed from
service, it calls the destroy method of the Servlet interface to allow the servlet to
release any resources it is using and save any persistent state. For example, the
container may do this when it wants to conserve memory resources, or when it is
being shut down.
Before the servlet container calls the destroy method, it must allow any
threads that are currently running in the service method of the servlet to complete
execution, or exceed a server-defined time limit.


is why you should code your servlets as if the container can create as many as it likes and why you should use ContextListeners when available for startup and shutdown tasks like this.
 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all who have contributed to this thread about threads. I'm convinced that the problem is not caused by some sort of aberrant threading behavior. Not only does it not make sense from the Java perspective but in the last couple of hours I have shut down Tomcat on the three suspect machines and there was no diminution of the mapper requests. So the requests must be coming from somewhere out of our development/production environment.

Thanks again to all who took a stab at this problem. I always learn something about Java when I post to this board even if my original problem is not solved.

-=beeky
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!