Skip to content

Commit

Permalink
Set Spring-specific name for shutdown hook thread
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Sep 20, 2019
1 parent 734ceed commit 3603e0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
@@ -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.
Expand Down Expand Up @@ -38,6 +38,7 @@
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 03.11.2003
*/
public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
*/
Expand Down
Expand Up @@ -115,6 +115,7 @@
* @author Juergen Hoeller
* @author Mark Fisher
* @author Stephane Nicoll
* @author Sam Brannen
* @since January 21, 2001
* @see #refreshBeanFactory
* @see #getBeanFactory
Expand Down Expand Up @@ -927,18 +928,20 @@ protected void resetCommonCaches() {


/**
* 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()
*/
@Override
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) {
Expand Down

0 comments on commit 3603e0c

Please sign in to comment.