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
This commit is contained in:
Andy Wilkinson 2015-12-15 14:59:05 +00:00
parent da50eb9ab2
commit bcaee0ebee

View File

@ -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) {