Opt-in training run: start, warm up, then exit 0 (for JDK AOT cache, CDS, PGO and agent runs)

Author: alvarosanchezCreated Sep 24, 2026Updated Sep 24, 2026
Labelstype: enhancementrelates-to: http-server

Feature description

Add an opt-in training run to Micronaut.run. The application starts, optionally sends a few GET requests to itself, then stops and exits with status 0. The JDK AOT cache (-XX:AOTCacheOutput), dynamic CDS, GraalVM PGO (.iprof) and native-image-agent all need a representative run that ends in an orderly JVM exit.

Why a switch is needed

A Micronaut application never stops on its own:

So every pipeline scripts a readiness loop, a warm-up and a stop (SIGTERM, a stop endpoint or jcmd), as the Gradle CRaC plugin does for checkpoints (checkpoint.sh). On Windows, Process.destroy() skips shutdown hooks, so the JVM writes nothing. JDK 25.0.4 and later can end an AOT recording with jcmd <pid> AOT.end_recording (JDK-8370203). That needs jcmd, which the default eclipse-temurin:25-jre image does not have (MicronautDockerfile.java), and it does not cover PGO, the agent or a warm-up.

The switch needs a warm-up (measured)

Exiting right after startup, like Spring's spring.context.exit=onRefresh, is not enough. We trained two AOT caches that differ only in when training stopped:

Stop at "Startup completed" First 200, then GET /hello ×3
Median time to first 200 306.2 ms 259.1 ms
Classes not served from the cache 626 of 6,438 (175 io.micronaut, 70 lambdas) 87 of 6,483 (8 io.micronaut, 5 lambdas)
Max RSS 158.0–160.0 MiB 130.9–131.3 MiB
  • Paired difference: +49.0 ms [+36.4, +56.7]. Without a cache the median is 520.5 ms, so stopping at startup gives up about 18% of the cache's gain.
  • In-process requests match external ones. A wrapper main sending 4 GETs through HttpURLConnection, then System.exit(0), against curl plus SIGTERM: −6.5 ms [−10.8, +6.5], with the same misses and RSS.
  • A training -javaagent is not an alternative. Its cache missed 802 classes (about 170 MiB max RSS), because java.instrument turns off the cache's optimized module handling.

Proposed change

  1. Property. An ordinary property, for example micronaut.application.training.enabled, so -D and MICRONAUT_APPLICATION_TRAINING_ENABLED both work. It is off by default and logs a WARN when on.
  2. Exit, in Micronaut.start() (context). Act right after embeddedApplication.start() returns. By then every startup listener has run synchronously (NettyHttpServer.java).
    • Stop the application, close the context and call System.exit(0). This covers every EmbeddedApplication, messaging included.
    • Skip System.exit in the test environment, as handleStartupException does.
    • Failures already go through handleStartupException and exit non-zero, so a failed training run fails the build.
  3. Warm-up, in http-server. Optional …training.warmup.paths and …warmup.repeat.
    • A ServerStartupEvent listener, ordered last, sends the GETs through java.net.HttpURLConnection, so no client module is needed.
    • Request the configured micronaut.server.host, or loopback when none is set. An unset host binds the wildcard address, while getURL() falls back to $HOSTNAME.
    • A status of 400 or more, or an I/O error, throws. That fails startup, because listener exceptions propagate.
    • HTTPS-only servers need a custom warm-up.
  4. No new SPI. Document custom warm-up (POST, auth, messaging) as a startup listener annotated @Requires(property = "micronaut.application.training.enabled", value = "true").
Sketch of the exit in Micronaut.start()
java
// after embeddedApplication.start() and the "Startup completed" log, before the shutdown hook and latch
Environment environment = applicationContext.getEnvironment();
if (environment.getProperty(TRAINING_ENABLED, Boolean.class, false)) {
    LOG.warn("Training run ({}=true): startup and warm-up done, exiting with 0", TRAINING_ENABLED);
    try (applicationContext) {
        embeddedApplication.stop();
    }
    if (!environment.getActiveNames().contains(Environment.TEST)) {
        System.exit(0);
    }
    return applicationContext;
}

Compatibility

  • Opt-in only. The warm-up adds about 40 JDK networking classes to the cache, with no measured startup or RSS cost.
  • Training runs application code. Applications that need external services should train with a training environment or with test resources.

How to verify

  • Functional tests: an HTTP application with warm-up paths and a messaging-only application both exit 0; a warm-up path returning 500 exits non-zero; nothing exits under test.
  • java -XX:AOTCacheOutput=app.aot -Dmicronaut.application.training.enabled=true -jar app.jar writes the cache. Replacing the output flag with a strict -XX:AOTMode=on -XX:AOTCache=app.aot also exits 0.
  • A Windows CI job covers the exit.

How this was found

We profiled micronaut-runner with its benchmark-large sample (Micronaut Platform 5.1.5, Netty).

  • Setup. Runner's extracted layout (a JAR-only class path) on OpenJDK 25.0.4.1, macOS on Apple Silicon with 12 CPUs, on a shared machine. Caches were made with -XX:AOTMode=record and then create.
  • Timing. From spawn to the first HTTP 200. After 3 warm-up rounds, we ran n=20 paired rounds in random interleaved order and report the median paired difference with a bootstrap 95% CI.
  • Diagnostics. Class counts come from one -Xlog:class+load launch per cache. Max RSS is from /usr/bin/time -l over 3 runs.

Related

  • In-image training in micronaut-projects/micronaut-gradle-plugin#1379 and micronaut-projects/micronaut-maven-plugin#1721 would use this switch, falling back to a script on older cores.
  • micronaut-runner:

Source: micronaut-projects/micronaut-core