How to test Java signal handler in Eclipse 3.2
The following code implements the handle method of Java SignalHandler
fucntionality (please see below).
I can test code within a linux command line environment (using kill -
pid/%1), but would like to know how to do the same in Eclipse 3.2. I
would like to do the following:
Run code.
Switch to an eclipse view - something which will capture my CTRL cdoe
input.
Test my signal Handler code by proessing CTRL keys :)
See the key-press being trapped by the signalHandler code.
Can someone please tell me how to do this within Eclipse 3.2. If I
need to wrap an eclipse handler round my code, then so be it :o) Any
help would be appreciated.
thanks
Miles.
Code
====
public class **** implements sun.misc.SignalHandler {
private boolean Running = true;
private static Logger myLogger;
public SignalHandler() {
try
{
this.setupHandler();
}
catch (SignalHandlerException e)
{
myLogger.info ( e.toString() );
}
}
private void setupHandler() throws SignalHandlerException {
this.handleSignal("INT");
this.handleSignal("TERM");
//this.handleSignal("HUP");
}
public void handleSignal(final String signalName) throws
SignalHandlerException {
try {
sun.misc.Signal.handle(new sun.misc.Signal(signalName), this);
} catch (IllegalArgumentException x) {
// Most likely this is a signal that's not supported on this
// platform or with the JVM as it is currently configured
throw new SignalHandlerException ( "SignalHandlerException" +
x.getMessage() );
}
}
/**
* Called by Sun Microsystems' signal trapping routines in the JVM.
* @param signal The {@link sun.misc.Signal} that we received
**/
public synchronized void handle(sun.misc.Signal signal) {
// we are only interested in catching the "TERM" signal
if ( signal.getName().equals("TERM") ) {
myLogger.info(" TERM received " + signal.getName() );
myLogger.info("threadName" + Thread.currentThread().getName()
);
System.exit(-1);
} else if ( signal.getName().equals("INT") ||
signal.getName().equals("HUP") ) {
setRunningStatus( false );
notifyAll(); //TODO - clarify if notify OR notifyall
}
myLogger.info( signal.getName() + " " + signal.getNumber() );
myLogger.info( getRunningStatus()+"" );
}
public void setRunningStatus( boolean Status ) {
this.Running = Status;
}
public boolean getRunningStatus() {
return ( Running );
}
/**
* General "clean up" method which is called when we receive a TERM
signal
* This will likely be superseeded by specific cleanup code
*
**/
public boolean cleanUp() {
// TODO - we need to decide what we are going to do one we trap a
TERMinate signal.
// We need to complete all our threads nicely.
myLogger.info("Cleaning up!");
return ( true );
}
public synchronized void myWait() {
try
{
wait();
}
catch (InterruptedException e)
{
myLogger.info( "myWait WOKEN UP" + e );
}
}
/**
*
* The main method - entry into the code
* retain for testing
*
**/
public static void main(final String[] args) {
SignalHandler mySignalHandler = new SignalHandler();
while ( mySignalHandler.getRunningStatus() ) {
myLogger.info("waiting");
mySignalHandler.myWait();
myLogger.info("woken up");
}
}