-
Notifications
You must be signed in to change notification settings - Fork 41k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
437 additions
and
217 deletions.
There are no files selected for viewing
216 changes: 0 additions & 216 deletions
216
...figure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
...n/java/org/springframework/boot/autoconfigure/preinitialize/BackgroundPreinitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2012-2024 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.preinitialize; | ||
|
||
/** | ||
* Interface used to preinitialize in the background code that may otherwise cause a delay | ||
* when first called. Implementations should be registered in {@code spring.factories}. | ||
* | ||
* @author Phillip Webb | ||
* @since 4.0.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface BackgroundPreinitializer { | ||
|
||
/** | ||
* Perform any require preinitialization. | ||
* @throws Exception on any initialization error | ||
*/ | ||
void preinitialize() throws Exception; | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
...mework/boot/autoconfigure/preinitialize/BackgroundPreinitializingApplicationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright 2012-2025 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.preinitialize; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; | ||
import org.springframework.boot.context.event.ApplicationFailedEvent; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.boot.context.event.SpringApplicationEvent; | ||
import org.springframework.boot.context.logging.LoggingApplicationListener; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.core.NativeDetector; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.io.support.SpringFactoriesLoader; | ||
|
||
/** | ||
* {@link ApplicationListener} to trigger early initialization in a background thread of | ||
* time-consuming tasks. | ||
* <p> | ||
* Set the {@link #IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME} system property to | ||
* {@code true} to disable this mechanism. | ||
* | ||
* @author Phillip Webb | ||
* @author Andy Wilkinson | ||
* @author Artsiom Yudovin | ||
* @author Sebastien Deleuze | ||
* @see BackgroundPreinitializer | ||
*/ | ||
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(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.