Skip to content

Commit 1d316c7

Browse files
philwebbwilkinsona
authored andcommitted
Split up BackgroundPreinitializer
Issue: 44065
1 parent 1e0c0ea commit 1d316c7

12 files changed

+437
-217
lines changed

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

-216
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.preinitialize;
18+
19+
/**
20+
* Interface used to preinitialize in the background code that may otherwise cause a delay
21+
* when first called. Implementations should be registered in {@code spring.factories}.
22+
*
23+
* @author Phillip Webb
24+
* @since 4.0.0
25+
*/
26+
@FunctionalInterface
27+
public interface BackgroundPreinitializer {
28+
29+
/**
30+
* Perform any require preinitialization.
31+
* @throws Exception on any initialization error
32+
*/
33+
void preinitialize() throws Exception;
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.preinitialize;
18+
19+
import java.util.List;
20+
import java.util.concurrent.CountDownLatch;
21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
23+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
24+
import org.springframework.boot.context.event.ApplicationFailedEvent;
25+
import org.springframework.boot.context.event.ApplicationReadyEvent;
26+
import org.springframework.boot.context.event.SpringApplicationEvent;
27+
import org.springframework.boot.context.logging.LoggingApplicationListener;
28+
import org.springframework.context.ApplicationListener;
29+
import org.springframework.core.NativeDetector;
30+
import org.springframework.core.Ordered;
31+
import org.springframework.core.io.support.SpringFactoriesLoader;
32+
33+
/**
34+
* {@link ApplicationListener} to trigger early initialization in a background thread of
35+
* time-consuming tasks.
36+
* <p>
37+
* Set the {@link #IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME} system property to
38+
* {@code true} to disable this mechanism.
39+
*
40+
* @author Phillip Webb
41+
* @author Andy Wilkinson
42+
* @author Artsiom Yudovin
43+
* @author Sebastien Deleuze
44+
* @see BackgroundPreinitializer
45+
*/
46+
class BackgroundPreinitializingApplicationListener implements ApplicationListener<SpringApplicationEvent>, Ordered {
47+
48+
/**
49+
* System property that instructs Spring Boot how to run pre initialization. When the
50+
* property is set to {@code true}, no pre-initialization happens and each item is
51+
* initialized in the foreground as it needs to. When the property is {@code false}
52+
* (default), pre initialization runs in a separate thread in the background.
53+
*/
54+
public static final String IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME = "spring.backgroundpreinitializer.ignore";
55+
56+
private static final AtomicBoolean started = new AtomicBoolean();
57+
58+
private static final CountDownLatch complete = new CountDownLatch(1);
59+
60+
private final SpringFactoriesLoader factoriesLoader;
61+
62+
private final boolean enabled;
63+
64+
BackgroundPreinitializingApplicationListener() {
65+
this(SpringFactoriesLoader.forDefaultResourceLocation());
66+
}
67+
68+
BackgroundPreinitializingApplicationListener(SpringFactoriesLoader factoriesLoader) {
69+
this.factoriesLoader = factoriesLoader;
70+
this.enabled = !NativeDetector.inNativeImage()
71+
&& !Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME)
72+
&& Runtime.getRuntime().availableProcessors() > 1;
73+
}
74+
75+
@Override
76+
public int getOrder() {
77+
return LoggingApplicationListener.DEFAULT_ORDER + 1;
78+
}
79+
80+
@Override
81+
public void onApplicationEvent(SpringApplicationEvent event) {
82+
if (!this.enabled) {
83+
return;
84+
}
85+
if (event instanceof ApplicationEnvironmentPreparedEvent && started.compareAndSet(false, true)) {
86+
preinitialize();
87+
}
88+
if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) && started.get()) {
89+
try {
90+
complete.await();
91+
}
92+
catch (InterruptedException ex) {
93+
Thread.currentThread().interrupt();
94+
}
95+
}
96+
}
97+
98+
private void preinitialize() {
99+
Runner runner = new Runner(this.factoriesLoader.load(BackgroundPreinitializer.class));
100+
try {
101+
Thread thread = new Thread(runner, "background-preinit");
102+
thread.start();
103+
}
104+
catch (Exception ex) {
105+
// This will fail on Google App Engine where creating threads is
106+
// prohibited. We can safely continue but startup will be slightly slower
107+
// as the initialization will now happen on the main thread.
108+
complete.countDown();
109+
}
110+
}
111+
112+
/**
113+
* Runner thread to call the {@link BackgroundPreinitializer} instances.
114+
*
115+
* @param preinitializers the preinitializers
116+
*/
117+
record Runner(List<BackgroundPreinitializer> preinitializers) implements Runnable {
118+
119+
@Override
120+
public void run() {
121+
for (BackgroundPreinitializer preinitializer : this.preinitializers) {
122+
try {
123+
preinitializer.preinitialize();
124+
}
125+
catch (Throwable ex) {
126+
}
127+
}
128+
complete.countDown();
129+
}
130+
131+
}
132+
133+
}

0 commit comments

Comments
 (0)