Skip to content

Support virtual threads for Jackson pool. #39800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

package org.springframework.boot.autoconfigure.http;

import com.fasterxml.jackson.core.util.JsonRecyclerPools;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.thread.Threading;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
Expand Down Expand Up @@ -63,8 +66,13 @@ protected static class MappingJackson2XmlHttpMessageConverterConfiguration {
@Bean
@ConditionalOnMissingBean
public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter(
Jackson2ObjectMapperBuilder builder) {
return new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build());
Jackson2ObjectMapperBuilder builder, Environment environment) {
ObjectMapper om = builder.createXmlMapper(true).build();
if (Threading.VIRTUAL.isActive(environment)) {
om.getFactory().setRecyclerPool(JsonRecyclerPools.sharedLockFreePool());
}

return new MappingJackson2XmlHttpMessageConverter(om);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.util.JsonRecyclerPools;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
Expand All @@ -49,6 +50,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties.ConstructorDetectorStrategy;
import org.springframework.boot.autoconfigure.thread.Threading;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jackson.JsonComponentModule;
import org.springframework.boot.jackson.JsonMixinModule;
Expand All @@ -59,6 +61,7 @@
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -127,8 +130,13 @@ static class JacksonObjectMapperConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build();
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder, Environment environment) {
ObjectMapper om = builder.createXmlMapper(false).build();
if (Threading.VIRTUAL.isActive(environment)) {
om.getFactory().setRecyclerPool(JsonRecyclerPools.sharedLockFreePool());
}

return om;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@

import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.util.JsonRecyclerPools.LockFreePool;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import jakarta.json.bind.Jsonb;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
Expand Down Expand Up @@ -292,6 +296,21 @@ void shouldRegisterHints() {
assertThat(RuntimeHintsPredicates.reflection().onMethod(Encoding.class, "shouldForce")).rejects(hints);
}

@Test
@EnabledOnJre(JRE.JAVA_21)
void shouldUseVirtualThreads() {
this.contextRunner.withUserConfiguration(JacksonObjectMapperBuilderConfig.class)
.withPropertyValues("spring.threads.virtual.enabled:true")
.run((context) -> {
MappingJackson2XmlHttpMessageConverter converter = context
.getBean("mappingJackson2XmlHttpMessageConverter", MappingJackson2XmlHttpMessageConverter.class);
assertThat(converter).extracting(MappingJackson2XmlHttpMessageConverter::getObjectMapper)
.extracting(ObjectMapper::getFactory)
.extracting(JsonFactory::_getRecyclerPool)
.isInstanceOf(LockFreePool.class);
});
}

private ApplicationContextRunner allOptionsRunner() {
return this.contextRunner.withConfiguration(AutoConfigurations.of(GsonAutoConfiguration.class,
JacksonAutoConfiguration.class, JsonbAutoConfiguration.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.util.JsonRecyclerPools.LockFreePool;
import com.fasterxml.jackson.core.util.JsonRecyclerPools.ThreadLocalPool;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand All @@ -53,6 +56,8 @@
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.ReflectionHintsPredicates;
Expand Down Expand Up @@ -491,6 +496,27 @@ void shouldRegisterPropertyNamingStrategyHints() {
"UPPER_CAMEL_CASE", "SNAKE_CASE", "UPPER_SNAKE_CASE", "LOWER_CASE", "KEBAB_CASE", "LOWER_DOT_CASE");
}

@Test
void shouldUseThreadLocalPool() {
this.contextRunner.withPropertyValues("spring.threads.virtual.enabled:false").run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
assertThat(mapper).extracting(ObjectMapper::getFactory)
.extracting(JsonFactory::_getRecyclerPool)
.isInstanceOf(ThreadLocalPool.class);
});
}

@Test
@EnabledOnJre(JRE.JAVA_21)
void shouldUseVirtualThread() {
this.contextRunner.withPropertyValues("spring.threads.virtual.enabled:true").run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
assertThat(mapper).extracting(ObjectMapper::getFactory)
.extracting(JsonFactory::_getRecyclerPool)
.isInstanceOf(LockFreePool.class);
});
}

private void shouldRegisterPropertyNamingStrategyHints(Class<?> type, String... fieldNames) {
RuntimeHints hints = new RuntimeHints();
new JacksonAutoConfigurationRuntimeHints().registerHints(hints, getClass().getClassLoader());
Expand Down