diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index ede5ab2b6549..c63d2ce0da35 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -1633,6 +1633,9 @@ public ConfigurableApplicationContext getApplicationContext() { } + /** + * {@link SpringApplicationHook} decorator that ensures the hook is only used once. + */ private static final class SingleUseSpringApplicationHook implements SpringApplicationHook { private final AtomicBoolean used = new AtomicBoolean(); @@ -1651,8 +1654,8 @@ public SpringApplicationRunListener getRunListener(SpringApplication springAppli } /** - * <<<<<<< HEAD Starts a non-daemon thread to keep the JVM alive on - * {@link ContextRefreshedEvent}. Stops the thread on {@link ContextClosedEvent}. + * Starts a non-daemon thread to keep the JVM alive on {@link ContextRefreshedEvent}. + * Stops the thread on {@link ContextClosedEvent}. */ private static final class KeepAlive implements ApplicationListener { @@ -1696,15 +1699,18 @@ private void stopKeepAliveThread() { } + /** + * Strategy used to handle startup concerns. + */ abstract static class Startup { private Duration timeTakenToStarted; - abstract long startTime(); + protected abstract long startTime(); - abstract Long processUptime(); + protected abstract Long processUptime(); - abstract String action(); + protected abstract String action(); final Duration started() { long now = System.currentTimeMillis(); @@ -1712,15 +1718,15 @@ final Duration started() { return this.timeTakenToStarted; } + Duration timeTakenToStarted() { + return this.timeTakenToStarted; + } + private Duration ready() { long now = System.currentTimeMillis(); return Duration.ofMillis(now - startTime()); } - Duration timeTakenToStarted() { - return this.timeTakenToStarted; - } - static Startup create() { ClassLoader classLoader = Startup.class.getClassLoader(); return (ClassUtils.isPresent("jdk.crac.management.CRaCMXBean", classLoader) @@ -1730,61 +1736,61 @@ static Startup create() { } - private static class CoordinatedRestoreAtCheckpointStartup extends Startup { + /** + * Standard {@link Startup} implementation. + */ + private static class StandardStartup extends Startup { - private final StandardStartup fallback = new StandardStartup(); + private final Long startTime = System.currentTimeMillis(); @Override - Long processUptime() { - long uptime = CRaCMXBean.getCRaCMXBean().getUptimeSinceRestore(); - return (uptime >= 0) ? uptime : this.fallback.processUptime(); + protected long startTime() { + return this.startTime; } @Override - String action() { - if (restoreTime() >= 0) { - return "Restored"; + protected Long processUptime() { + try { + return ManagementFactory.getRuntimeMXBean().getUptime(); + } + catch (Throwable ex) { + return null; } - return this.fallback.action(); - } - - private long restoreTime() { - return CRaCMXBean.getCRaCMXBean().getRestoreTime(); } @Override - long startTime() { - long restoreTime = restoreTime(); - if (restoreTime >= 0) { - return restoreTime; - } - return this.fallback.startTime(); + protected String action() { + return "Started"; } } - private static class StandardStartup extends Startup { + /** + * Coordinated-Restore-At-Checkpoint {@link Startup} implementation. + */ + private static class CoordinatedRestoreAtCheckpointStartup extends Startup { - private final Long startTime = System.currentTimeMillis(); + private final StandardStartup fallback = new StandardStartup(); @Override - long startTime() { - return this.startTime; + protected Long processUptime() { + long uptime = CRaCMXBean.getCRaCMXBean().getUptimeSinceRestore(); + return (uptime >= 0) ? uptime : this.fallback.processUptime(); } @Override - Long processUptime() { - try { - return ManagementFactory.getRuntimeMXBean().getUptime(); - } - catch (Throwable ex) { - return null; - } + protected String action() { + return (restoreTime() >= 0) ? "Restored" : this.fallback.action(); + } + + private long restoreTime() { + return CRaCMXBean.getCRaCMXBean().getRestoreTime(); } @Override - String action() { - return "Started"; + protected long startTime() { + long restoreTime = restoreTime(); + return (restoreTime >= 0) ? restoreTime : this.fallback.startTime(); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/StartupInfoLoggerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/StartupInfoLoggerTests.java index a6b1c90ba6de..ae1bbb4ed2ee 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/StartupInfoLoggerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/StartupInfoLoggerTests.java @@ -110,17 +110,17 @@ static class TestStartup extends Startup { } @Override - long startTime() { + protected long startTime() { return this.startTime; } @Override - Long processUptime() { + protected Long processUptime() { return this.uptime; } @Override - String action() { + protected String action() { return this.action; }