Skip to content

4.x: Document ServerLifecycle beforeStart and afterStop #9629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion docs/src/main/asciidoc/se/guides/upgrade_4x.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////

Copyright (c) 2023, 2024 Oracle and/or its affiliates.
Copyright (c) 2023, 2025 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,6 +72,25 @@ include::{sourcedir}/se/guides/Upgrade4xSnippets.java[tag=snippet_2, indent=0]

Just create it, configure it, and wait for it to start. If any exceptions happen, they are handled the traditional way using available language constructions.

== Server Lifecycle

In Helidon 3.x you provided code to run after WebServer startup and after WebServer shutdown using asynchronous constructs:

[source,java]
.Helidon 3.x server lifecycle
----
include::{sourcedir}/se/guides/Upgrade4xSnippets.java[tag=snippet_9, indent=0]
----

In Helidon 4 your `HttpService` can interpose on the server lifecycle by overriding the `beforeStart` and `afterStop` methods:
[source,java]
.Helidon 4.x server lifecycle
----
include::{sourcedir}/se/guides/Upgrade4xSnippets.java[tag=snippet_10, indent=0]
----

No special API is needed for after server starup since the server starts sycnhronously as described in the previous section.

== Server Features and Media Support Discovery

In previous versions of Helidon you had to explicitly register WebServer features (`register(MetricsSupport.create())`) and
Expand Down
18 changes: 14 additions & 4 deletions docs/src/main/asciidoc/se/webserver.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////

Copyright (c) 2018, 2024 Oracle and/or its affiliates.
Copyright (c) 2018, 2025 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,9 +34,9 @@ include::{rootdir}/includes/se.adoc[]
** <<Configuring the WebServer in a Configuration File, Configuring the WebServer in a Configuration File>>
** <<Configuring TLS, Configuring TLS>>
** <<Configuration Options, Configuration Options>>
*** <<Routing, Routing>>
*** <<Request Handling, Request Handling>>
*** <<Error Handling, Error Handling>>
- <<Routing, Routing>>
** <<Request Handling, Request Handling>>
** <<Error Handling, Error Handling>>
- <<Server Features, Server Features>>
** <<Access Log, Access Log>>
** <<Context, Context>>
Expand Down Expand Up @@ -291,6 +291,16 @@ include::{sourcedir}/se/WebServerSnippets.java[tag=snippet_7, indent=0]
In this example, the `GET` handler matches requests to `/hello/subpath`.

[[anchor-http-feature]]

=== Server Lifecycle

In Helidon 4 your `HttpService` can interpose on the server lifecycle by overriding the `beforeStart` and `afterStop` methods:
[source,java]
.Helidon 4.x server lifecycle
----
include::{sourcedir}/se/guides/Upgrade4xSnippets.java[tag=snippet_10, indent=0]
----

=== Using `HttpFeature`

By implementing the `io.helidon.webserver.http.HttpFeature` interface, you can organize multiple routes and/or filters into
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
* Copyright (c) 2024, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -170,4 +170,46 @@ void snippet_8() {
Config config = Config.global();
// end::snippet_8[]
}

/* Helidon 3 code
static Single<WebServer> startServer() {
Config config = Config.create();

WebServer server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.addMediaSupport(JsonpSupport.create())
.build();

// tag::snippet_9[]
Single<WebServer> webserver = server.start();

webserver.thenAccept(ws -> {
System.out.println("WEB server is up! http://localhost:" + ws.port() + "/greet");
ws.whenShutdown().thenRun(() -> System.out.println("Helidon WebServer has stopped"));
})
.exceptionallyAccept(t -> {
System.err.println("Startup failed: " + t.getMessage());
t.printStackTrace(System.err);
});
// end::snippet_9[]
return webserver;
}
*/

// tag::snippet_10[]
static class MyService implements HttpService {
@Override
public void beforeStart() {
System.out.println("MyService: Helidon WebServer is starting!");
}

@Override
public void afterStop() {
System.out.println("MyService: Helidon WebServer has stopped.");
}
// end::snippet_10[]
@Override
public void routing(HttpRules rules) {
}
}
}