'initial-delay' 属性不能与 cron 和 trigger 任务一起使用

Ank*_*hal 3 java cron spring scheduled-tasks

我正在构建几个要运行的 cron,我需要在服务器启动一段时间后运行的 cron 之一。

<task:scheduled ref="myCron"
            method="processData" cron="0/15 * * * * ?" initial-delay="45000"></task:scheduled>
Run Code Online (Sandbox Code Playgroud)

我需要每 15 秒运行一次这个 cron,它确实做到了。但是我需要在服务器启动 45 秒后运行这个 cron,而不是立即运行。

下面是我的xsd,

<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-3.2.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.2.xsd"
    default-lazy-init="false">
Run Code Online (Sandbox Code Playgroud)

例外

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: the 'initial-delay' attribute may not be used with cron and trigger tasks
Run Code Online (Sandbox Code Playgroud)

Arn*_*aud 5

从 的代码ScheduledTasksBeanDefinitionParser可以看出croninitial-delay不兼容:

if (hasInitialDelayAttribute && (hasCronAttribute || hasTriggerAttribute)) {
                parserContext.getReaderContext().error(
                        "the 'initial-delay' attribute may not be used with cron and trigger tasks", taskElement);
                continue; // with the possible next task element
            }
Run Code Online (Sandbox Code Playgroud)

您可能想要使用固定延迟实现,例如:

<task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000"/>
Run Code Online (Sandbox Code Playgroud)

请参阅第33.3.2触发器实现中的Spring文档