Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Artsiom Yudovin
* @since 1.3.0
*/
@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
Expand All @@ -57,7 +58,13 @@ public class BackgroundPreinitializer
public void onApplicationEvent(SpringApplicationEvent event) {
if (event instanceof ApplicationStartingEvent
&& preinitializationStarted.compareAndSet(false, true)) {
performPreinitialization();

if (Boolean.getBoolean("spring.backgroundpreinitializer.ignore")) {
performPreinitialization(this.perform());
}
else {
performPreinitializationBackground(this.perform());
}
}
if ((event instanceof ApplicationReadyEvent
|| event instanceof ApplicationFailedEvent)
Expand All @@ -71,31 +78,21 @@ public void onApplicationEvent(SpringApplicationEvent event) {
}
}

private void performPreinitialization() {
private void performPreinitialization(Runnable runnable) {
try {
Thread thread = new Thread(new Runnable() {

@Override
public void run() {
runSafely(new ConversionServiceInitializer());
runSafely(new ValidationInitializer());
runSafely(new MessageConverterInitializer());
runSafely(new MBeanFactoryInitializer());
runSafely(new JacksonInitializer());
runSafely(new CharsetInitializer());
preinitializationComplete.countDown();
}

public void runSafely(Runnable runnable) {
try {
runnable.run();
}
catch (Throwable ex) {
// Ignore
}
}
runnable.run();
}
catch (Exception ex) {
// This will fail on GAE 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.
preinitializationComplete.countDown();
}
}

}, "background-preinit");
private void performPreinitializationBackground(Runnable runnable) {
try {
Thread thread = new Thread(runnable, "background-preinit");
thread.start();
}
catch (Exception ex) {
Expand All @@ -106,6 +103,32 @@ public void runSafely(Runnable runnable) {
}
}

private Runnable perform() {
return new Runnable() {

@Override
public void run() {
runSafely(new ConversionServiceInitializer());
runSafely(new ValidationInitializer());
runSafely(new MessageConverterInitializer());
runSafely(new MBeanFactoryInitializer());
runSafely(new JacksonInitializer());
runSafely(new CharsetInitializer());
preinitializationComplete.countDown();
}

public void runSafely(Runnable runnable) {
try {
runnable.run();
}
catch (Throwable ex) {
// Ignore
}
}

};
}

/**
* Early initializer for Spring MessageConverters.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ content into your application. Rather, pick only the properties that you need.

# SPRING CORE
spring.beaninfo.ignore=true # Whether to skip search of BeanInfo classes.
spring.backgroundpreinitializer.ignore=true # Whether background preinitializer should be ignored.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't add this here as it's not managed the same way as a regular property. Let's document the property and its usage on the class Javadoc instead.


# SPRING CACHE ({sc-spring-boot-autoconfigure}/cache/CacheProperties.{sc-ext}[CacheProperties])
spring.cache.cache-names= # Comma-separated list of cache names to create if supported by the underlying cache manager.
Expand Down