Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

java - @Async not working for me

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...