|
From: Leif M. <le...@ta...> - 2003-08-28 10:14:34
|
Andy,
One idea is to have your code register a Shutdown Hook using the
method in
the java.lang.Runtime class. In case you are familiar with them, a
shutdown hook
is a thread which is run before the JVM is shutdown.
Anyway, if you make your thread set a flag that it is ready to
shutdown and then
wait until your other thread sets another flag that it is ok to quit,
then the shutdown
hook will not allow the JVM to quit until your thread is done working
with its files
and has gone into a wait state. If you do this, be sure to use "try
{...} finally" blocks
to make sure that both of the flags are always set at the relevant
times to avoid
deadlocks at shutdown.
The Wrapper will still kill the JVM it does not exit cleanly after
30 seconds of
being requested to do so. You can extend this using the
wrapper.shutdown.timeout
property.
http://wrapper.tanukisoftware.org/doc/english/prop-shutdown-timeout.html
Something like this:
---
// Class variables
private static Object m_semaphore = new Object();
private static boolean m_workerBusy = false;
private static boolean m_quitting = false;
private static Thread m_workerThread;
---
// This goes in your initialization code.
m_workerThread = new Thread( new Runnable() {
try {
while( !m_quitting ) {
// This wait will be interrupted by the shutdown hook to
stop the wait.
try {
Thread.sleep( 300000 );
} catch ( InterruptedException e ) {
return;
}
m_workerBusy = true;
// Do your work here...
synchronized( m_semaphore )
{
m_workerBusy = false;
m_semaphore.notifyAll();
}
}
} finally {
// Worker thread exited.
m_workerThread = null;
}
}, "worker" );
// Important to set this as a daemon or the JVM will think it needs
to keep running.
m_workerThread.setDaemon( true );
m_workerThread.start();
Thread shutdownHook = new Thread( new Runnable() {
public void run() {
m_quitting = true;
// Store the workerThread in a local variable to we never get
a NPE due to bad timing.
Thread workerThread = m_workerThread;
if ( workerThread != null ) {
workerThread.interrupt();
synchronized( m_semaphore ) {
while ( m_workerBusy ) {
m_semaphore.wait();
}
}
}
}
}, "shutdown-hook" );
Runtime.getRuntime().addShutdownHook( shutdownHook );
I didn't compile that, so hopefully it works. Please post back your
results for
future users.
Cheers,
Leif
And...@DM... wrote:
>Hi.
>
>Is it possible to delay the shutdown of the service?
>
>This is what I'm currently doing. I launch a service in 2000 that looks
>for new files in a directory every 5 minutes or so. The service class
>launches a java class which starts a java thread. The thread does the work
>of looking for files then sleeping etc.
>
>When I stop the service from the command line I want to be able to let the
>thread finish what it is doing before killing the service. Any ideas to how
>I can do this would be most appreciated.
>
>Thanks,
>Andy.
>
>
>
>
>-------------------------------------------------------
>This sf.net email is sponsored by:ThinkGeek
>Welcome to geek heaven.
>http://thinkgeek.com/sf
>_______________________________________________
>Wrapper-user mailing list
>Wra...@li...
>https://lists.sourceforge.net/lists/listinfo/wrapper-user
>
>
>
|