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/>.
19 package org.sonews.daemon;
21 import java.sql.SQLException;
22 import org.sonews.daemon.storage.Database;
23 import org.sonews.util.Log;
26 * Base class of all sonews threads.
27 * Instances of this class will be automatically registered at the ShutdownHook
28 * to be cleanly exited when the server is forced to exit.
29 * @author Christian Lins
32 public abstract class AbstractDaemon extends Thread
35 /** This variable is write synchronized through setRunning */
36 private boolean isRunning = false;
39 * Protected constructor. Will be called by derived classes.
41 protected AbstractDaemon()
43 setDaemon(true); // VM will exit when all threads are daemons
44 setName(getClass().getSimpleName());
48 * @return true if shutdown() was not yet called.
50 public boolean isRunning()
54 return this.isRunning;
59 * Marks this thread to exit soon. Closes the associated Database connection
61 * @throws java.sql.SQLException
68 this.isRunning = false;
69 Database db = Database.getInstance(false);
78 * Calls shutdownNow() but catches SQLExceptions if occurring.
80 public void shutdown()
86 catch(SQLException ex)
100 this.isRunning = true;