From bcaee0ebee4d1881ebb81a6a4df265d7e9a8c367 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 15 Dec 2015 14:59:05 +0000 Subject: [PATCH] Perform initialization in foreground if BackgroundPreinitializer fails Google App Engine probits the creation of new threads. This leads to a failure in BackgroundPreinitializer when the single thread executor attempts to create its single thread. This commit enhances the existing fail safety of BackgroundPreinitializer by catching any exceptions thrown while creating the executor and submitting the tasks to it. Any initialisation that has not performed in the background will be performed in the foreground instead. Closes gh-4662 --- .../autoconfigure/BackgroundPreinitializer.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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 index bbd2c4beb29..938aaf803f0 100644 --- 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 @@ -39,11 +39,18 @@ public class BackgroundPreinitializer @Override public void onApplicationEvent(ApplicationStartedEvent event) { - ExecutorService executor = Executors.newSingleThreadExecutor(); - submit(executor, new MessageConverterInitializer()); - submit(executor, new MBeanFactoryInitializer()); - submit(executor, new ValidationInitializer()); - executor.shutdown(); + try { + ExecutorService executor = Executors.newSingleThreadExecutor(); + submit(executor, new MessageConverterInitializer()); + submit(executor, new MBeanFactoryInitializer()); + submit(executor, new ValidationInitializer()); + executor.shutdown(); + } + 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. + } } private void submit(ExecutorService executor, Runnable runnable) {