Spring,Java - 并行运行许多计划任务

时间:2017-11-14 10:48:46

标签: java spring scheduled-tasks executor

我尝试与调度程序并行运行许多计划任务,但只有一个启动。 我在spring-scheduler.xml中的bean配置

<?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:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <task:executor id="executor" pool-size="5"/>
    <task:scheduler id="scheduler" pool-size="10"/>

    <bean id="UpdateScheduler" class="org.ws.scheduled.UpdateScheduler" />
    <bean id="PatchData" class="org.ws.scheduled.PatchData" />

    <task:scheduled-tasks> 
        <task:scheduled ref="UpdateScheduler" method="start" fixed-delay="1000" />
        <task:scheduled ref="PatchData" method="start" fixed-delay="5000" />
    </task:scheduled-tasks>

    <task:annotation-driven scheduler="scheduler" executor="executor" />

</beans>

1 个答案:

答案 0 :(得分:0)

谢谢大家! 我开始使用此配置,它在https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#scheduling-task-namespace-scheduled-tasks

工作正常
<task:scheduled-tasks scheduler="myScheduler">
    <task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000"/>
    <task:scheduled ref="beanB" method="methodB" fixed-rate="5000"/>
    <task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI"/>