aspose file tools*
The moose likes Java in General and the fly likes What signal to use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What signal to use" Watch "What signal to use" New topic
Author

What signal to use

sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
Hi,

There are three signals present in my application TERM , INT & ABRT that will shutdown our application gracefully.

I need to use a new signal that will do some special processing, but won't eventually shutdown the application and that the application will continue to work normally after that.
Whats the signal i should use. Any custom signal i can use ?? Also can anyone point me to some examples for this.
I will have a webservice (again in java) that will send this new signal to my already running application (standalone) to get some special processing done.

Thanks in Advance!!
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

Terminate, Interrupt and Abort?
I guess the name of the signal could best to reflect whatever it is this "special processing" entails.
Or maybe URG for urgent processing of some generic thing at the highest priority?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

Are you talking about Unix signals or is this a custom system of signals?

As far as I know, since Unix signals are platform-specific (they don't exist on Windows, for example), the standard Java library does not support them. So I'm curious how your application is handling these, if they are indeed Unix signals.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Signal handling can be done using sun.misc.Signal, though this ties the code to the concrete JRE, of course.

This might help.
sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
Thanks All,

I think I will use SIGUSR1 for handling custom user signals. (sun.misc.Signal) My code will run only on RHEL5 so i am not worried of doing signal handing for Windows.
Is there a cleaner way to send the signal to one java program from another.
Currently I use : Runtime.getRuntime().exec("kill -USR1 6345"); //where 6345 is the process id of the targeted program
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

sksumit kumar wrote:Is there a cleaner way to send the signal to one java program from another.

I don't think so, because these signals are a platform-specific Unix thing.

You're now using an internal Sun class in the JDK, which is not a good idea - see: Why Developers Should Not Write Programs That Call 'sun' Packages. In short, 'sun' packages are not part of the public API of Java and may change or disappear in any new version of Oracle's Java.

Using Unix signals to make Java programs communicate with each other sounds like an unusual design to me. There are better, platform-independent ways to do this, for example via RMI or sockets.
sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
@Jesper

Can't help. There is already signal handling in this legacy code. So was thinking of extending it to handle user signals.

I might be thinking crazy. But is there some hack by which i can send some parameters to the Java program through signals ??
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14352
    
  22

I'm not an expert in Unix signals but as far as I know signals do not have a way to include parameters.
sumit anand kumar
Ranch Hand

Joined: Apr 28, 2010
Posts: 83
OMG, I never could have thought this

Exception in thread "main" java.lang.IllegalArgumentException: Signal already used by VM: SIGUSR1
at sun.misc.Signal.handle(Signal.java:149)

If i start with -Xrs option

Exception in thread "main" java.lang.IllegalArgumentException: Signal already used by VM: SIGTERM
at sun.misc.Signal.handle(Signal.java:149)


Various signals we use are INT,TERM,ABRT,PIPE & HUP

I am using RHEL5 64bit & JDK6

Now m stuck
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

java -X on RHEL5 gives the following output (among others):

-Xrs reduce use of OS signals by Java/VM (see documentation)

Try to find said documentation and learn more about the -Xrs parameter.
 
Consider Paul's rocket mass heater.
 
subject: What signal to use