Set Spring-specific name for shutdown hook thread

Prior to this commit, the name of the Thread registered via
ConfigurableApplicationContext#registerShutdownHook() was the generic,
default thread name ("Thread-#"). That made it difficult to discern
which executing thread was the Spring ApplicationContext shutdown hook.

This commit improves diagnostics by setting the thread name of the
ApplicationContext shutdown hook to "SpringContextShutdownHook".

Closes gh-23670
This commit is contained in:
Sam Brannen
2019-09-20 13:16:59 +02:00
parent 734ceed301
commit 3603e0c448
2 changed files with 18 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ import org.springframework.lang.Nullable;
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 03.11.2003
*/
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
@@ -86,6 +87,14 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
*/
String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment";
/**
* {@link Thread#getName() Name} of the {@linkplain #registerShutdownHook()
* shutdown hook} thread: {@value}.
* @since 5.2
* @see #registerShutdownHook()
*/
String SHUTDOWN_HOOK_THREAD_NAME = "SpringContextShutdownHook";
/**
* Set the unique id of this application context.
@@ -164,6 +173,8 @@ public interface ConfigurableApplicationContext extends ApplicationContext, Life
* on JVM shutdown unless it has already been closed at that time.
* <p>This method can be called multiple times. Only one shutdown hook
* (at max) will be registered for each context instance.
* <p>As of Spring Framework 5.2, the {@linkplain Thread#getName() name} of
* the shutdown hook thread should be {@link #SHUTDOWN_HOOK_THREAD_NAME}.
* @see java.lang.Runtime#addShutdownHook
* @see #close()
*/

View File

@@ -115,6 +115,7 @@ import org.springframework.util.ReflectionUtils;
* @author Juergen Hoeller
* @author Mark Fisher
* @author Stephane Nicoll
* @author Sam Brannen
* @since January 21, 2001
* @see #refreshBeanFactory
* @see #getBeanFactory
@@ -927,10 +928,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
/**
* Register a shutdown hook with the JVM runtime, closing this context
* on JVM shutdown unless it has already been closed at that time.
* Register a shutdown hook {@linkplain Thread#getName() named}
* {@code SpringContextShutdownHook} with the JVM runtime, closing this
* context on JVM shutdown unless it has already been closed at that time.
* <p>Delegates to {@code doClose()} for the actual closing procedure.
* @see Runtime#addShutdownHook
* @see ConfigurableApplicationContext#SHUTDOWN_HOOK_THREAD_NAME
* @see #close()
* @see #doClose()
*/
@@ -938,7 +941,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
public void registerShutdownHook() {
if (this.shutdownHook == null) {
// No shutdown hook registered yet.
this.shutdownHook = new Thread() {
this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) {
@Override
public void run() {
synchronized (startupShutdownMonitor) {