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



我正在构建几个要运行的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

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
            }

您可能希望使用固定延迟实现,例如:

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

参见 Spring 文档第 33.3.2 节 触发器实现

最新更新