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
pull/4760/merge
Andy Wilkinson 9 years ago
parent da50eb9ab2
commit bcaee0ebee

@ -39,12 +39,19 @@ public class BackgroundPreinitializer
@Override @Override
public void onApplicationEvent(ApplicationStartedEvent event) { public void onApplicationEvent(ApplicationStartedEvent event) {
try {
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
submit(executor, new MessageConverterInitializer()); submit(executor, new MessageConverterInitializer());
submit(executor, new MBeanFactoryInitializer()); submit(executor, new MBeanFactoryInitializer());
submit(executor, new ValidationInitializer()); submit(executor, new ValidationInitializer());
executor.shutdown(); 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) { private void submit(ExecutorService executor, Runnable runnable) {
executor.submit(new FailSafeRunnable(runnable)); executor.submit(new FailSafeRunnable(runnable));

Loading…
Cancel
Save