cmjanicki
Posts: 6
Joined: Mon May 18, 2009 1:37 am

Stopping a service should handle System.exit(0) gracefully

I understand that stopping a service calls stop() in our main class. As part of our normal (Unix) shutdown we shutdown a few critical threads and close resources, then System.exit(0) if everything went OK (System.exit(-1) if something went wrong). But this procedure seems to cause Windows to complain that the application closed unexpectedly when an admin tries to stop our Java service application. I'm assuming that this is because there were still some threads running, meaning that the JVM didn't shutdown by itself.

If that's true, then I think this a bug, or at least a feature request... It seems to me that any exit code of '0' should be considered graceful/OK. Otherwise our very threaded service is going to require a lot of new code to track all our threads and gracefully stop them... overkill I think, given the nature of most of our threads. Or maybe setting all of those to "daemon" status would work with less trouble?

Any thoughts?

Thanks,
Chris
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: Stopping a service should handle System.exit(0) gracefully

Hello,

Please note that calling System.exit() forces your app to terminate without returning control to our .exe wrapper. Our wrapper executes some cleanup tasks to avoid errors like the one you are encountering. You should also do a proper cleanup (including stopping any threads that are running) before exiting.
If that's true, then I think this a bug, or at least a feature request... It seems to me that any exit code of '0' should be considered graceful/OK. Otherwise our very threaded service is going to require a lot of new code to track all our threads and gracefully stop them... overkill I think, given the nature of most of our threads. Or maybe setting all of those to "daemon" status would work with less trouble?
This is not a bug. We will take into consideration issuing some improvements to also support System.exit().

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/

Return to “Common Problems”