Java 使用Spring任务命名空间安排任务运行一次
我正在spring中使用任务名称空间设置一个调度任务方案 我想根据cron表达式安排大多数任务启动,有些任务只启动一次,启动后的固定延迟,然后再也不会启动(即,在SimpleTriggerBean上将Java 使用Spring任务命名空间安排任务运行一次,java,spring,Java,Spring,我正在spring中使用任务名称空间设置一个调度任务方案 我想根据cron表达式安排大多数任务启动,有些任务只启动一次,启动后的固定延迟,然后再也不会启动(即,在SimpleTriggerBean上将repeatCount设置为0可以实现什么效果) 是否可以在任务名称空间中实现这一点,或者我需要返回到为触发器定义bean吗?如果您查看,您将看到只有三种不同的配置类型:固定延迟、固定速率和cron 如果你看一看数据的来源,你会发现这些值中只计算了一个。以下是相关部分: String cronAtt
repeatCount
设置为0
可以实现什么效果)
是否可以在任务名称空间中实现这一点,或者我需要返回到为触发器定义bean吗?如果您查看,您将看到只有三种不同的配置类型:固定延迟
、固定速率
和cron
如果你看一看数据的来源,你会发现这些值中只计算了一个。以下是相关部分:
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);
}
}
因此,无法组合这些属性。简言之:名称空间无法将您带到那里。我的工作示例:
<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>
这个方法有效,而且比其他答案简单得多
// Will fire the trigger 1 + repeatCount number of times, start delay is in milliseconds
simple name: 'mySimpleTrigger', startDelay: 5000, repeatCount: 0
如果您不需要初始延迟,可以在启动时使其“仅运行一次”,如下所示:
<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>
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>
<task:scheduled-tasks>
<task:scheduled ref="myThing" method="doStuff"
fixed-rate="#{ T(java.lang.Long).MAX_VALUE }"
initial-delay="5000"/>
</task:scheduled-tasks>