本文介绍了'initial-delay'属性不能与cron和触发任务一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building few crons to run, one of the cron i need to run after some time the server has started.

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

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

I need to run this cron at every 15 seconds, which it does. But i need to run this cron after 45 seconds server has started and not immediate.

下面是我的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 ,您会看到 cron initial-delay 不兼容:

From the code of ScheduledTasksBeanDefinitionParser, you can see that cron and initial-delay are not compatible :

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
            }

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

You may want to use the fixed-delay implementation, e.g :

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

请参见Spring 33.3.2触发实现

See Spring documentation at section 33.3.2 Trigger implementations

这篇关于'initial-delay'属性不能与cron和触发任务一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!