Skip to content
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

Add details about how to save state across live reloads #44287

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
29 changes: 29 additions & 0 deletions docs/src/main/asciidoc/writing-extensions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1970,6 +1970,35 @@ information about this start, in particular:
It also provides a global context map you can use to store information between restarts, without needing to resort to
static fields.

Here is an example of a build step that persists context across live reloads:

[source,java]
----
@BuildStep(onlyIf = {IsDevelopment.class})
public void keyPairDevService(LiveReloadBuildItem liveReloadBuildItem, BuildProducer<KeyPairBuildItem> keyPairs) {

KeyPairContext ctx = liveReloadBuildItem.getContextObject(KeyPairContext.class); // <1>
if (ctx == null && !liveReloadBuildItem.isLiveReload()) { // <2>
KeyPair keyPair = generateKeyPair(2048);
Map<String, String> properties = generateDevServiceProperties(keyPair);
liveReloadBuildItem.setContextObject( // <3>
KeyPairContext.class, new KeyPairContext(properties));
keyPairs.produce(new KeyPairBuildItem(properties));
}

if (ctx != null) {
Map<String, String> properties = ctx.getProperties();
keyPairs.produce(new KeyPairBuildItem(properties));
}
}

static record KeyPairContext(Map<String, String> properties) {}
----

<1> You can retrieve the context from `LiveReloadBuildItem`. This call returns `null` if there is no context for the specified type; otherwise, it returns the stored instance from a previous live reload execution.
<2> You can check if this is the first execution (not a live reload).
<3> The `LiveReloadBuildItem#setContext` method allows you to set a context across live reloads.

==== Triggering Live Reload

Live reload is generally triggered by an HTTP request, however not all applications are HTTP applications and some extensions
Expand Down
Loading