@Async不适合我 [英] @Async not working for me
问题描述
我正在使用@Scheduled,它一直运行良好,但无法让@Async工作。我测试了很多次,似乎它使我的方法异步。我还缺少其他任何东西,配置或参数吗?我有一个有两个方法的类,一个用@Scheduled标记的方法,执行并调用第二个用@ Async标记的方法。
I am using @Scheduled and it have been working fine, but can't get the @Async working. I tested it many times, and seems that it is making my method asynchronous. Is there any other thing, configuration, or parameter I am missing? I have one class that has two methods one, the method marked with @Scheduled, executes and calls the second one which has been marked with @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方法时,您可能绕过了异步代理代码,只是调用你的普通方法,即在同一个线程内。
As you're calling your @Async method from another method in the same object, you're probably bypassing the async proxy code and just calling your plain method, ie within the same thread.
解决这个问题的一种方法是确保你对@Async方法的调用来自另一个对象。请参阅本文末尾的评论:
http://groovyjavathoughts.blogspot.com/2010/01/asynchronous-code-with-spring-3-simple.html
One way of solving this is by making sure your call to the @Async method is from another object. See comments at end of this article: http://groovyjavathoughts.blogspot.com/2010/01/asynchronous-code-with-spring-3-simple.html
但它得到了凌乱的做这样的事情,所以你可以自动装配TaskScheduler,将你的方法包装在Runnable中并自己执行。
But it gets messy doing things like that, so you could just autowire the TaskScheduler, wrap up your method in a Runnable and execute it yourself.
这篇关于@Async不适合我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!