org/sonews/daemon/ShutdownHook.java
author chris <chris@marvin>
Wed, 01 Jul 2009 10:48:22 +0200
changeset 2 1090e2141798
parent 1 6fceb66e1ad7
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon;
    20 
    21 import java.sql.SQLException;
    22 import java.util.Map;
    23 
    24 /**
    25  * Will force all other threads to shutdown cleanly.
    26  * @author Christian Lins
    27  * @since sonews/0.5.0
    28  */
    29 class ShutdownHook extends Thread
    30 {
    31 
    32   /**
    33    * Called when the JVM exits.
    34    */
    35   @Override
    36   public void run()
    37   {
    38     System.out.println("sonews: Trying to shutdown all threads...");
    39 
    40     Map<Thread, StackTraceElement[]> threadsMap = Thread.getAllStackTraces();
    41     for(Thread thread : threadsMap.keySet())
    42     {
    43       // Interrupt the thread if it's a AbstractDaemon
    44       AbstractDaemon daemon;
    45       if(thread instanceof AbstractDaemon && thread.isAlive())
    46       {
    47         try
    48         {
    49           daemon = (AbstractDaemon)thread;
    50           daemon.shutdownNow();
    51         }
    52         catch(SQLException ex)
    53         {
    54           System.out.println("sonews: " + ex);
    55         }
    56       }
    57     }
    58     
    59     for(Thread thread : threadsMap.keySet())
    60     {
    61       AbstractDaemon daemon;
    62       if(thread instanceof AbstractDaemon && thread.isAlive())
    63       {
    64         daemon = (AbstractDaemon)thread;
    65         System.out.println("sonews: Waiting for " + daemon + " to exit...");
    66         try
    67         {
    68           daemon.join(500);
    69         }
    70         catch(InterruptedException ex)
    71         {
    72           System.out.println(ex.getLocalizedMessage());
    73         }
    74       }
    75     }
    76     
    77     // We have notified all not-sleeping AbstractDaemons of the shutdown;
    78     // all other threads can be simply purged on VM shutdown
    79     
    80     System.out.println("sonews: Clean shutdown.");
    81   }
    82   
    83 }