Call the System.exit () method in the Servlet destroy () method

This is a continuation of my earlier question .

There was an error in Tomcat 5.0.28 in which the Servlet destroy () method was not called by the container at shutdown. This was fixed in Tomcat 5.0.30, but if the Servlet destroy () method had System.exit (), this would cause the Tomcat Windows service to throw error 1053 and refuse to shut down gracefully (see the link above for more details on this error)

Does anyone know if:

  • Calling System.exit () inside the Servlet destroy () method to force the destruction of any non-daemon threads is a good idea?

  • Why Tomcat 5.0.30 and (later versions, including Tomcat 6.xx) cannot shut down normally if the servlet's destroy () method has System.exit ().

+5
source share
4 answers

You ask two questions:

Question 1: calls the System.exit () method inside the Servlet destroy () method to force the destruction of any non-daemon threads?

A call to System.exit () inside ANY method associated with a servlet is always 100% incorrect. Your code is not the only code running in the JVM - even if you are the only servlet executing (the servlet container has resources that it will need to clear when the JVM really exits.)

- destroy(). , , . ( MyThread ServletManagedThread):

 public class MyServlet extends HttpServlet {
    private List<ServletManagedThread> threads = new ArrayList<ServletManagedThread>();     

     // lots of irrelevant stuff left out for brevity

    public void init() {
        ServletManagedThread t = new MyThread();
        threads.add(t);
        t.start();
    }

    public void destroy() {
        for(ServletManagedThread thread : threads) {
           thread.stopExecuting();
        }
    }
 }

 public abstract class ServletManagedThread extends Thread {

    private boolean keepGoing = true;

    protected abstract void doSomeStuff();
    protected abstract void probablySleepForABit();
    protected abstract void cleanup();

    public void stopExecuting() {
       keepRunning = false;
    }

    public void run() {
       while(keepGoing) {
            doSomeStuff();
            probablySleepForABit();
       }
       this.cleanup();
    }
}

, / concurrency, , , , , , , .

2: Tomcat 5.0.30 ( , Tomcat 6.xx) , System.exit() destroy() ?

. Microsoft , 1053 , Windows , . , Tomcat -, . , System.exit() . Tomcat ( , Catalina) VM (see org.apache.catalina.startup.Catalina.start(), , 5.0.30). JVM System.exit(). , .

(triggered by your System.exit()) ( - ), , 1053, Runtime.exit(int) ( System.exit()):

, . , ; .

" ", , 1053.

, , .

, ( ), .

, System.exit() Tomcat - .

+12

System.exit() Servlet destroy() , daemon, ?

- . destroy() , , : /webapp , webapp , webapp ..

System.exit() JVM! , ?

Tomcat 5.0.30 ( , Tomcat 6.x.x) , destroy() System.exit().

, , .

, , /​​ - , .

+16

System.exit() Servlet destroy() - ?

. , Tomcat, . Tomcat . , .

Tomcat 5.0.30 ( , Tomcat 6.x.x) , System.exit() destroy() .

. ... . . . System.exit, .

, -, , ?

+3

, Jared, keepGoing stopExecuting() , , . ?

0

All Articles