BackgroundPreinitializingApplicationListener Class — spring-boot Architecture
Architecture documentation for the BackgroundPreinitializingApplicationListener class in BackgroundPreinitializingApplicationListener.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializingApplicationListener.java lines 46–133
class BackgroundPreinitializingApplicationListener implements ApplicationListener<SpringApplicationEvent>, Ordered {
/**
* System property that instructs Spring Boot how to run pre initialization. When the
* property is set to {@code true}, no pre-initialization happens and each item is
* initialized in the foreground as it needs to. When the property is {@code false}
* (default), pre initialization runs in a separate thread in the background.
*/
public static final String IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME = "spring.backgroundpreinitializer.ignore";
private static final AtomicBoolean started = new AtomicBoolean();
private static final CountDownLatch complete = new CountDownLatch(1);
private final SpringFactoriesLoader factoriesLoader;
private final boolean enabled;
BackgroundPreinitializingApplicationListener() {
this(SpringFactoriesLoader.forDefaultResourceLocation());
}
BackgroundPreinitializingApplicationListener(SpringFactoriesLoader factoriesLoader) {
this.factoriesLoader = factoriesLoader;
this.enabled = !NativeDetector.inNativeImage()
&& !Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME)
&& Runtime.getRuntime().availableProcessors() > 1;
}
@Override
public int getOrder() {
return LoggingApplicationListener.DEFAULT_ORDER + 1;
}
@Override
public void onApplicationEvent(SpringApplicationEvent event) {
if (!this.enabled) {
return;
}
if (event instanceof ApplicationEnvironmentPreparedEvent && started.compareAndSet(false, true)) {
preinitialize();
}
if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) && started.get()) {
try {
complete.await();
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
private void preinitialize() {
Runner runner = new Runner(this.factoriesLoader.load(BackgroundPreinitializer.class));
try {
Thread thread = new Thread(runner, "background-preinit");
thread.start();
}
catch (Exception ex) {
// This will fail on Google App Engine where creating threads is
// prohibited. We can safely continue but startup will be slightly slower
// as the initialization will now happen on the main thread.
complete.countDown();
}
}
/**
* Runner thread to call the {@link BackgroundPreinitializer} instances.
*
* @param preinitializers the preinitializers
*/
record Runner(List<BackgroundPreinitializer> preinitializers) implements Runnable {
@Override
public void run() {
for (BackgroundPreinitializer preinitializer : this.preinitializers) {
try {
preinitializer.preinitialize();
}
catch (Throwable ex) {
}
}
complete.countDown();
}
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free