Shutdown via REST PUT request

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Shutdown via REST PUT request

Sean Johnston
I've a standalone application that I'm trying to implement a shutdown for. Initially I had this as a very simple Socket listen. However, wanting to put a more proper API in place I'm looking at REST and RESTlet is looking good.

I've got it mostly working. That is, I have something that handles authentication and responds to a PUT request on http://host:port/shutdown. However, whilst this shuts down the various bits of my application, it doesn't shut down the REST server itself so the JVM keeps on running.

I tried calling #stop on the Application but that doesn't seem to do anything. I can get it to terminate by calling stop on the top level Component, but that doesn't exit gracefully as the originating PUT handler throws exceptions.

Is there a way to gracefully shut everything down in response to a PUT request?

--
Sean - Its a conical sort of effort
Reply | Threaded
Open this post in threaded view
|

Re: Shutdown via REST PUT request

Tim Peierls
You can call Component.stop() asynchronously with a delay, so that the PUT handler has a chance to complete. (Although I think POST captures the intent better: You're POSTing a request to shut down the component.)

It could be as simple as this:

@Post 
public String postShutdown(final String shutdownMessage) {
    Runnable task = new Runnable() {
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException ex) {
                log.warn("Swallowing interruption in order to shut down");
            } finally {
                log.info("Shutting down with message: " + shutdownMessage);
                theComponent.stop(); // and whatever other cleanup is needed
            }
        }
    };
    new Thread(task).start();
    return "Shutdown request posted; will shut down in 5 seconds";
}

--tim


On Wed, Apr 23, 2014 at 1:15 PM, Sean Johnston <[hidden email]> wrote:
I've a standalone application that I'm trying to implement a shutdown for. Initially I had this as a very simple Socket listen. However, wanting to put a more proper API in place I'm looking at REST and RESTlet is looking good.

I've got it mostly working. That is, I have something that handles authentication and responds to a PUT request on http://host:port/shutdown. However, whilst this shuts down the various bits of my application, it doesn't shut down the REST server itself so the JVM keeps on running.

I tried calling #stop on the Application but that doesn't seem to do anything. I can get it to terminate by calling stop on the top level Component, but that doesn't exit gracefully as the originating PUT handler throws exceptions.

Is there a way to gracefully shut everything down in response to a PUT request?

--
Sean - Its a conical sort of effort

Reply | Threaded
Open this post in threaded view
|

Re: Shutdown via REST PUT request

Fabian Mandelbaum
I had to do System.exit(0) too to completely finish the thing, besides shutting down Restlet components and possibly other services, else Restlet itself is shut down, but the JVM that fired the app is not.

I'm using Apache's Commons Daemon to 'daemonize' the application, and in the latest versions I've removed this feature, and use the script's 'stop' to stop the service cleanly.

Indeed, like Tim said, a POST is better than a PUT.



On Wed, Apr 23, 2014 at 3:25 PM, Tim Peierls <[hidden email]> wrote:
You can call Component.stop() asynchronously with a delay, so that the PUT handler has a chance to complete. (Although I think POST captures the intent better: You're POSTing a request to shut down the component.)

It could be as simple as this:

@Post 
public String postShutdown(final String shutdownMessage) {
    Runnable task = new Runnable() {
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException ex) {
                log.warn("Swallowing interruption in order to shut down");
            } finally {
                log.info("Shutting down with message: " + shutdownMessage);
                theComponent.stop(); // and whatever other cleanup is needed
            }
        }
    };
    new Thread(task).start();
    return "Shutdown request posted; will shut down in 5 seconds";
}

--tim


On Wed, Apr 23, 2014 at 1:15 PM, Sean Johnston <[hidden email]> wrote:
I've a standalone application that I'm trying to implement a shutdown for. Initially I had this as a very simple Socket listen. However, wanting to put a more proper API in place I'm looking at REST and RESTlet is looking good.

I've got it mostly working. That is, I have something that handles authentication and responds to a PUT request on http://host:port/shutdown. However, whilst this shuts down the various bits of my application, it doesn't shut down the REST server itself so the JVM keeps on running.

I tried calling #stop on the Application but that doesn't seem to do anything. I can get it to terminate by calling stop on the top level Component, but that doesn't exit gracefully as the originating PUT handler throws exceptions.

Is there a way to gracefully shut everything down in response to a PUT request?

--
Sean - Its a conical sort of effort




--
Fabián Mandelbaum
IS Engineer