使用Spring 3.2.RC2使控制器方法异步的问题

bal*_*teo 2 asynchronous controller spring-mvc

我正在尝试测试Spring MVC控制器最新异步功能,但我无法让它工作.

这是我的异步方法的代码:

@RequestMapping(value = "/hello")
    public Callable<String> async(final Model model) {
        System.out.println("entered async controller method");
        return new Callable<String>() {
            public String call() throws Exception {
                Thread.sleep(2000L);
                model.addAttribute("message", "asyncRequest dealt with");
                System.out.println("about to return from call()");
                return "hello";
            }
        };
}
Run Code Online (Sandbox Code Playgroud)

以下是web.xml的相关部分:

<servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
Run Code Online (Sandbox Code Playgroud)

然而,"即将从call()返回"从未打印在控制台中,我永远不会看到这样的日志:08:25:17 [MvcAsync1] WebAsyncManager - ...在控制台中......

仅供参考,我使用的是Spring 3.2.RC2

use*_*995 6

进一步配置async-support工具aAsyncTaskExecutor

默认情况下,Spring MVC使用a SimpleAsyncTaskExecutor来执行控制器方法返回的Callable实例.对于生产,您必须将其替换AsyncTaskExecutor为适合您的环境的实现.

创建AsyncTaskExecutor

<beans:bean id="asyncTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <beans:property name="corePoolSize" value="5" />
    <beans:property name="maxPoolSize" value="10" />
    <beans:property name="queueCapacity" value="25" />
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

然后分配 task-executor

<mvc:annotation-driven >
    <mvc:async-support default-timeout="30000" task-executor="asyncTaskExecutor" />
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)

http://blog.springsource.org/2012/05/10/spring-mvc-3-2-preview-making-a-controller-method-asynchronous/