Skip to Content

Restarting Java application from within the application itself

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?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Apr 12 at 03:01 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Ivan Mirisola 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