[英]Scheduling tasks to run once, using the Spring task namespace
I'm setting up a scheduled tasks scheme in spring, using the task namespace. 我正在使用任务命名空间在spring中设置一个计划任务方案。
I want to schedule most tasks to fire according to a cron expression, and some to fire just once, a fixed delay after startup, and then never again (ie what setting repeatCount
to 0
on a SimpleTriggerBean would achieve). 我想根据cron表达式安排大多数任务,有些只需要启动一次,启动后固定延迟,然后再也不会(即SimpleTriggerBean上的
repeatCount
设置为0
)。
Is it possible to achieve this within the task namespace, or do I need to revert to defining beans for my triggers? 是否有可能在任务命名空间中实现这一点,或者我是否需要恢复为我的触发器定义bean?
If you don't need an initial delay, you can make it run 'just once' on startup as follows: 如果您不需要初始延迟,可以在启动时“运行一次”,如下所示:
<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>
(Of course, if you think your code is going to run for longer than 3E8 years , you may need a different approach...) (当然,如果您认为您的代码运行时间超过3E8年 ,您可能需要采用不同的方法......)
If you need an initial delay, you can configure it as follows (I'm testing with Spring 3.1.1) - this doesn't require any additional dependencies and you don't have to write your own trigger, but you do have to configure the PeriodicTrigger
provided by Spring: 如果你需要一个初始延迟,你可以按如下方式配置它(我正在使用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 appears to support the "initial-delay" attribute directly, but I haven't tested this; Spring 3.2似乎直接支持“initial-delay”属性,但我没有对此进行测试; I'd guess this works:
我猜这有效:
<task:scheduled-tasks>
<task:scheduled ref="myThing" method="doStuff"
fixed-rate="#{ T(java.lang.Long).MAX_VALUE }"
initial-delay="5000"/>
</task:scheduled-tasks>
My working example: 我的工作范例:
<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>
If you have a look at the Task namespace XSD , you'll see that there are only three different configuration types: fixed-delay
, fixed-rate
and cron
. 如果你看一下Task namespace XSD ,你会发现只有三种不同的配置类型:
fixed-delay
, fixed-rate
和cron
。
And if you look at the source of ScheduledTasksBeanDefinitionParser , you'll see that no more than one of these values are evaluated. 如果查看ScheduledTasksBeanDefinitionParser的源代码,您将看到只评估其中一个值。 Here is the relevant part:
以下是相关部分:
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);
}
}
So there is no way to combine these attributes. 所以没有办法结合这些属性。 In short: the namespace won't get you there.
简而言之:命名空间不会让你到那里。
This works and is way easier than the other answers. 这有效,并且比其他答案更容易。
// 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.