Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
8b9991fc
Commit
8b9991fc
authored
Jun 24, 2021
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.5.x'
Closes gh-27076
parents
0c01a08b
5a9fa3c8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
0 deletions
+38
-0
SpringApplicationShutdownHook.java
...g/springframework/boot/SpringApplicationShutdownHook.java
+3
-0
SpringApplicationShutdownHookTests.java
...ingframework/boot/SpringApplicationShutdownHookTests.java
+35
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplicationShutdownHook.java
View file @
8b9991fc
...
@@ -125,6 +125,9 @@ class SpringApplicationShutdownHook implements Runnable {
...
@@ -125,6 +125,9 @@ class SpringApplicationShutdownHook implements Runnable {
* @param context the context to clean
* @param context the context to clean
*/
*/
private
void
closeAndWait
(
ConfigurableApplicationContext
context
)
{
private
void
closeAndWait
(
ConfigurableApplicationContext
context
)
{
if
(!
context
.
isActive
())
{
return
;
}
context
.
close
();
context
.
close
();
try
{
try
{
int
waited
=
0
;
int
waited
=
0
;
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationShutdownHookTests.java
View file @
8b9991fc
...
@@ -26,10 +26,12 @@ import java.util.concurrent.CountDownLatch;
...
@@ -26,10 +26,12 @@ import java.util.concurrent.CountDownLatch;
import
org.awaitility.Awaitility
;
import
org.awaitility.Awaitility
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.support.AbstractApplicationContext
;
import
org.springframework.context.support.AbstractApplicationContext
;
import
org.springframework.context.support.GenericApplicationContext
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatIllegalArgumentException
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatIllegalArgumentException
;
...
@@ -91,6 +93,15 @@ class SpringApplicationShutdownHookTests {
...
@@ -91,6 +93,15 @@ class SpringApplicationShutdownHookTests {
assertThat
(
finished
).
containsExactly
(
context
,
handlerAction
);
assertThat
(
finished
).
containsExactly
(
context
,
handlerAction
);
}
}
@Test
void
runDueToExitDuringRefreshWhenContextHasBeenClosedDoesNotDeadlock
()
throws
InterruptedException
{
GenericApplicationContext
context
=
new
GenericApplicationContext
();
TestSpringApplicationShutdownHook
shutdownHook
=
new
TestSpringApplicationShutdownHook
();
shutdownHook
.
registerApplicationContext
(
context
);
context
.
registerBean
(
CloseContextAndExit
.
class
,
context
,
shutdownHook
);
context
.
refresh
();
}
@Test
@Test
void
runWhenContextIsClosedDirectlyRunsHandlerActions
()
{
void
runWhenContextIsClosedDirectlyRunsHandlerActions
()
{
TestSpringApplicationShutdownHook
shutdownHook
=
new
TestSpringApplicationShutdownHook
();
TestSpringApplicationShutdownHook
shutdownHook
=
new
TestSpringApplicationShutdownHook
();
...
@@ -221,4 +232,28 @@ class SpringApplicationShutdownHookTests {
...
@@ -221,4 +232,28 @@ class SpringApplicationShutdownHookTests {
}
}
static
class
CloseContextAndExit
implements
InitializingBean
{
private
final
ConfigurableApplicationContext
context
;
private
final
Runnable
shutdownHook
;
CloseContextAndExit
(
ConfigurableApplicationContext
context
,
SpringApplicationShutdownHook
shutdownHook
)
{
this
.
context
=
context
;
this
.
shutdownHook
=
shutdownHook
;
}
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
this
.
context
.
close
();
// Simulate System.exit by running the hook on a separate thread and waiting
// for it to complete
Thread
thread
=
new
Thread
(
this
.
shutdownHook
);
thread
.
start
();
thread
.
join
(
15000
);
assertThat
(
thread
.
isAlive
()).
isFalse
();
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment