signals

M

Martin A

Is it possible for an external program to send a signal (such as SIGINT,
SIGUSR1, SIGUSR2) to a java application, and have this java application
handle it?
 
C

Chris Smith

Martin A said:
Is it possible for an external program to send a signal (such as SIGINT,
SIGUSR1, SIGUSR2) to a java application, and have this java application
handle it?

No, not without JNI.

For one thing, I don't know how you'd expect to map UNIX signal names to
other operating systems like Windows or pre-OSX Macintosh. What you can
do is add shutdown hooks, which will execute before the application
ends... that amounts to running code in response to delivery of some
signals on UNIX, but it's limited in terms of what signals work (SIGINT
and SIGTERM mainly), and you can't stop the VM from exiting. Other
forms of generic IPC with Java need to be done via IP sockets to be
portable. If you don't care about portability but still want to avoid
JNI, then opening a pre-existing named UNIX socket would also work, but
the Java application could not create that socket.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
I

iksrazal

Sure. One caveat, uses sun.misc libs. I've used this code, however,
since 1.2.1 without problems.

The instances at the bottom do RMI which is otherwise not shown. Of
course, -9 can't be caught.

import java.io.*;
import sun.misc.*;//for signal catching
import com.protomatter.syslog.*;

public class MDDBDriver
{
public static void main(String[] args)
{
SignalHandler handler = new SignalHandler()
{
public void handle(Signal sig)
{
Syslog.error(this, "SNIFF_SUPPRESSED Explicit kill sent to
server process, sig is --> "+sig+", aborting...");
System.exit(0);
}
};
Signal.handle(new Signal("TERM") , handler);//string appened to SIG,
meaning SIGTERM

CRMLogRegister instance1= new CRMLogRegister();
CRMDBPool instance2= new CRMDBPool();
MDDBRegister instance3= new MDDBRegister();
}

}

HTH
iksrazal
http://www.braziloutsource.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top