@Async不适合我

12 java spring

我正在使用@Scheduled,它一直工作正常,但无法让@Async工作.我测试了很多次,似乎它使我的方法异步.我还缺少其他任何东西,配置或参数吗?我有一个有两个方法的类,一个用@Scheduled标记的方法,执行并调用第二个用@Async标记的方法.

这是我的配置:

<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="com.socialmeety" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<task:annotation-driven/>

<!-- Configures support for @Controllers -->
<mvc:annotation-driven />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<dwr:configuration />
<dwr:annotation-config />
<dwr:url-mapping />
<dwr:controller id="dwrController" debug="true" />

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
Run Code Online (Sandbox Code Playgroud)

谢谢.

Cor*_*her 36

当您从同一对象中的另一个方法调用@Async方法时,您可能绕过异步代理代码并只调用您的普通方法,即在同一个线程内.

解决此问题的一种方法是确保您对@Async方法的调用来自另一个对象.请参阅本文末尾的评论:http: //groovyjavathoughts.blogspot.com/2010/01/asynchronous-code-with-spring-3-simple.html

但是这样做很麻烦,所以你可以自动装配TaskScheduler,将你的方法包装在Runnable中并自己执行.