Skip to content

Commit

Permalink
Migrate extension Config to @ConfigMapping
Browse files Browse the repository at this point in the history
Moving the config interface to runtime

If the config is runtime, the call to the config should be done in the recorder
  • Loading branch information
gastaldi committed Sep 18, 2024
1 parent dbee2bb commit 21aa7a0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,44 +135,47 @@ For this, what do we need:
The _runtime_ part of an extension contains only the classes and resources required at runtime.
For the version extension, it would be a single class that prints the version.

First, let's create the configuration class that will allow users to dynamically configure whether the version should be printed:
[example, role="cta"]
--

In the runtime module, create the `src/main/java` directory, and, in this directory, the `io.quarkus.workshop.superheroes.version.runtime.VersionRecorder` class with the following content:
Under the `runtime` module, create the `src/main/java` directory, and, in this directory, create a new interface named `io.quarkus.workshop.superheroes.version.runtime.VersionConfig` with the following content:

[source,java]
----
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionRecorder.java[]
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java[]
----
--
The `@ConfigMapping` annotation declares `VersionConfig` as a configuration interface which configuration properties are prefixed with `quarkus.version` prefix. It is also used in CDI aware environments to scan and register Config Mappings.

Simple right?
But how does it work?
Look at the `@Recorder` annotation.
It indicates that this class is a _recorder_ used to record actions executed later at runtime.
Indeed, these actions are replayed at runtime.
We will see how this recorder is used from the _deployment_ module.
The `@ConfigRoot` annotation declares that the configuration is available during runtime.

== The deployment module
The `@WithDefault("true")` is used to declare the default value returned when `enabled()` is called.

This module contains _build steps_, i.e., methods called during the augmentation phase and computing just enough bytecode to serve the services the application requires.
The version extension consists of a single build step that extracts the version and uses the `VersionRecorder`.
By default, the name of the property is derived from what's declared in the `prefix` attribute inside `@ConfigMapping` plus the name of the method. In our case, the user-configured property will be `quarkus.version.enabled`.

[example, role="cta"]
--
First, let's create the configuration class that will allow users to dynamically configure whether the version should be printed:

Under the `deployment` module, create new class `io.quarkus.workshop.superheroes.version.deployment.VersionConfig` with the following content:
Still in the `runtime` module, create the `io.quarkus.workshop.superheroes.version.runtime.VersionRecorder` class with the following content:

[source,java]
----
include::{code-root}/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java[]
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionRecorder.java[]
----
--

The `@ConfigRoot` annotation declares `VersionConfig` as a configuration class which configuration properties are prefixed with `quarkus.` prefix.
Simple right?
But how does it work?
Look at the `@Recorder` annotation.
It indicates that this class is a _recorder_ used to record actions executed later at runtime.
Indeed, these actions are replayed at runtime.
We will see how this recorder is used from the _deployment_ module.

== The deployment module

The `@ConfigItem` is used to declare individual configuration properties. By default, the name of the property is derived from the name of the field. In our case, the user-configured property will be `quarkus.version.enabled`.
This module contains _build steps_, i.e., methods called during the augmentation phase and computing just enough bytecode to serve the services the application requires.
The version extension consists of a single build step that extracts the version and uses the `VersionRecorder`.

Next, open the `ExtensionVersionProcessor` class, and update the content to be:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.workshop.superheroes.version.runtime.VersionConfig;
import io.quarkus.workshop.superheroes.version.runtime.VersionRecorder;

class ExtensionVersionProcessor {
Expand All @@ -19,8 +20,6 @@ FeatureBuildItem feature() {
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void recordVersion(ApplicationInfoBuildItem app, VersionConfig versionConfig, VersionRecorder recorder) {
if (versionConfig.enabled) {
recorder.printVersion(app.getVersion());
}
recorder.printVersion(versionConfig, app.getVersion());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.workshop.superheroes.version.runtime;

import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.version")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
public interface VersionConfig {

/**
* Enables or disables the version printing at startup.
*/
@WithDefault("true")
boolean enabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
@Recorder
public class VersionRecorder {

public void printVersion(String version) {
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
public void printVersion(VersionConfig versionConfig, String version) {
if (versionConfig.enabled()) {
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
}
}

}

0 comments on commit 21aa7a0

Please sign in to comment.