diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java new file mode 100644 index 00000000000..bbd2c4beb29 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java @@ -0,0 +1,111 @@ +/* + * Copyright 2012-2015 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 + * + * http://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; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import javax.validation.Validation; + +import org.apache.catalina.mbeans.MBeanFactory; + +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; + +/** + * {@link ApplicationListener} to trigger early initialization in a background thread of + * time consuming tasks. + * + * @author Phillip Webb + * @since 1.3.0 + */ +public class BackgroundPreinitializer + implements ApplicationListener { + + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + submit(executor, new MessageConverterInitializer()); + submit(executor, new MBeanFactoryInitializer()); + submit(executor, new ValidationInitializer()); + executor.shutdown(); + } + + private void submit(ExecutorService executor, Runnable runnable) { + executor.submit(new FailSafeRunnable(runnable)); + } + + /** + * Wrapper to ignore any thrown exceptions. + */ + private static class FailSafeRunnable implements Runnable { + + private final Runnable delegate; + + FailSafeRunnable(Runnable delegate) { + this.delegate = delegate; + } + + @Override + public void run() { + try { + this.delegate.run(); + } + catch (Throwable ex) { + // Ignore + } + } + + } + + /** + * Early initializer for Spring MessageConverters. + */ + private static class MessageConverterInitializer implements Runnable { + + @Override + public void run() { + new AllEncompassingFormHttpMessageConverter(); + } + + } + + /** + * Early initializer to load Tomcat MBean XML. + */ + private static class MBeanFactoryInitializer implements Runnable { + + @Override + public void run() { + new MBeanFactory(); + } + + } + + /** + * Early initializer for javax.validation. + */ + private static class ValidationInitializer implements Runnable { + + @Override + public void run() { + Validation.byDefaultProvider().configure(); + } + + } +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 9daae351249..c3cd466d3b7 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -2,6 +2,10 @@ org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer +# Application Listeners +org.springframework.context.ApplicationListener=\ +org.springframework.boot.autoconfigure.BackgroundPreinitializer + # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\