Skip to content

Instantly share code, notes, and snippets.

@mbreese
Created February 13, 2024 00:25
Show Gist options
  • Save mbreese/90a166e4f1f86265a23e7733fc8705a8 to your computer and use it in GitHub Desktop.
Save mbreese/90a166e4f1f86265a23e7733fc8705a8 to your computer and use it in GitHub Desktop.
Capture signals from Java
package io.compgen.ngsutils.support;
import sun.misc.Signal;
import sun.misc.SignalHandler;
abstract public class CustomSignalHandler implements SignalHandler {
private SignalHandler old = null;
private void setOld(SignalHandler old) {
this.old = old;
}
public static void install(String sig, CustomSignalHandler handler) {
Signal SIG = new Signal(sig);
SignalHandler old = Signal.handle(SIG, handler);
handler.setOld(old);
}
@Override
public void handle(Signal sig) {
run();
if (old != null) {
old.handle(sig);
}
}
abstract public void run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment