From 304c6a02ba9a526f9a2bbbd944ba47a4003f874c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 5 Sep 2018 14:09:14 +0200 Subject: [PATCH] Polish "Make it possible to disable the BackgroundPreinitializer" Closes gh-14298 --- .../BackgroundPreinitializer.java | 77 ++++++++++--------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java index 0dabb8d3b1a..6566b819e65 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java @@ -38,8 +38,11 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage /** * {@link ApplicationListener} to trigger early initialization in a background thread of - * time consuming tasks. Set property spring.backgroundpreinitializer.ignore=true for - * disable background preinitializer. + * time consuming tasks. + *

+ * Set the {@value IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME} system property to + * {@code true} to disable this mechanism and let such initialization happen in the + * foreground. * * @author Phillip Webb * @author Andy Wilkinson @@ -50,6 +53,15 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage public class BackgroundPreinitializer implements ApplicationListener { + /** + * System property that instructs Spring Boot how to run pre initialization. When the + * property is set to {@code true}, no pre intialization 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. + * @since 2.1.0 + */ + public static final String IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME = "spring.backgroundpreinitializer.ignore"; + private static final AtomicBoolean preinitializationStarted = new AtomicBoolean( false); @@ -57,11 +69,10 @@ public class BackgroundPreinitializer @Override public void onApplicationEvent(SpringApplicationEvent event) { - if (!Boolean.getBoolean("spring.backgroundpreinitializer.ignore")) { - if (event instanceof ApplicationStartingEvent - && preinitializationStarted.compareAndSet(false, true)) { - this.background(this.performPreinitialization()); - } + if (!Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME) + && event instanceof ApplicationStartingEvent + && preinitializationStarted.compareAndSet(false, true)) { + performPreinitialization(); } if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent) @@ -75,9 +86,31 @@ public class BackgroundPreinitializer } } - private void background(Runnable runnable) { + private void performPreinitialization() { try { - Thread thread = new Thread(runnable, "background-preinit"); + 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.start(); } catch (Exception ex) { @@ -88,32 +121,6 @@ public class BackgroundPreinitializer } } - 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. */