Fix for #549.
3 * see AUTHORS for the list of contributors
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.
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.
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/>.
21 import java.sql.SQLException;
23 import org.sonews.daemon.AbstractDaemon;
26 * Will force all other threads to shutdown cleanly.
27 * @author Christian Lins
30 class ShutdownHook extends Thread
34 * Called when the JVM exits.
39 System.out.println("sonews: Trying to shutdown all threads...");
41 Map<Thread, StackTraceElement[]> threadsMap = Thread.getAllStackTraces();
42 for(Thread thread : threadsMap.keySet())
44 // Interrupt the thread if it's a AbstractDaemon
45 AbstractDaemon daemon;
46 if(thread instanceof AbstractDaemon && thread.isAlive())
50 daemon = (AbstractDaemon)thread;
53 catch(SQLException ex)
55 System.out.println("sonews: " + ex);
60 for(Thread thread : threadsMap.keySet())
62 AbstractDaemon daemon;
63 if(thread instanceof AbstractDaemon && thread.isAlive())
65 daemon = (AbstractDaemon)thread;
66 System.out.println("sonews: Waiting for " + daemon + " to exit...");
71 catch(InterruptedException ex)
73 System.out.println(ex.getLocalizedMessage());
78 // We have notified all not-sleeping AbstractDaemons of the shutdown;
79 // all other threads can be simply purged on VM shutdown
81 System.out.println("sonews: Clean shutdown.");