代码之家  ›  专栏  ›  技术社区  ›  Karl Johansson

使用Spring任务命名空间将任务调度为运行一次

  •  15
  • Karl Johansson  · 技术社区  · 14 年前

    我正在spring中设置一个调度任务方案,使用任务名称空间。

    repeatCount 0

    4 回复  |  直到 14 年前
        1
  •  7
  •   Sean Patrick Floyd    14 年前

    如果你看一下 Task namespace XSD ,您将看到只有三种不同的配置类型: fixed-delay , fixed-rate cron .

    ScheduledTasksBeanDefinitionParser ,您将看到计算的值不超过一个。以下是相关部分:

    String cronAttribute = taskElement.getAttribute("cron");
    if (StringUtils.hasText(cronAttribute)) {
        cronTaskMap.put(runnableBeanRef, cronAttribute);
    }
    else {
        String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
        if (StringUtils.hasText(fixedDelayAttribute)) {
            fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
        }
        else {
            String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
            if (!StringUtils.hasText(fixedRateAttribute)) {
                parserContext.getReaderContext().error(
                        "One of 'cron', 'fixed-delay', or 'fixed-rate' is required",
                        taskElement);
                // Continue with the possible next task element
                continue;
            }
            fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
        }
    }
    

        2
  •  15
  •   bacar    11 年前

    如果您不需要初始延迟,可以使其在启动时“只运行一次”,如下所示:

    <task:scheduled-tasks>
        <!--  Long.MAX_VALUE ms = 3E8 years; will run on startup 
                      and not run again for 3E8 years --> 
        <task:scheduled ref="myThing" method="doStuff" 
                    fixed-rate="#{ T(java.lang.Long).MAX_VALUE }" />
    </task:scheduled-tasks>
    

    (当然,如果您认为您的代码将运行超过 3E8 years ,您可能需要另一种方法……)

    如果您需要一个初始延迟,您可以如下配置它(我正在用Spring3.1.1进行测试)-这不需要任何额外的依赖项,您也不必编写自己的触发器,但是您必须配置 PeriodicTrigger Spring提供:

    <bean id="onstart" class="org.springframework.scheduling.support.PeriodicTrigger" > 
        <!--  Long.MAX_VALUE ms = 3E8 years; will run 5s after startup and
                   not run again for 3E8 years --> 
        <constructor-arg name="period" value="#{ T(java.lang.Long).MAX_VALUE }" /> 
        <property name="initialDelay" value="5000" /> 
    </bean> 
    <task:scheduled-tasks> 
        <task:scheduled ref="myThing" method="doStuff" trigger="onstart" /> 
    </task:scheduled-tasks> 
    

    Spring3.2似乎直接支持“initialdelay”属性,但我还没有对此进行测试;我猜这是可行的:

    <task:scheduled-tasks>
        <task:scheduled ref="myThing" method="doStuff" 
                            fixed-rate="#{ T(java.lang.Long).MAX_VALUE }" 
                            initial-delay="5000"/>
    </task:scheduled-tasks>
    
        3
  •  13
  •   aruizca    14 年前

    我的工作示例:

    <bean id="whateverTriggerAtStartupTime" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail" ref="whateverJob"/>
        <property name="repeatCount" value="0"/>
        <property name="repeatInterval" value="10"/>
    </bean>
    
        4
  •  3
  •   Jeremy Gehrs    13 年前

        // Will fire the trigger 1 + repeatCount number of times, start delay is in milliseconds
        simple name: 'mySimpleTrigger', startDelay: 5000, repeatCount: 0