SpringMVC定时任务(task)开启多线程执行

  spring的task默认是单线程执行,如果定时任务过多,某个任务执行时间过长,就可能影响到其他任务的执行频率,因此,有必要给其添加多线程并行执行,可以有效降低任务被影响的几率。解决方案就是修改xml文件配置,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/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/task
          http://www.springframework.org/schema/task/spring-task.xsd"
          default-autowire="byName" default-lazy-init="false">

    <!-- 1.扫描指定的包 -->
    <context:component-scan base-package="com.qfx.system.task"/>

    <!-- 2.启用定时任务 -->
    <!-- <task:annotation-driven /> -->
    <task:annotation-driven scheduler="myTask" />

    <!-- 3.配置定时任务的线程池 -->
    <task:scheduler id="myTask" pool-size="5"/>
</beans>

猜你喜欢

转载自blog.51cto.com/1197822/2425701
今日推荐