Say, I have a Java application running, and I have provided RESTful web services call (via Java Jersey). First, am I right that every invocation of a HTTP RESTful request (or servlet request) runs on a different thread from the main thread? And if so, if I provide a RESTful call to kill the app, how can it reach the main thread (the one started by the main() method) to kill it and subsequently stop the program?
Note: the reason I’m asking this is I need to do a graceful shutdown of the main thread (and the program).
Your restful service should treat a shutdown request the same as any other request.
For example, if your app was an online store, you might have calls like
processOrderorgetInventory. These calls have access to many things in your app, right? Well, just make a new call calledshutdown. This call will shut the gates to future calls, wait for existing calls to finish, save in-memory information to the DB, and other safe shutdown procedures.It’s really just another call. I wouldn’t worry so much about “stopping the main method” from a call – think in terms of logical behavior, not technical behavior.
It would appear for Jersey, you’re looking for
client.destroy();.