From eb3982b6c25d6c3dd49f6c4cc000c40364916a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Fri, 10 Nov 2023 09:07:36 +0100 Subject: [PATCH] Property-driven onRefresh exit for AppCDS purpose This commit allows to terminate the JVM when the -Dspring.context.exit=onRefresh property is set, which can be useful for AppCDS training run in order to get most of the AppCDS cache without starting the beans. Closes gh-31595 --- .../support/DefaultLifecycleProcessor.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java b/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java index 78c9db0483..f1680b3617 100644 --- a/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/support/DefaultLifecycleProcessor.java @@ -73,22 +73,32 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor /** * Property name for a common context checkpoint: {@value}. * @since 6.1 - * @see #CHECKPOINT_ON_REFRESH_VALUE + * @see #ON_REFRESH_VALUE * @see org.crac.Core#checkpointRestore() */ public static final String CHECKPOINT_PROPERTY_NAME = "spring.context.checkpoint"; /** - * Recognized value for the context checkpoint property: {@value}. + * Property name for terminating the JVM when the context reaches a specific phase: {@value}. + * @since 6.1 + * @see #ON_REFRESH_VALUE + */ + public static final String EXIT_PROPERTY_NAME = "spring.context.exit"; + + /** + * Recognized value for the context checkpoint and exit properties: {@value}. * @since 6.1 * @see #CHECKPOINT_PROPERTY_NAME - * @see org.crac.Core#checkpointRestore() + * @see #EXIT_PROPERTY_NAME */ - public static final String CHECKPOINT_ON_REFRESH_VALUE = "onRefresh"; + public static final String ON_REFRESH_VALUE = "onRefresh"; private static final boolean checkpointOnRefresh = - CHECKPOINT_ON_REFRESH_VALUE.equalsIgnoreCase(SpringProperties.getProperty(CHECKPOINT_PROPERTY_NAME)); + ON_REFRESH_VALUE.equalsIgnoreCase(SpringProperties.getProperty(CHECKPOINT_PROPERTY_NAME)); + + private static final boolean exitOnRefresh = + ON_REFRESH_VALUE.equalsIgnoreCase(SpringProperties.getProperty(EXIT_PROPERTY_NAME)); private final Log logger = LogFactory.getLog(getClass()); @@ -182,6 +192,9 @@ public class DefaultLifecycleProcessor implements LifecycleProcessor, BeanFactor if (checkpointOnRefresh) { new CracDelegate().checkpointRestore(); } + if (exitOnRefresh) { + Runtime.getRuntime().halt(0); + } this.stoppedBeans = null; try {