Skip to content

Commit 56b817e

Browse files
committed
Merge branch '1.5.x'
2 parents 579c6fe + d62c26c commit 56b817e

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616

1717
package org.springframework.boot.autoconfigure;
1818

19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.atomic.AtomicBoolean;
21+
1922
import javax.validation.Validation;
2023

2124
import org.apache.catalina.mbeans.MBeanFactory;
2225

2326
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
27+
import org.springframework.boot.context.event.ApplicationFailedEvent;
28+
import org.springframework.boot.context.event.ApplicationReadyEvent;
29+
import org.springframework.boot.context.event.SpringApplicationEvent;
2430
import org.springframework.boot.context.logging.LoggingApplicationListener;
2531
import org.springframework.context.ApplicationListener;
2632
import org.springframework.core.annotation.Order;
@@ -38,10 +44,31 @@
3844
*/
3945
@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
4046
public class BackgroundPreinitializer
41-
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
47+
implements ApplicationListener<SpringApplicationEvent> {
48+
49+
private static final AtomicBoolean preinitalizationStarted = new AtomicBoolean(false);
50+
51+
private static final CountDownLatch preinitializationComplete = new CountDownLatch(1);
4252

4353
@Override
44-
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
54+
public void onApplicationEvent(SpringApplicationEvent event) {
55+
if (event instanceof ApplicationEnvironmentPreparedEvent) {
56+
if (preinitalizationStarted.compareAndSet(false, true)) {
57+
performPreinitialization();
58+
}
59+
}
60+
if (event instanceof ApplicationReadyEvent
61+
|| event instanceof ApplicationFailedEvent) {
62+
try {
63+
preinitializationComplete.await();
64+
}
65+
catch (InterruptedException ex) {
66+
Thread.currentThread().interrupt();
67+
}
68+
}
69+
}
70+
71+
private void performPreinitialization() {
4572
try {
4673
Thread thread = new Thread(new Runnable() {
4774

@@ -52,6 +79,7 @@ public void run() {
5279
runSafely(new ValidationInitializer());
5380
runSafely(new JacksonInitializer());
5481
runSafely(new ConversionServiceInitializer());
82+
preinitializationComplete.countDown();
5583
}
5684

5785
public void runSafely(Runnable runnable) {
@@ -70,6 +98,7 @@ public void runSafely(Runnable runnable) {
7098
// This will fail on GAE where creating threads is prohibited. We can safely
7199
// continue but startup will be slightly slower as the initialization will now
72100
// happen on the main thread.
101+
preinitializationComplete.countDown();
73102
}
74103
}
75104

0 commit comments

Comments
 (0)