Skip to Content
0

Restarting Java application from within the application itself

Apr 12 at 04:35 AM

42

avatar image

I have deployed a Java application in SAP Cloud platform, "Java Web Tomcat 8" Runtime to be exact. I have a requirement where I need to provide the capability to allow users to restart the application from within the application itself. Is there a support from Cloud Platform to achieve this.


On a different note, I accidentally found that if I do a System.exit(0) the application restarts itself, but I couldn't find any documentation that guarantees the behavior. Is there something like for certain exit codes, the Cloud Platform restarts the application?

10 |10000 characters needed characters left characters exceeded

Can you explain why the application needs to be restarted by the user in the first place?

0

I basically need to reinitialize the application by reading some config files from document repository.

0
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Ivan Mirisola
Apr 12 at 03:01 PM
0

Hi Kalpa,

Your web application is actually running inside a servlet container which is provided to you by the chosen runtime (i.e.: Java Web Tomcat 8).
Since the container is responsible for your application life-cycle, you should never call System.exit(0). This not an SCP "thing". Calling System.exit(0) will shutdown the entire VM responsible for Tomcat.

Here's my two cents on the restart: SCP is responsible for the application's health. A VM termination may be considered by SCP a critical problem, since no one has ordered the application to stop. Thus it may try to restart the whole container in an attempt to have the application status back to normal. Those are may assumptions.

If you really need to stop your application gracefully, you should connect to the shutdown port and write to it the word SHUTDOWN.

try { 
    Socket socket = new Socket("localhost", 8005); //this is the standard in server.xml on the SDK
    if (socket.isConnected()) { 
        PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); 
        pw.println("SHUTDOWN");//send shut down command - also standard
        pw.close(); 
        socket.close(); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
}

Regards,
Ivan

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Ivan,

Thank you very much for the descriptive answer, I tried that and my observation is this stops the application and SCP starts the application again, automatically. So effectively this is a restart. I wonder SCP restarts the application because it treats this SHUTDOWN as a critical problem in the application and it tries to recover.

0
Kalpa Welivitigoda

Hi Kalpa,

I suspect this happens when you try to shutdown Tomcat from within the same Tomcat instance. This should be accomplished from an external application. If you already have tried that and the behavior is the same, than perhaps this is SCP trying to recover from a misinterpreted shutdown.

In such case, I would recommend opening an incident to SAP's support for further investigation. In my opinion it should be possible to shutdown your application programmatically.

Regards,
Ivan

0