3 Şubat 2023 Cuma

sun.misc.SignalHandler Arayüzü

Giriş
Şu satırı dahil ederiz
import sun.misc.Signal;
import sun.misc.SignalHandler;
SIGTERM sinyalini yakalamamızı sağlar. Açıklaması şöyle
The downside of using SignalHandler is that some OS may not be supported. For example, SignalHandler would not be called in Windows, since Windows does not have a SIGTERM signal. As documented in the Oracle documentation, using “sun” packages is not recommended or should be avoided since the package does not guarantee to work with all Java-compatible platforms (like the Windows example above), and could be removed in future versions of the platform. Therefore, you should not rely on this solution and keep in mind that you need to continue to search for a better way to handle this issue properly.
Örnek
Elimizde şöyle bir kod olsun
class SignalHandlerImpl implements SignalHandler {

  @Override
  public void handle(Signal signal) {
    try {
    ...
    } catch (Exception e) {
      System.exit(128 + signal.getNumber());
} }
}
Şöyle yaparız
void addSignalHandler() {
  SignalHandler signalHandler = new SignalHandlerImpl();
  Signal.handle(new Signal("TERM"), signalHandler);
  Signal.handle(new Signal("INT"), signalHandler);
}



Hiç yorum yok:

Yorum Gönder