I would like to configure asynchronous processing support for a subset of request mappings in my Spring Boot application which match /async/*. Examples:
localhost:8080/async/downloadLargeFilelocalhost:8080/async/longRunningRask
Taking the first example, I have implemented my method as follows using StreamingResponseBody:
@GetMapping
public ResponseEntity<StreamingResponseBody> downloadLargeFile() throws IOException {
long size = Files.size(path);
InputStream inputStream = Files.newInputStream(path);
return ResponseEntity.ok()
.contentLength(size)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=large_file.txt")
.body(inputStream::transferTo);
}
In the documentation for StreamingResponseBody, it states I should be configuring an AsyncTaskExecutor, so I have this @Configuration class which implements WebMvcConfigurer as well:
@Configuration
public class AsyncConfigurer implements WebMvcConfigurer {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setDefaultTimeout(-1);
configurer.setTaskExecutor(asyncTaskExecutor());
}
@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
return new SimpleAsyncTaskExecutor("async");
}
}
However, I cannot find a way to use this task executor only with requests that match a given pattern.
As a more general question - how do I restrict the WebMvcConfigurer to apply only to a subset of requests matching a pattern?
If this is not possible or not recommended, what is the proper way to accomplish the same behaviour?
SimpleAsyncTaskExecutorbut rather a threadpool based one. - M. DeinumAsyncTaskExecutorfor other requests as well, but limiting it to working for just one would already be of assistance. Are you suggesting that I may want to use a pooled one if I have several async requests? - Paul BennTaskExecutoryou define, so that the regular request handling threads are free to handle other incoming requests again. - M. DeinumFuture,Flux, or aStreamingResponseBodyare handled async (check the spring documentation for that). - M. Deinum