Revert "Use virtual threads in BackgroundPreinitializer if enabled"

This reverts commit 4bbc336321.
This commit is contained in:
Moritz Halbritter 2023-08-03 17:24:56 +02:00
parent f34dc05452
commit d93d05ade2

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2022 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.
@ -23,7 +23,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import jakarta.validation.Configuration;
import jakarta.validation.Validation;
import org.springframework.boot.autoconfigure.thread.Threading;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
@ -32,9 +31,6 @@ import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.core.NativeDetector;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
@ -51,7 +47,6 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage
* @author Andy Wilkinson
* @author Artsiom Yudovin
* @author Sebastien Deleuze
* @author Moritz Halbritter
* @since 1.3.0
*/
public class BackgroundPreinitializer implements ApplicationListener<SpringApplicationEvent>, Ordered {
@ -84,8 +79,7 @@ public class BackgroundPreinitializer implements ApplicationListener<SpringAppli
}
if (event instanceof ApplicationEnvironmentPreparedEvent
&& preinitializationStarted.compareAndSet(false, true)) {
ConfigurableEnvironment environment = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment();
performPreinitialization(environment);
performPreinitialization();
}
if ((event instanceof ApplicationReadyEvent || event instanceof ApplicationFailedEvent)
&& preinitializationStarted.get()) {
@ -98,9 +92,9 @@ public class BackgroundPreinitializer implements ApplicationListener<SpringAppli
}
}
private void performPreinitialization(Environment environment) {
private void performPreinitialization() {
try {
Runnable runnable = new Runnable() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
@ -108,7 +102,8 @@ public class BackgroundPreinitializer implements ApplicationListener<SpringAppli
runSafely(new ValidationInitializer());
if (!runSafely(new MessageConverterInitializer())) {
// If the MessageConverterInitializer fails to run, we still might
// be able to initialize Jackson
// be able to
// initialize Jackson
runSafely(new JacksonInitializer());
}
runSafely(new CharsetInitializer());
@ -125,12 +120,8 @@ public class BackgroundPreinitializer implements ApplicationListener<SpringAppli
}
}
};
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor("background-preinit-");
if (Threading.VIRTUAL.isActive(environment)) {
taskExecutor.setVirtualThreads(true);
}
taskExecutor.execute(runnable);
}, "background-preinit");
thread.start();
}
catch (Exception ex) {
// This will fail on GAE where creating threads is prohibited. We can safely