Custom Search

Friday, July 17, 2009

Handling signals in Java

Signals by definition are specific to the underlying implementation ( read, operating system) .

Java, being a platform independent language , it is often discouraged to write platform specific stuff except for the rarest cases and when there is a clear business justification for the same.

Java does have an undocumented API that talks about signals where we can override the default signal handler for a given signal.  Scenarios where this might be useful and necessary are when we try to release resources ( connections etc.).

Note: When we override default signal handlers, it is important to delegate the behavior to the default handler implementation (Hint: Save the old handler when overriding the same) after we finish the current implementation.


package experiment;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import sun.misc.Signal;
import sun.misc.SignalHandler;

public class CustomSignalHandler implements SignalHandler
{

private static final Logger LOGGER = Logger.getLogger(CustomSignalHandler.class.getName());

private static Map<Signal, SignalHandler> handlers = new HashMap<Signal, SignalHandler>();


@Override
public void handle(Signal signal)
{
LOGGER.info("received " + signal);

// Delegate to the existing handler after handling necessary clean-up.
handlers.get(signal).handle(signal);
}


/**
* Important: This API is not portable but heavily platform dependent as the signal name depends
* on the underying operating system.

*
* @param signalName
* @param signalHandler
*/
public static void delegateHandler(final String signalName,
final SignalHandler signalHandler)
{
try {
Signal signal = new Signal(signalName);
SignalHandler oldhandler = Signal.handle(signal, signalHandler);
} finally {
handlers.put(signal, oldhandler);
}
}


public static void main(String[] args)
{
final int LONG_TIME = 50000;

SignalHandler example = new CustomSignalHandler();
delegateHandler("TERM", example);
delegateHandler("INT", example);
delegateHandler("ABRT", example);

try
{
Thread.sleep(LONG_TIME);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}


No comments: