Google Answers Logo
View Question
 
Q: /etc/init.d start, stop, restart control of Java application on Linux ( No Answer,   4 Comments )
Question  
Subject: /etc/init.d start, stop, restart control of Java application on Linux
Category: Computers > Programming
Asked by: msponer-ga
List Price: $30.00
Posted: 19 May 2003 11:02 PDT
Expires: 18 Jun 2003 11:02 PDT
Question ID: 205895
Hi,
I have a custom Java application that needs to be controlled by the
/etc/init.d/ facillity on a Linux server. I want this to work exactly
like Apache or any other service running on the machine, so that as
root I can type:

/etc/init.d/javaserver start
/etc/init.d/javaserver stop
/etc/init.d/javaserver restart

To start, stop, and restart this service, and allow this service to be
automatically stopped and started on shutdown and boot of the machine.

On restart and stop, the application must be alerted in some way so
that it can gracefully release client connections and SQL database
connections. I can code anything in Java, but the answer to this
question must say how my code will know how to enter the "shutdown"
mode and start releasing connections. Maybe Java has some kind of
facillity to get process messages like C programs (SIGHUP).

I am looking for the shell script that will do this, and an
explanation of how to let my Java code know when there is a "stop"
event. I am familiar with RedHat Linux's /etc/init directory structure
to control which runlevels a service is run, and so don't need an
explanation of how to use the script.

I will have multiple copies of this service running on different JVM's
(ie: javaserver_beta, javaserver_development, javaserver_production)
that are bound to different ports or IP's on the same machine, so a
script that uses a simple "ps -ef | grep java | sed | kill ..." will
not work. This needs to know which JVM to kill or send the message to.

Thank you.

Matt.

Clarification of Question by msponer-ga on 19 May 2003 15:24 PDT
I'd like to emphasize that I am looking for a shell script similiar to
those found in /etc/init.d on a RedHat linux system.

The shell script will:
Start, Stop, and Restart a program running inside a JVM

The start command line is:
java -jar /home/server/Server.jar

On Stop and Restart, it'll send a message to the program inside the
JVM (either through some kind of interprocess signal (ie: kill -HUP
{pid}) or creating a "you-die-now" file in /var/run, and then wait x
seconds for the application to terminate on it's own before sending a
kill -9 {pid} ....
Answer  
There is no answer at this time.

Comments  
Subject: Re: /etc/init.d start, stop, restart control of Java application on Linux
From: rhansenne-ga on 19 May 2003 14:02 PDT
 
Hi msponer-ga,

You can provide a method "protected void finalize()" in your main
class and place all cleanup code in there. This method will be called
when the garbage collector frees up the object.

Kind regards,

rhansenne-ga.
Subject: Re: /etc/init.d start, stop, restart control of Java application on Linux
From: msponer-ga on 19 May 2003 15:12 PDT
 
Thank you.

However, I need to be more graceful than a finalize method. My
application needs to enter into a "shutdown mode" where it refuses new
client requests, but continues to service pending client requests,
then terminates when all pending requests are finished. I have the
Java code to support this, I simply have no mechanism to start this
rolling.

If Java does not have some kind of Unix process notification
capabillity, I am happy to resort to having a thread check for the
existence of a "shutdown" file every x seconds.

Whoever makes the init runlevel service script for this question could
then simply just add a "touch /var/run/javaserver.shutdown" type
mechanism to the script, that then sends a kill -9 'cat
/var/run/javaserver.pid' type command if the JVM doesn't terminate
itself after x seconds...
Subject: Re: /etc/init.d start, stop, restart control of Java application on Linux
From: harald-ga on 19 May 2003 16:13 PDT
 
You can use java.lang.Runtime.addShutdownHook(Thread t) to register a
thread that should be started when the virtual machine is shut down
(by pressing Ctrl-C or sending the TERM signal with kill).

Regards,

Harald
Subject: Re: /etc/init.d start, stop, restart control of Java application on Linux
From: glassrazor-ga on 04 Jun 2003 17:05 PDT
 
Check out the Posix for Java project at:

http://www.bmsi.com/java/posix/package.html

It is a JNI library that allows you to get signals from Unix using a
Java event style interface.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy