From 10df14c488bbca2d5a3bd6b85a00839672f12217 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 10 Oct 2019 10:24:33 -0300 Subject: [PATCH] Display application name in shutdown message Fixes #4499 --- .../quarkus/deployment/steps/MainClassBuildStep.java | 5 +++++ .../src/main/java/io/quarkus/runtime/Application.java | 7 ++++++- .../src/main/java/io/quarkus/runtime/Timing.java | 10 ++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/MainClassBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/MainClassBuildStep.java index 3c81287427064..af9a314a80faa 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/MainClassBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/MainClassBuildStep.java @@ -234,6 +234,11 @@ MainClassBuildItem build(List staticInitTasks, mv.setModifiers(Modifier.PUBLIC | Modifier.STATIC); final ResultHandle appClassInstance = mv.newInstance(ofConstructor(appClassName)); + + // Set the application name + mv.invokeVirtualMethod(ofMethod(Application.class, "setName", void.class, String.class), appClassInstance, + mv.load(applicationInfo.getName())); + // run the app mv.invokeVirtualMethod(ofMethod(Application.class, "run", void.class, String[].class), appClassInstance, mv.getMethodParam(0)); diff --git a/core/runtime/src/main/java/io/quarkus/runtime/Application.java b/core/runtime/src/main/java/io/quarkus/runtime/Application.java index a0e8b2d721bd2..8cc773f761324 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/Application.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/Application.java @@ -36,6 +36,7 @@ public abstract class Application { private final Lock stateLock = Locks.reentrantLock(); private final Condition stateCond = stateLock.newCondition(); + private String name; private int state = ST_INITIAL; private volatile boolean shutdownRequested; private static volatile Application currentApplication; @@ -164,7 +165,7 @@ public final void stop() { stateLock.lock(); try { state = ST_STOPPED; - Timing.printStopTime(); + Timing.printStopTime(name); stateCond.signalAll(); } finally { stateLock.unlock(); @@ -178,6 +179,10 @@ public static Application currentApplication() { protected abstract void doStop(); + public void setName(String name) { + this.name = name; + } + /** * Run the application as if it were in a standalone JVM. */ diff --git a/core/runtime/src/main/java/io/quarkus/runtime/Timing.java b/core/runtime/src/main/java/io/quarkus/runtime/Timing.java index 60a993947bb6e..e834ae4ea719c 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/Timing.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/Timing.java @@ -58,8 +58,8 @@ public static void printStartupTime(String name, String version, String quarkusV final Logger logger = Logger.getLogger("io.quarkus"); //Use a BigDecimal so we can render in seconds with 3 digits precision, as requested: final BigDecimal secondsRepresentation = convertToBigDecimalSeconds(bootTimeNanoSeconds); - String safeAppName = (name == null || name.isEmpty()) ? UNSET_VALUE : name; - String safeAppVersion = (version == null || version.isEmpty()) ? UNSET_VALUE : version; + String safeAppName = (name == null || name.trim().isEmpty()) ? UNSET_VALUE : name; + String safeAppVersion = (version == null || version.trim().isEmpty()) ? UNSET_VALUE : version; if (UNSET_VALUE.equals(safeAppName) || UNSET_VALUE.equals(safeAppVersion)) { logger.infof("Quarkus %s started in %ss. %s", quarkusVersion, secondsRepresentation, httpServerInfo); } else { @@ -71,11 +71,13 @@ public static void printStartupTime(String name, String version, String quarkusV bootStartTime = -1; } - public static void printStopTime() { + public static void printStopTime(String name) { final long stopTimeNanoSeconds = System.nanoTime() - bootStopTime; final Logger logger = Logger.getLogger("io.quarkus"); final BigDecimal secondsRepresentation = convertToBigDecimalSeconds(stopTimeNanoSeconds); - logger.infof("Quarkus stopped in %ss", secondsRepresentation); + logger.infof("%s stopped in %ss", + (UNSET_VALUE.equals(name) || name == null || name.trim().isEmpty()) ? "Quarkus" : name, + secondsRepresentation); bootStopTime = -1; }