知行-fengzf的gravatar头像
知行-fengzf 2023-12-19 11:40:43
记一次SpringMVC异步请求@Async失效问题

1、问题背景

      项目是SpringMVC老项目,网站部分表单提交时间卡慢,4s左右,分段日志时间定为发现其中有3s多是花在了调用发送邮件微服务上,而这段逻辑是异步调用,也就是说异步调用@Async失效。

记一次SpringMVC异步请求@Async失效问题

记一次SpringMVC异步请求@Async失效问题

2、问题分析

(1)、代码中已引入task相关配置

xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd

 

<task:annotation-driven executor="asyncExecutor" scheduler="scheduler"/>
<!-- 支持 @Async 注解 -->
<task:executor id="asyncExecutor" pool-size="100-10000" queue-capacity="10"/>

 

 

<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.gstarcad">
   <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Controller" />
</context:component-scan>

(2)、异步方法和调用类也没有在一个方法中,最后发现注解扫描时,出现了重复实例化,加载两个xml存在覆盖问题,@Async就失效了。

在app-resource.xml中扫描了一次,如上配置

加载app-mvc.xml时又扫了整个包

<context:component-scan base-package="com.gstarcad.website"/>

调整为

<context:component-scan base-package="com.gstarcad.website"
                        use-default-filters="false">
    <context:include-filter type="annotation"
                            expression="org.springframework.stereotype.Controller" />
</context:component-scan>

异步调用生效。

<context:component-scan> 有个属性use-default-filters它的默认值为true,会扫描指定包下的全部的标有@Component的类,并注册成bean。Use-default-filter此时为true那么会对base-package包或者子包下的所有的进行Java类进行扫描,并把@Component的类的java类注册成bean,所以还是不能避免异步方法所在类被重复扫描。
如果需要指定一些包扫描,一些包不扫描,则需要修改Use-dafault-filter的默认值。
Use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描。

3、结论

避免@Async失效

(1)、异步方法和调用类不能在同一个方法中;

2)、注解扫描时,要注意过滤,避免重复实例化,覆盖问题。


打赏
最近浏览
zdmxjxj  LV10 7月20日
CrystalQ  LV8 7月16日
paul_gaopeng  LV5 7月2日
好的好的  LV8 6月26日
Hachi6  LV13 6月25日
3334004690  LV10 6月24日
grant1123  LV2 6月22日
dapeng0011  LV13 6月5日
暂无贡献等级
gnnhka  LV10 5月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友