簡體   English   中英

使用Spring任務命名空間調度任務運行一次

[英]Scheduling tasks to run once, using the Spring task namespace

我正在使用任務命名空間在spring中設置一個計划任務方案。

我想根據cron表達式安排大多數任務,有些只需要啟動一次,啟動后固定延遲,然后再也不會(即SimpleTriggerBean上的repeatCount設置為0 )。

是否有可能在任務命名空間中實現這一點,或者我是否需要恢復為我的觸發器定義bean?

如果您不需要初始延遲,可以在啟動時“運行一次”,如下所示:

<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年 ,您可能需要采用不同的方法......)

如果你需要一個初始延遲,你可以按如下方式配置它(我正在使用Spring 3.1.1測試) - 這不需要任何額外的依賴項,你不必編寫自己的觸發器,但你必須配置Spring提供的PeriodicTrigger

<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> 

Spring 3.2似乎直接支持“initial-delay”屬性,但我沒有對此進行測試; 我猜這有效:

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

我的工作范例:

<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>

如果你看一下Task namespace XSD ,你會發現只有三種不同的配置類型: fixed-delayfixed-ratecron

如果查看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);
    }
}

所以沒有辦法結合這些屬性。 簡而言之:命名空間不會讓你到那里。

這有效,並且比其他答案更容易。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM