Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Method at the End of Program Execution

Tags:

java

unirest

I am creating a client library for an API endpoint using Unirest to simulate GET and POST requests. Once the program finishes, the following code must be called in order to terminate the current thread.

Unirest.shutdown(); // must be called in order to clear the high CPU consuming thread 

Is there any possible way implicitly call this in my client library at the end of the program's execution?

like image 213
Malik Brahimi Avatar asked Apr 06 '26 12:04

Malik Brahimi


1 Answers

Yes - your best option is likely a Shutdown Hook. It will be called/executed when the JVM is terminating. As an example:

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    public void run() {
        System.out.println("JVM shutting down, closing Unirest");
        Unirest.shutdown();
    }
}));

You should ideally call the addShutdownHook() method as soon as possible, after you have started the Unirest service.

like image 193
Craig Otis Avatar answered Apr 09 '26 02:04

Craig Otis