Commit bcaee0eb authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent da50eb9a
...@@ -39,12 +39,19 @@ public class BackgroundPreinitializer ...@@ -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));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment