[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Update to the signal handling patch
> I've checked in a quick patch that allows to specify Classes as sender. So you
> can write something like .newEntry( MyClass.class, "message", ...)
Ok, in that case, here's YAVOTP (Yet Another Version Of The Patch :)).
--
/ Peter Schuller, InfiDyne Technologies HB
PGP userID: 0x5584BD98 or 'Peter Schuller <peter.schuller@infidyne.com>'
Key retrival: Send an E-Mail to getpgpkey@scode.infidyne.com
E-Mail: peter.schuller@infidyne.com Web: http://scode.infidyne.com
Index: Server.java
===================================================================
RCS file: /raid/Repository/ozone/org/ozoneDB/core/Server.java,v
retrieving revision 1.42
diff -r1.42 Server.java
10a11
> import java.lang.reflect.*;
28a30
> private static volatile boolean stoppedBySignal = false;
92a95,99
> /* This is a good place to set up our shutdown hook. Doing it
> * earlier would case a NullPointerException in race conditions.
> * Doing it now is fine in terms of preventing database corruption
> * because request processing has no yet begun. */
> setupShutdownHook();
151c158,163
< env.shutdown();
---
> if (stoppedBySignal == false) {
> /* If we got stopped due to a signal,
> * env.shutdown() has already been invoked
> * by the shutdown hook. */
> env.shutdown();
> }
154c166
< if (env != null) {
---
> if (env != null && stoppedBySignal == false) {
162c174,223
< }
---
> }
>
> private static void setupShutdownHook() {
> try {
> /* We will now attempt to add the shutdown hook
> * using reflection. If we're running in a VM that does
> * not support shutdown hooks (i.e., pre-1.3), it will yield
> * a NoSuchMethodException, which we quietly ignore. */
> Class runtimeClass = Runtime.class;
> Method hookMethod = runtimeClass.getMethod("addShutdownHook", new Class[]{Thread.class});
> Runtime rt = Runtime.getRuntime();
> hookMethod.invoke(rt, new Object[]{createShutdownHook()});
>
> env.logWriter.newEntry(Server.class, "Shutdown hook successfully added. Process kills are now handled.",
> LogWriter.INFO);
> } catch (NoSuchMethodException nsme) {
> /* Pre-1.3 VM. */
> env.logWriter.newEntry(Server.class, "Running on pre-1.3 VM; no shutdown hook added.", LogWriter.INFO);
> } catch (SecurityException se) {
> /* Either a screwy setup, or permission deliberately denied. */
> env.logWriter.newEntry(Server.class, "Shutdown hook not added; permission denied.", LogWriter.WARN);
> } catch (InvocationTargetException ite) {
> /* Runtime.addShutdownHook() failed */
> Throwable te = ite.getTargetException();
> env.logWriter.newEntry(Server.class, "WARNING: Shutdown hook addition failed: "
> + te.getClass().getName() + ": " + te.getMessage(),
> LogWriter.ERROR);
> } catch (IllegalAccessException iae) {
> /* The VM is FUBAR. */
> env.logWriter.newEntry(Server.class,"Runtime.addShutdownHook() not public? VM bug?", LogWriter.ERROR);
> }
> }
>
> /**
> * Constructs and returns a Thread to be used as a shutdown hook. It
> * invokes Env.shutdown().
> */
> private static Runnable createShutdownHook() {
> return new Thread() {
> public void run() {
> if (env.shuttingdown == false) {
> /* Synchronization is not necessary
> for the following two variables. */
> stoppedBySignal = true;
> stop = true;
> env.shutdown();
> }
> }
> };
> }