'initial-delay' 属性不能用于 cron 和触发器任务

the 'initial-delay' attribute may not be used with cron and trigger tasks

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

<task:scheduled ref="myCron"
            method="processData" cron="0/15 * * * * ?" initial-delay="45000"></task:scheduled>

我需要每 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">

异常

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: the 'initial-delay' attribute may not be used with cron and trigger tasks

ScheduledTasksBeanDefinitionParsercode可以看出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
            }

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

<task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000"/>

请参阅 Spring documentation 部分 33.3.2 触发器实现