Skip to content

Commit

Permalink
Add detail about how to set/get context across live reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Nov 4, 2024
1 parent 7ab2138 commit 7bdd9da
Showing 1 changed file with 29 additions and 0 deletions.
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<DevServiceResultBuildItem> devServices) {
KeyPairContext ctx = liveReloadBuildItem.getContextObject(KeyPairContext.class); // <1>
if (ctx == null && !liveReloadBuildItem.isLiveReload()) { // <2>
KeyPair keyPair = KeyUtils.generateKeyPair(KEY_SIZE);
Map<String, String> properties = generateDevServiceProperties(keyPair);
liveReloadBuildItem.setContext( // <3>
KeyPairContext.class, new KeyPairContext(properties));
devServices.produce(smallryeJwtDevServiceWith(properties));
}
if (ctx != null) {
Map<String, String> properties = ctx.getProperties();
devServices.produce(smallryeJwtDevServiceWith(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

0 comments on commit 7bdd9da

Please sign in to comment.