44

I started research to find an alternative to the sun.misc.Signal class, because it could be unsupported in upcoming JDKs (we're currently working on 1.6). When I build the project I get:

warning: sun.misc.SignalHandler is Sun proprietary API and may be removed in a future release

I came across multiple solutions but they don't fit my project e.g. in this question.

This is unacceptable in my situation because:

  • Signals are used not only for killing application
  • The application is huge - every conceptual change of communication between modules/JVMs could take years to implement

Thus, the desirable solution is to find something like a new Oracle version of this class or something which works in the same way. Does such a solution exist?

Community
  • 1
  • 1
plancys
  • 3,833
  • 2
  • 19
  • 26
  • Do you know that it is not supporting Java 8 or 9? Given Java rarely ever removes anything, is this a real concern? – Peter Lawrey Oct 31 '13 at 15:54
  • 1
    If there is any possibility to not supporting this I can't take a risk. Like I said before - it is huge application and every possible fail connected to this problem could be disaster for whole project :) – plancys Oct 31 '13 at 15:58
  • 2
    Your concern is that someone will change the JVM to Java 9 in production without any thought, testing or warning and then blame you if it doesn't work? – Peter Lawrey Oct 31 '13 at 16:01
  • Maybe you are right but my concern is to solve problem, not to 'hack' them or act like there is no problem. – plancys Nov 05 '13 at 09:51
  • I am not clear what the problem is if you have sensible release processes. If you don't this is the true problem worth solving. – Peter Lawrey Nov 05 '13 at 21:32
  • possible duplicate of [Capture SIGINT in Java](http://stackoverflow.com/questions/2541475/capture-sigint-in-java) – Ciro Santilli OurBigBook.com Apr 09 '15 at 16:38
  • If you don't mind changing how you launch the VM, using JNI and only targeting **Hotspot** VMs then it's possible to use [Signal Chaining](https://docs.oracle.com/javase/10/troubleshoot/handle-signals-and-exceptions.htm#GUID-CB49A2A7-2A9F-4C18-948F-6D4A96FF688D). – earcam Feb 07 '19 at 12:38

3 Answers3

29

As you will find repeated ad infinitum when learning about signal handling in Java, you are encouraged to avoid writing Java code that depends directly on signals. The general best practice is to allow the JVM to exit normally on ctrl+c and other signals by registering a shutdown hook instead. Handling signals directly makes your Java program OS-dependent.

But sometimes that's OK, and you really, really do want to handle signals yourself.

Even though it's not exposed as part of the official JDK API, some part of the JVM has to handle signals (in order to trigger the shutdown hooks and exit), and that component is sun.misc.Signal. Although this is an implementation detail and therefore could change, it is unlikely in practice. If it were to change, it would need to be replaced with an equivalent mechanism, and likely documented in the Java Platform Troubleshooting Guide.

The sibling class sun.misc.Unsafe is widely used and is similarly undocumented. There is active work towards trying to remove this class because it's "become a 'dumping ground' for non-standard, yet necessary, methods", but the current proposal, while limiting some non-standard APIs, keeps both sun.misc.Unsafe and sun.misc.Signal available by default. An earlier plan to actually prevent access to these classes would still have included a command-line flag to allow access to them for backwards-compatibility.

In short while you cannot rely on sun.misc.Signal and must plan for the eventuality that this behavior changes, it is highly unlikely that this behavior will change before JDK 10, and if it does either a new, better mechanism will probably be introduced or there will be a reasonable way to re-enable it if needed.

However it would be wise to compartmentalize the code that relies on any sun.misc classes to as small a scope as possible - create a wrapping API for signal handling so that callers don't need to interact directly with sun.misc. That way if the API changes you only need to change the implementation of your wrapper, rather than all your signal handling code.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 2
    @specializt care to share a citation or reference? It's still in the head revision of [JDK 8](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/sun/misc/Signal.java) and JEP 260 still indicates `sun.misc.{Signal,SignalHandler}` will "*remain accessible in JDK 9*". Even if the plan for JDK 9 had changed it would be very strange at this point for them to remove it from the already- released JDK 8. – dimo414 Mar 24 '16 at 13:50
  • 7
    Update - `sun.misc.Signal` is alive and well in JDK 11, and I can't find anything indicating any current plans for removal. – nerdherd Jan 29 '19 at 17:25
  • 3
    Another update: `sun.misc.Signal` is still available in JDK 16 – josemmo May 09 '21 at 10:37
4

Such a solution does exist. JNR ( https://github.com/jnr/ ) is an efficient POSIX layer; comparison with JNI+JNA I read here: https://www.slideshare.net/skrb/jnr-java-native-runtime .

This is my code to handle SIGHUP in Java using JNR:

SignalHandler handler = new SignalHandler()
    {
    @Override public void handle(int signal)  { reloadThings(); }
    };
final POSIX posix = POSIXFactory.getPOSIX(new DefaultPOSIXHandler(), true);
posix.signal(Signal.SIGHUP, handler);

The SignalHandler code is what will be done, the posix Handle is the JNR entry point AFAIK, and then the last line registers the signal.

JNR seems to be under active development (as of 2021), and isn't too large. Looks good to me - I just started using it because of this. PS: I just checked this to work with SIGINT = Ctrl+C. It does.

foo
  • 1,968
  • 1
  • 23
  • 35
  • 1
    Very nice, just add one dependency `com.github.jnr:jnr-posix` and copy your snippet and it works. Just for CTRL+C/SIGINT, I had to set `useNativePOSIX` of `POSIXFactory#getPOSIX` to `false`, at least on Linux, otherwise the signal was trapped but the handler not executed. – Torsten Römer Jul 08 '22 at 14:11
-13

Your best bet is to transition away from signals since they are not well supported.

Alternatives for IPC:

  • Sockets (including web services, JMS, etc)
  • File locking
  • Memory mapped file
Jon B
  • 471
  • 5
  • 8
  • 7
    None of those alternatives can be used to e.g. handle a SIGTERM (Ctrl-C) in a console application –  Nov 17 '15 at 12:59