How can I set server shutdown hook in Helidon SE 4 (nima)

97 views Asked by At

There is no obvious way to do so in version Helidon 4 SE

`public static void main(final String[] args) {
    // load logging configuration
    LogConfig.configureRuntime();

    // initialize global config from default configuration
    Config config = Config.create();
    Config.global(config);

    // Get webserver config from the "server" section of application.yaml
    WebServerConfig.Builder builder = WebServer.builder();
    WebServer server = builder
            .config(config.get("server"))
            .routing(Main::routing)
            .build()
            .start();

    System.out.println("WEB server is up! http://localhost:" + server.port() + "/greet");
}`

I looked how this problem solved in Helidon SE 3:

server.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));

But in Helison SE 4 method server.whenShutdown() doesnt exists.

1

There are 1 answers

1
Tim Quinn On BEST ANSWER

Here's one way.

In Helidon 4, any Service or Handler you write (such as the GreetService in the QuickStart SE example) and register with the HttpRouting can implement the void afterStop() method (as well as beforeStart() if you are so inclined).

The following addition to GreetService in that example would accomplish the same as your example from 3.x:

@Override
    public void afterStop() {
        System.out.println("WEB server is DOWN. Good bye!");
    }

You would probably not want to add such a server shutdown announcement to more than one of your Service or Handler implementations, but of course you can do whatever makes sense for you in each service's or handler's afterStop code.