java @Async 对我不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4060718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:36:52  来源:igfitidea点击:

@Async not working for me

javaspring

提问by Corin Fletcher

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.

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

Here is my config:

这是我的配置:

<!-- 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" />

Thanks.

谢谢。

回答by Corin Fletcher

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 方法时,您可能会绕过异步代理代码而只是调用您的普通方法,即在同一线程中。

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

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

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.

但是做这样的事情会变得混乱,所以你可以自动装配 TaskScheduler,将你的方法包装在一个 Runnable 中并自己执行它。

回答by Kirk Backus

This is a complementary answer to the accepted one. You can call an async method in your own class, but you have to create a self-referential bean.

这是对已接受答案的补充答案。您可以在自己的类中调用异步方法,但必须创建一个自引用 bean。

The only side-effect here is that you cannotcall any async code inside the constructor. It is a nice way to keep your code all in the same place.

这里唯一的副作用是您不能在构造函数中调用任何异步代码。这是将代码保存在同一个地方的好方法。

@Autowired ApplicationContext appContext;
private MyAutowiredService self;

@PostConstruct
private void init() {
    self = appContext.getBean(MyAutowiredService.class);
}

public void doService() {
    //This will invoke the async proxy code
    self.doAsync();
}

@Async 
public void doAsync() {
    //Async logic here...
}

回答by Salim Hamidi

I had a problem similar to this. And I spent a lot of time to fix it.

我遇到了类似的问题。我花了很多时间来修复它。

If you use spring-context 3.2, you also need to add @EnableAsyncon the class where you call the method service annotated @Async

如果使用spring-context 3.2,还需要在调用方法服务的类上添加@EnableAsync注释为@Async

Take a look at http://spring.io/guides/gs/async-method/#initial

看看http://spring.io/guides/gs/async-method/#initial

I hope that it'll help you.

我希望它会帮助你。

回答by Alireza Alallah

You can use @EnableAsyncin your service...

您可以@EnableAsync在您的服务中使用...