我正在使用@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" />
谢谢。
最佳答案
当您从同一对象中的另一个方法调用 @Async 方法时,您可能会绕过异步代理代码,而只是调用您的普通方法,即在同一个线程中。
解决此问题的一种方法是确保您对 @Async 方法的调用来自另一个对象。请参阅本文末尾的评论: http://groovyjavathoughts.blogspot.com/2010/01/asynchronous-code-with-spring-3-simple.html
但是这样做会很麻烦,所以您可以 Autowiring TaskScheduler,将您的方法包装在 Runnable 中并自己执行。
关于java - @Async 不适合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4060718/