@Async
@Service
中的方法-annotated類不是異步調用 - 它阻塞了線程。Spring @Async不工作
我在我的配置中有<task: annotation-driven />
,並且對該方法的調用來自類之外,因此代理應該被擊中。當我遍歷代碼時,代理確實被擊中了,但它似乎並不在任何與任務執行器中運行相關的類中。
我已經在AsyncExecutionInterceptor
中放置了斷點,並且它們從未受到影響。我已調試到AsyncAnnotationBeanPostProcessor
,並可以看到獲得應用的建議。
該服務也被定義爲一個接口(其中註釋的方法爲@Async
),實現的方法也註釋爲@Async
。都沒有標記@Transactional
。
任何想法可能出了什麼問題?
- =更新= -
奇怪的是,它的工作原理只有時,我有我的應用程序-servlet.xml文件我task
XML元素,而不是在我的應用程序-services.xml文件,如果我也從那裏做服務的組件掃描。通常情況下,我有一個XML文件,其中只有控制器(並相應地限制組件掃描),另一個文件中包含服務(再次限制組件掃描,以便它不會重新掃描其他加載的控制器文件)。
APP-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
>
<task:annotation-driven executor="executor" />
<task:executor id="executor" pool-size="7"/>
<!-- Enable controller annotations -->
<context:component-scan base-package="com.package.store">
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> -->
</context:component-scan>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
APP-services.xml的(沒有的時候,這裏指定的工作)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- Set up Spring to scan through various packages to find annotated classes -->
<context:component-scan base-package="com.package.store">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<task:annotation-driven executor="han" />
<task:executor id="han" pool-size="6"/>
...
我這麼想昭然若揭在我的配置中很明顯,還是在配置元素之間存在一些微妙的相互作用?
你確定你使用Spring''@ ASync'類型,而不是另一個庫嗎? – skaffman
'import org.springframework.scheduling.annotation.Async;'是我正在使用的。我已經看到,當代理被調用時,它認爲聲明的類(接口)沒有任何類或方法級別的註釋 - 這是一個謊言! –
只是爲了仔細檢查,該方法是外部調用,而不是從服務內部正確?並且您正在調用由Spring構建的服務的方法,而不是您已經「新」編輯的服務? – Pace