AnOldSoul
AnOldSoul

Reputation: 4207

Stop method not getting called when terminating the windows service

I have converted a java application into a windows Service using procrun. When I stop the service using Windows Services program it gets hung and doesn't respond. When launch it using the debug mode and then when I terminate it using ctrl+c, I can't see the stop method being called. I have given a print statement which is not getting printed. Below is my code. Please advice. There is nothing in the Windows Even logs related to this service.

public static void main(String[] args) {
    if ("start".equals(args[0])) {
        start(args);
    } else if ("stop".equals(args[0])) {
        stop(args);
    }

}

public static void start(String[] args) {

    KubeSdkMain sdkMain = new KubeSdkMain();
    sdkMain.startSDK();

}

public static void stop(String[] args) {

    System.out.println("stop");
    stop = true;

}

Below is the content of the bat file I run for installing the service.

cd "G:\Projects"
set PR_PATH="G:\Projects"
SET PR_SERVICE_NAME=TestService
SET PR_JAR=KKSDK.jar
SET START_CLASS=com.example.test
SET START_METHOD=start
SET STOP_CLASS=com.example.test
SET STOP_METHOD=stop
rem ; separated values
SET STOP_PARAMS=0
rem ; separated values
SET JVM_OPTIONS=-Dapp.home=%PR_PATH%
prunsrv.exe //IS//%PR_SERVICE_NAME% --Install="%PR_PATH%\prunsrv.exe" --Jvm=auto --Startup=auto --StartMode=jvm --StartClass=%START_CLASS% --StartMethod=%START_METHOD% --StopMode=jvm --StopClass=%STOP_CLASS% --StopMethod=%STOP_METHOD% ++StopParams=%STOP_PARAMS% --Classpath="%PR_PATH%\%PR_JAR%" --DisplayName="%PR_SERVICE_NAME%" ++JvmOptions=%JVM_OPTIONS%

pause

Upvotes: 1

Views: 1103

Answers (1)

Anand Kadhi
Anand Kadhi

Reputation: 1888

I think you should use a shutdown hook so that when you press ctrl+c shutdown hook gets called.
This link might help you
ShutDownHook

Upvotes: 1

Related Questions