Close
« Tutorial

Spring MVC - Async Request Configuration MCQ

0/13
0 ✓
0 ✗

1. Spring async request processing is built on the servlet asynchronous support. What must be enabled on the DispatcherServlet for it to work?

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 {
}

3. A handler method returns a Callable. Which TaskExecutor does Spring MVC use by default to run it?

4. Which return type of a handler method makes Spring run the work in a thread managed by a TaskExecutor?

5. Which method of WebMvcConfigurer do you override to set a custom TaskExecutor for async requests?

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
}

7. Which statement best describes the corePoolSize setting of a ThreadPoolTaskExecutor?

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();

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?

@GetMapping("/hello")
@ResponseBody
public Callable<String> hello() {
    print(Thread.currentThread().getName());
    return () -> {
        print(Thread.currentThread().getName());
        return "hi";
    };
}

10. A handler returns a Callable. What does request.isAsyncSupported() return when async support is enabled correctly?

@GetMapping("/check")
@ResponseBody
public Callable<String> check(
        HttpServletRequest request) {
    return () -> "" + request.isAsyncSupported();
}

11. What can you do by wrapping a Callable in a WebAsyncTask?

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
}

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);
Join