Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should threads have special design to be shutdown gracefully by Tomcat?

I have developed a multithreaded web application that runs in Tomcat. But I cannot use

shutdown.bat

Tomcat didn't stop gracefully. In the debugger I saw that threads continue to run after shutdown command. Do I have to fix my application code to meet special requirements ? Thanks.

like image 907
user710818 Avatar asked Dec 30 '11 14:12

user710818


People also ask

What is graceful shutdown in Tomcat?

graceful shutdown means it will give delay before terminate the process. Here my tomcat process is terminated immediately. I verified using JPS command.

How do I shut down Executorservice gracefully?

When using an Executor, we can shut it down by calling the shutdown() or shutdownNow() methods. Although, it won't wait until all threads stop executing. Waiting for existing threads to complete their execution can be achieved by using the awaitTermination() method.

Is Tomcat thread safe?

Tomcat is the reference implementation of the Servlet and JSP APIs. It is as "thread safe" as any servlet container.

How many threads can be executed at a time in Tomcat?

By default, Tomcat sets maxThreads to 200, which represents the maximum number of threads allowed to run at any given time. You can also specify values for the following parameters: minSpareThreads : the minimum number of threads that should be running at all times. This includes idle and active threads.


1 Answers

... and to tie the other responses to the workings of Java Servlet environments;

if you don't declare your threads as daemon threads, the way to signal the server shutdown to the threads is to implement a ServletContextListener and configure it to your web application (web.xml). When Tomcat is shutting down, it will first shut down each application, which in turn will cause contextDestroyed() method of the listener to be called - and this is where you can signal your own threads that they should finish their work.

like image 131
Juha Laiho Avatar answered Sep 21 '22 06:09

Juha Laiho