1. Spring async request processing is built on the servlet asynchronous support. What must be enabled on the DispatcherServlet for it to work?
Why: Spring async processing relies on the servlet async feature, so the servlet must be marked with asyncSupported set to true.
2. In a Java configuration class, which annotation enables Spring MVC infrastructure, including support for async return types such as Callable?
@Configuration
// Add MVC annotation here
public class AppConfig
implements WebMvcConfigurer {
}
Why:@EnableWebMvc enables Spring MVC Java configuration. The servlet's asyncSupported flag must still be true on the DispatcherServlet registration; Spring's AbstractAnnotationConfigDispatcherServletInitializer sets that to true by default.
3. A handler method returns a Callable. Which TaskExecutor does Spring MVC use by default to run it?
Why: By default, Spring MVC uses SimpleAsyncTaskExecutor. It starts a new thread for each task and does not reuse threads.
4. Which return type of a handler method makes Spring run the work in a thread managed by a TaskExecutor?
Why: When a handler returns Callable or StreamingResponseBody, Spring manages the thread and uses a TaskExecutor to run the work.
5. Which method of WebMvcConfigurer do you override to set a custom TaskExecutor for async requests?
Why: The configureAsyncSupport(AsyncSupportConfigurer) method is the place to set a custom executor for async request handling.
6. Which code correctly registers a custom executor named exec in the configurer?
@Override
public void configureAsyncSupport(
AsyncSupportConfigurer configurer) {
ThreadPoolTaskExecutor exec =
new ThreadPoolTaskExecutor();
exec.setCorePoolSize(5);
exec.initialize();
// Register the executor here
}
Why:AsyncSupportConfigurer.setTaskExecutor() sets the executor that Spring MVC uses for async request processing.
7. Which statement best describes the corePoolSize setting of a ThreadPoolTaskExecutor?
Why:corePoolSize is the number of threads the pool keeps ready. maxPoolSize is the upper limit, and queueCapacity is the number of tasks that can wait.
8. A ThreadPoolTaskExecutor is created with a thread name prefix. What is the main benefit of setting it?
ThreadPoolTaskExecutor t =
new ThreadPoolTaskExecutor();
t.setThreadNamePrefix("async-");
t.initialize();
Why: A thread name prefix gives threads clear names like async-1, which makes it easy to see which threads handled the async work in logs.
9. A handler is mapped as shown. A custom executor with the prefix worker- is configured. What is the most likely order of the printed thread names?
Why: When the servlet is set up for async processing, isAsyncSupported() returns true for the request.
11. What can you do by wrapping a Callable in a WebAsyncTask?
Why:WebAsyncTask lets you set a timeout and a task executor for a single Callable, instead of using the global settings.
12. Which return statement correctly wraps work into a task with a 5 second timeout and the executor exec?
@GetMapping("/task")
@ResponseBody
public WebAsyncTask<String> task() {
Callable<String> work = () -> "done";
ConcurrentTaskExecutor exec =
new ConcurrentTaskExecutor(
Executors.newFixedThreadPool(1));
// Return the task here
}
Why: The WebAsyncTask constructor takes the timeout in milliseconds first, then the executor, then the Callable.
13. A global executor with the prefix global- is configured. A handler returns the WebAsyncTask below. Which thread runs the Callable?
Callable<String> work = () -> {
print(Thread.currentThread().getName());
return "ok";
};
ConcurrentTaskExecutor exec =
new ConcurrentTaskExecutor(
Executors.newFixedThreadPool(1));
return new WebAsyncTask<>(10000L, exec, work);
Why: The executor given to WebAsyncTask is used for that task. It takes priority over the global executor set in the configuration.