Add BlockingExecutionConfigurer to WebFlux config

Closes gh-30678
This commit is contained in:
rstoyanchev
2023-07-12 16:54:45 +01:00
parent f40d1f2329
commit b016f385e1
10 changed files with 334 additions and 20 deletions

View File

@@ -18,6 +18,8 @@ package org.springframework.web.reactive.result.method.annotation;
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import org.reactivestreams.Publisher;
@@ -25,6 +27,8 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
@@ -33,7 +37,10 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.config.BlockingExecutionConfigurer;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.server.adapter.ForwardedHeaderTransformer;
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
@@ -70,6 +77,9 @@ class RequestMappingIntegrationTests extends AbstractRequestMappingIntegrationTe
url += "/";
assertThat(getRestTemplate().getForObject(url, String.class)).isEqualTo("root");
assertThat(getApplicationContext().getBean(TestExecutor.class).invocationCount.get()).isEqualTo(2);
assertThat(getApplicationContext().getBean(TestPredicate.class).invocationCount.get()).isEqualTo(2);
}
@ParameterizedHttpServerTest
@@ -109,7 +119,34 @@ class RequestMappingIntegrationTests extends AbstractRequestMappingIntegrationTe
@Configuration
@EnableWebFlux
static class WebConfig {
static class WebConfig implements WebFluxConfigurer {
@Override
public void configureBlockingExecution(BlockingExecutionConfigurer configurer) {
configurer.setExecutor(executor());
configurer.setControllerMethodPredicate(predicate());
}
@Bean
TestExecutor executor() {
return new TestExecutor();
}
@Bean
TestPredicate predicate() {
return new TestPredicate();
}
}
@Configuration
static class LocalConfig {
@Bean
public ForwardedHeaderTransformer forwardedHeaderTransformer() {
return new ForwardedHeaderTransformer();
}
}
@@ -145,12 +182,27 @@ class RequestMappingIntegrationTests extends AbstractRequestMappingIntegrationTe
}
@Configuration
static class LocalConfig {
private static class TestExecutor implements AsyncTaskExecutor {
@Bean
public ForwardedHeaderTransformer forwardedHeaderTransformer() {
return new ForwardedHeaderTransformer();
private final AtomicInteger invocationCount = new AtomicInteger();
@Override
public void execute(Runnable task) {
this.invocationCount.incrementAndGet();
task.run();
}
}
private static class TestPredicate implements Predicate<HandlerMethod> {
private final AtomicInteger invocationCount = new AtomicInteger();
@Override
public boolean test(HandlerMethod handlerMethod) {
this.invocationCount.incrementAndGet();
Class<?> returnType = handlerMethod.getReturnType().getParameterType();
return (ReactiveAdapterRegistry.getSharedInstance().getAdapter(returnType) == null);
}
}