Skip to content

Make it possible to disable the BackgroundPreinitializer #14298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all 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 @@ -38,10 +38,12 @@

/**
* {@link ApplicationListener} to trigger early initialization in a background thread of
* time consuming tasks.
* time consuming tasks. Set property spring.backgroundpreinitializer.ignore=true for
* disable background preinitializer.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Artsiom Yudovin
* @since 1.3.0
*/
@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
Expand All @@ -55,9 +57,11 @@ public class BackgroundPreinitializer

@Override
public void onApplicationEvent(SpringApplicationEvent event) {
if (event instanceof ApplicationStartingEvent
&& preinitializationStarted.compareAndSet(false, true)) {
performPreinitialization();
if (!Boolean.getBoolean("spring.backgroundpreinitializer.ignore")) {
if (event instanceof ApplicationStartingEvent
&& preinitializationStarted.compareAndSet(false, true)) {
this.background(this.performPreinitialization());
}
}
if ((event instanceof ApplicationReadyEvent
|| event instanceof ApplicationFailedEvent)
Expand All @@ -71,31 +75,9 @@ public void onApplicationEvent(SpringApplicationEvent event) {
}
}

private void performPreinitialization() {
private void background(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
}
}

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

private Runnable performPreinitialization() {
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